I am trying to set label text which resides in gridview
on RowDataBound
event of gridview
.The code for the same is as follows:
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int day = Convert.ToInt32(DropDownList3.SelectedValue);
int days = System.DateTime.DaysInMonth(2013,day);
Label lab = (Label)e.Row.FindControl("d");
lab.Text = days.ToString();
}
}
GRIDVIEW STRUCTURE:
<asp:GridView ID="GridView2" runat="server" OnRowDataBound="GridView2_RowDataBound" />
<asp:BoundField DataField="name" HeaderText="Name" SortExpression="n" />
<asp:BoundField DataField="dept" HeaderText="Department" SortExpression="d" />
<asp:BoundField DataField="code" HeaderText="Employee Code" SortExpression="c" />
<asp:TemplateField HeaderText="Total days" >
<itemtemplate>
<asp:Label ID="d" runat="server" Text="" />
</itemtemplate>
</asp:TemplateField>
</asp:GridView>
In the above code,'d' is the id of the label in the gridview.On the highlighted line i am getting error that OBJECT REFERENCE NOT SET TO INSTANCE OF AN OBJECT.
As far as I understand, the new label is not being created.Then how to assign values calculated in this event to gridview's label?