I have a gridview with fixed number of columns in it, but i want to make the columns dynamic that is number of columns should vary based on the selected option`
<asp:GridView ID="grid_additional_test" CssClass="table table-condensed table-striped hover table-bordered static pull-left table-hover"
runat="server" AutoGenerateColumns="False" Width="200px" OnRowDataBound="grid_additional_test_RowDataBound">
<Columns>
<asp:BoundField HeaderText="Test Code" DataField="Test_Code">
<HeaderStyle HorizontalAlign="Center" Width="20%" />
<ItemStyle Width="15%" CssClass="colL fix_gv_clm" HorizontalAlign="Left" />
<HeaderStyle CssClass="fix_gv_clm" />
</asp:BoundField>
</Columns>
<HeaderStyle BackColor="#F2F2F2" />
</asp:GridView>
` Here's my code behind to bind the data:
grid_additional_test.DataSource = ds.Tables[0];
grid_additional_test.DataBind();
grid_additional_test.Visible = true;
Please share your insights on it.
Check this.
for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
{
BoundField bf = new BoundField();
bf.DataField = ds.Tables[0].Columns[i].ColumnName;
bf.HeaderText = ds.Tables[0].Columns[i].ColumnName;
grid_additional_test.Columns.Add(bf);
}
grid_additional_test.DataSource = ds.Tables[0].DefaultView;
grid_additional_test.DataBind();
Make sure you remove all the columns you added to GridView through markup.
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.