Reputation: 8646
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.
Upvotes: 0
Views: 2130
Reputation: 4596
After binding the data source just below try to rename the columns like
gv.HeaderRow.Cells(0).Text = ds.tables(1).columns(0).columnName;
gv.HeaderRow.Cells(2).Text = ds.tables(1).columns(1).columnName;
also handle the rows count value > 0
some thing like above
Upvotes: 1