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>
Try this:
BoundField test = new BoundField();
test.DataField = "New DATAfield Name";
test.Headertext = "New Header";
CustomersGridView.Columns.Add(test);
First set the property of your Gridview autogeneratedcolumns=false;
then add following code to server side:
BoundField newColumnName= new BoundField();
newColumnName.DataField = "New DATAfield Name";
newColumnName.HeaderText = "New Header";
yourGridView.Columns.Add(newColumnName);
Note-Here NewColumnName is ur Dynamic Column that u want to add and MyGridView is ur gridview id
Really good article about this with examples in C# and VB.Net. It even shows how to add TemplateField dynamically.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.