c# – ASP.NET动态地将列添加到Gridview

前端之家收集整理的这篇文章主要介绍了c# – ASP.NET动态地将列添加到Gridview前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何根据条件动态添加一些列的GridView?
<asp:gridview id="CustomersGridView" 
    datasourceid="CustomeRSSource" 
    autogeneratecolumns="true"
    emptydatatext="No data available." 
    runat="server">

    <columns>
      <asp:boundfield datafield="CustomerID" headertext="Customer ID"/>
      <asp:boundfield datafield="CompanyName" headertext="Company Name"/>
      <asp:boundfield datafield="Address" headertext="Address"/>
      <asp:boundfield datafield="City" headertext="City"/>
      <asp:boundfield datafield="PostalCode" headertext="Postal Code"/>
      <asp:boundfield datafield="Country" headertext="Country"/>
    </columns>
    for(int i; i < length; i++)
      <asp:boundfield datafield="text" headertext="text"/>
  </asp:gridview>

解决方法

试试这个:
BoundField test = new BoundField();
test.DataField = "New DATAfield Name";
test.Headertext = "New Header";
CustomersGridView.Columns.Add(test);

猜你在找的C#相关文章