如何将多个列表合并并作为一个GridView数据源使用 [英] How to combine multiple lists and use as a GridView datasource

查看:125
本文介绍了如何将多个列表合并并作为一个GridView数据源使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ASP.net:

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

code-背后:

Code-behind:

lstName.Add(lstN[f]); //name
lstCMSID.Add(lstNum[f]); //number
lstSpecialtyPhys.Add(data.Text.ToString()); //value

我想这三个列表在上面的GridView结合起来,并显示它,就像这样:

I would like to combine the three List and display it in the above GridView, like this:

Name                        Number              Value
John Doe                    56                  90
James Coon                  34                  24

我怎样才能实现上述所以有三列,三个不同的标题文本。

How can I achieve the above so there are three columns with three different header text.

推荐答案

最简单的方法是做一个循环(一个用于,的foreach等)

The simple way would be to do a loop (a for, foreach, etc)

        List<dynamic> lstName = new List<dynamic>();
        List<dynamic> lstCMSID = new List<dynamic>();
        List<dynamic> lstSpecialtyPhys = new List<dynamic>();

        lstName.Add("John Doe");
        lstCMSID.Add("56");
        lstSpecialtyPhys.Add("90");

        lstName.Add("James Coon");
        lstCMSID.Add("34");
        lstSpecialtyPhys.Add("24");

        DataTable dt = new DataTable();
        dt.Columns.Add("Name");
        dt.Columns.Add("Number");
        dt.Columns.Add("Value");

        for (int i = 0; i < lstName.Count; i++)
        {
            dt.Rows.Add(lstName[i], lstCMSID[i], lstSpecialtyPhys[i]);
        }

        gvSP.DataSource = dt;
        gvSP.DataBind();

这篇关于如何将多个列表合并并作为一个GridView数据源使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆