6
Answers

Gridview issue

Ask a question
Pankaj Singh

Pankaj Singh

12y
4.5k
1
Hi,

I am binding data from SQL table in gridview. I want to change one column(command field) value not header at runtime. I am using following code but it change header also.

HTML code:

<Columns>
<asp:BoundField DataField="id" HeaderText="ID" />
<asp:ImageField DataImageUrlField="imgpath" HeaderText="Photo">
<ControlStyle Height="50px" Width="50px" />
</asp:ImageField>
<asp:BoundField DataField="name" HeaderText="Name" />
<asp:BoundField DataField="Country" HeaderText="Country" />
<asp:CommandField HeaderText="Status" SelectText="Apply"
ShowSelectButton="True">
<ControlStyle ForeColor="Blue" />
</asp:CommandField>
</Columns>

server side code:

protected void grddetails_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
{
int len = ds.Tables[0].Rows.Count;
for (int i = 0; i < len; i++)
{
int id = Convert.ToInt32(ds.Tables[0].Rows[i].ItemArray[0]);
// check ID exists or not
com = new SqlCommand("SELECT 1 FROM Table WHERE id=" + id + "", con);
con.Open();
drd = com.ExecuteReader();
if (drd.Read())
e.Row.Cells[4].Text="Apply";

else

e.Row.Cells[4].Text="not Apply";
con.Close();

}

}

Please help me

Thanks in advance

Answers (6)