C# 在gridview中动态添加列

C# 在gridview中动态添加列,c#,asp.net,gridview,C#,Asp.net,Gridview,我有一个gridview,其中列是动态绑定的。列计数可能是7、12或17。这取决于用户选择。我希望在rowdatabound方法中添加下面的列。第一个问题是我无法在rowdatabound中设置列名,第二个问题是列计数为0,第三个问题是不可见的列无法访问。如果有人知道如何通过html表实现相同的功能。如果有人能抽出时间来帮助我,请 <Columns> <asp:BoundField HeaderText="Services" DataField="Services"

我有一个gridview,其中列是动态绑定的。列计数可能是7、12或17。这取决于用户选择。我希望在rowdatabound方法中添加下面的列。第一个问题是我无法在rowdatabound中设置列名,第二个问题是列计数为0,第三个问题是不可见的列无法访问。如果有人知道如何通过html表实现相同的功能。如果有人能抽出时间来帮助我,请

<Columns>
    <asp:BoundField HeaderText="Services"  DataField="Services" HeaderStyle-BackColor="#089de3" ItemStyle-Width="12%"> </asp:BoundField>
    <asp:BoundField HeaderText="Category"  DataField="Category" HeaderStyle-BackColor="#089de3" ItemStyle-Width="12%" > </asp:BoundField>
    <asp:CheckBoxField HeaderText="ALTA"  DataField="1_ALTA" HeaderStyle-BackColor="#e34e08" ItemStyle-Width="2%"   />
    <asp:CheckBoxField HeaderText="HPLAN"  DataField="1_HPLAN" HeaderStyle-BackColor="#e34e08" ItemStyle-Width="2%" />
    <asp:CheckBoxField  HeaderText="HOSP"  DataField="1_HOSP" HeaderStyle-BackColor="#e34e08" ItemStyle-Width="2%"/>
    <asp:CheckBoxField HeaderText="OTHER" DataField="1_OTHER"  HeaderStyle-BackColor="#e34e08" ItemStyle-Width="2%"/>
    <asp:BoundField HeaderText="OTHERSCOLOR" Visible="false" DataField="1_OthersColor" HeaderStyle-BackColor="#e34e08" ItemStyle-Width="2%"/>

    <asp:CheckBoxField HeaderText="ALTA"  DataField="2_ALTA" HeaderStyle-BackColor="#089de3" ItemStyle-Width="2%" />
    <asp:CheckBoxField HeaderText="HPLAN" DataField="2_HPLAN"  HeaderStyle-BackColor="#089de3" ItemStyle-Width="2%" />
    <asp:CheckBoxField HeaderText="HOSP" DataField="2_HOSP"  HeaderStyle-BackColor="#089de3" ItemStyle-Width="2%" />
    <asp:CheckBoxField HeaderText="OTHER" DataField="2_OTHER"  HeaderStyle-BackColor="#089de3" ItemStyle-Width="2%"/>
    <asp:BoundField HeaderText="OTHERSCOLOR" DataField="2_OthersColor" Visible="false" HeaderStyle-BackColor="#089de3" ItemStyle-Width="2%"/>

    <asp:CheckBoxField HeaderText="ALTA" DataField="3_ALTA"  HeaderStyle-BackColor="#e34e08" ItemStyle-Width="2%"  />
    <asp:CheckBoxField HeaderText="HPLAN" DataField="3_HPLAN"  HeaderStyle-BackColor="#e34e08" ItemStyle-Width="2%" />
    <asp:CheckBoxField HeaderText="HOSP" DataField="3_HOSP"  HeaderStyle-BackColor="#e34e08" ItemStyle-Width="2%" />
    <asp:CheckBoxField HeaderText="OTHER" DataField="3_OTHER"  HeaderStyle-BackColor="#e34e08" ItemStyle-Width="2%"/>
    <asp:BoundField HeaderText="OTHERSCOLOR" DataField="3_OthersColor" Visible="false" HeaderStyle-BackColor="#e34e08" ItemStyle-Width="2%"/>

</Columns>


共享用户选择的代码以及您尝试添加新列的位置。根据您需要重新考虑解决方案的字段列表,GridView可能不是您的最佳选择。Gridview非常适合显示和编辑现有数据。它不太适合数据插入。不太适合动态列创建和数据收集。有很多重复。不知道您的需求,只看到您列出的内容,这5个字段可以存在于单独的FormView中,可以根据您的用户需求重复收集数据。