You cannot add columns to GridView at runtime
Here's the sample, you will notice that my GridView has 3 columns and i have bind the DownStream in the 3rd Column, but when rendered it displays Notification since I m changing its value at runtime
<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound">
<Columns>
<asp:BoundField DataField="station_name" HeaderText="station_name" ItemStyle-Width="150" />
<asp:BoundField DataField="state" HeaderText="state" ItemStyle-Width="150" />
<asp:BoundField DataField="down_streamvalue" HeaderText="notification" ItemStyle-Width="150" />
</Columns>
</asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Station_Name"), new DataColumn("State"), new DataColumn("down_streamvalue") });
dt.Rows.Add("Mumbai", "Maharastra", 3);
dt.Rows.Add("Delhi", "Delhi", 7);
dt.Rows.Add("Kolkatta", "West Bengal", 11);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void OnRowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int downStream = int.Parse(e.Row.Cells[2].Text);
if (downStream >= 0 && downStream < 5)
{
e.Row.Cells[2].Text = "Normal";
}
if (downStream >= 5 && downStream < 10)
{
e.Row.Cells[2].Text = "Warning";
}
if (downStream >= 10)
{
e.Row.Cells[2].Text = "Danger";
}
}
}