Please try to use OnRowDataBound to add the linkbutton dynamically.
And LinkButton lbl = (LinkButton)e.Row.Cells[1].FindControl("LinkButton1") is to find an exist control in the gridview not to create one.
You could refer to below code:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (division == "SOUTH")
{
LinkButton lbl = new LinkButton();
foreach (GridViewRow r in GridView1.Rows)
{
string str = r.Cells[0].Text;
string str1 = str.Substring(0, str.IndexOf("(")).Trim();
if (str1 == "Abnormal")
{
con.Open();
cmd = new OracleCommand(" ", con);
dr = cmd.ExecuteReader();
while (dr.Read())
{
lbl.Text = dr[0].ToString();
}
r.Cells[1].Controls.Add(lbl);
con.Close();
}
else if (str1 == "Sub-Normal")
{
con.Open();
cmd = new OracleCommand(" ", con);
dr = cmd.ExecuteReader();
while (dr.Read())
{
lbl.Text = dr[0].ToString();
}
r.Cells[1].Controls.Add(lbl);
con.Close();
}
else if (str1 == "Normal")
{
con.Open();
cmd = new OracleCommand(" ", con);
dr = cmd.ExecuteReader();
while (dr.Read())
{
lbl.Text.Text = dr[0].ToString();
}
r.Cells[1].Controls.Add(lbl);
con.Close();
}
else if (str1 == "Critical")
{
con.Open();
cmd = new OracleCommand(" ", con);
dr = cmd.ExecuteReader();
while (dr.Read())
{
lbl.Text = dr[0].ToString();
}
r.Cells[1].Controls.Add(lbl);
con.Close();
}
else if (str1 == "Super Critical")
{
con.Open();
cmd = new OracleCommand(" ", con);
dr = cmd.ExecuteReader();
while (dr.Read())
{
lbl.Text = dr[0].ToString();
}
r.Cells[1].Controls.Add(lbl);
con.Close();
}
}
for (int i = 2; i < GridView1.Columns.Count; i++)
{
GridView1.Columns[i].Visible = false;
}
GridView1.Columns[6].Visible = true;
}
}
}
Best Regards
Cathy
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
None
0 Points
1 Post
How to add multiple linkbutton in gridview Dynamically
Jan 25, 2017 06:10 AM|ryan_xcoder|LINK
I have to add linkbutton to each row in gridview dynamically. When i'm running my code i'm getting error of object reference.
each link button is fired separate query so i have to check which button is click in which row and column.
Here is my code:
Thanks in advanced.
Star
8670 Points
2882 Posts
Re: How to add multiple linkbutton in gridview Dynamically
Jan 26, 2017 06:23 AM|Cathy Zou|LINK
HI ryan_xcoder,
Please try to use OnRowDataBound to add the linkbutton dynamically.
And LinkButton lbl = (LinkButton)e.Row.Cells[1].FindControl("LinkButton1") is to find an exist control in the gridview not to create one.
You could refer to below code:
Best Regards
Cathy
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.