I have a grid in which i wanted to bind header columns of grid dynamically.
For that i made grid as:
<asp:GridView ID="gvSearch" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkSubjects1" runat="server" />
</HeaderTemplate>
<HeaderTemplate>
<asp:CheckBox ID="chkSubjects2" runat="server" />
</HeaderTemplate>
<HeaderTemplate>
<asp:CheckBox ID="chkSubjects3" runat="server" />
</HeaderTemplate>
<HeaderTemplate>
<asp:CheckBox ID="chkSubjects4" runat="server" />
</HeaderTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I wanted to bind text to this header check boxes.
Hence My first attemt was to bind text to it on RowDataBound
event.
I made it as:
If e.Row.RowType = DataControlRowType.Header Then
For i As Integer = 0 To ds1.Tables(0).Columns.Count - 1
CType(e.Row.FindControl("chkSubjects'" & i + 1 & "'"), CheckBox).Text = DirectCast(ds1.Tables(0).Columns(i), System.Data.DataColumn).ColumnName
e.Row.Cells(i).Text = DirectCast(ds1.Tables(0).Columns(i), System.Data.DataColumn).ColumnName
Next
End If
But it was giving me error on CType(e.Row.FindControl("chkSubjects'" & i + 1 & "'"), CheckBox).Text
as Object Referace, and when i made quickwatch it was giving its value as Referance object has value of nothing
I written same code with BindGrd function calling it onload as:
Private Sub BindGrd()
For i As Integer = 0 To ds1.Tables(0).Columns.Count - 1
CType(gvSearch.HeaderRow.FindControl("chkSubjects'" & i + 1 & "'"), CheckBox).Text = DirectCast(ds1.Tables(0).Columns(i), System.Data.DataColumn).ColumnName
Next
End Sub
Here i added HeaderRow
as shown above, but this code was also giving same error.
Please help me.