Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
413 views
in Technique[技术] by (71.8m points)

c# - How to combine multiple lists and use as a GridView datasource

ASP.net:

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

Code-behind:

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...