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:
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
Shen HengbinPosted Jul 22, 2012, 4:15 AM
Pankaj SinghPosted Jul 22, 2012, 1:21 PM
Pankaj SinghPosted Jul 21, 2012, 9:24 PM
Shen HengbinPosted Jul 21, 2012, 6:06 AM
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRow row = ((DataRowView)e.Row.DataItem).Row;
int id = Convert.ToInt32(row.ItemArray[0]);
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();
}
Pankaj SinghPosted Jul 20, 2012, 1:24 PM
Shen HengbinPosted Jul 20, 2012, 5:00 AM
if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
To
if (e.Row.RowType == DataControlRowType.DataRow)
DataControlRowType.Header is the header row.