0

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.

4

1 回答 1

1

绑定下面的数据源后,尝试重命名列

gv.HeaderRow.Cells(0).Text = ds.tables(1).columns(0).columnName;
gv.HeaderRow.Cells(2).Text = ds.tables(1).columns(1).columnName;

还处理行计数值 > 0

像上面的一些东西

于 2013-07-30T09:44:52.447 回答