Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET dynamically add column to Gridview

Tags:

c#

asp.net

how can I add to GridView dynamically some columns based on condition?

  <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>
like image 744
user3305953 Avatar asked Feb 13 '14 12:02

user3305953


People also ask

How to add row in GridView dynamically c#?

Add(new DataColumn("RowNumber", typeof(string))); dt. Columns. Add(new DataColumn("Column1", typeof(string)));//for TextBox value.


1 Answers

Try this:

BoundField test = new BoundField();
test.DataField = "New DATAfield Name";
test.Headertext = "New Header";
CustomersGridView.Columns.Add(test);
like image 196
spetzz Avatar answered Oct 08 '22 11:10

spetzz