Table with dynamic number of columns to show

2019-09-03 07:37发布

问题:

I have a data table with data like "insurance name, plantype, premium...." for each row.

So on my front end I have to show like below:

Insurance Name     HealthNet       Harvard         UniCare

Plan Type            HMO            PPO              HMO  

Premium              100            150             200

So sometimes I may have only two columns to show HealthNet and Harvard. Sometimes more than three. How to use repeater in this case to make it dynamic based on data table count?

Thanks

回答1:

Sample:

aspx markup as shown below:

<asp:GridView ID="gv" runat="server" AutoGenerateColumns="true">
</asp:GridView>

In codebehind simply do this:

gv.DataSource = yourDataTable;   // doesn't matter how many columns are there, it will handle it automatically
gv.DataBind();


回答2:

This will help how to pivot a data table:

http://geekswithblogs.net/dotNETvinz/archive/2009/05/10/pivot-data-in-gridview---a-generic-pivot-method-with.aspx



标签: c# repeater