简体   繁体   English

将列表绑定到gridview C#

[英]Bind list to gridview C#

I'm trying to bind list to gridview . 我正在尝试将列表绑定到gridview Situation is like this: I take data from .txt file, later I put it inside first list List<Mycolumns> . 情况是这样的:我从.txt文件中获取数据,之后我把它放在第一个列表List<Mycolumns> I have data in list (with 3 separated columns) that I created. 我创建了列表中的数据(有3个独立的列)。 I am taking data from one of the columns called System_Description . 我从一个名为System_Description的列中获取数据。 Now I would like to show this data in gridview , but only thing that I get is length of each row. 现在我想在gridview显示这些数据,但我得到的只是每行的长度。 How should I fix it? 我该如何解决? Here is my code. 这是我的代码。

private void button7_Click(object sender, EventArgs e)
    {
        List<MyColumns> list = new List<MyColumns>();

        OpenFileDialog openFile1 = new OpenFileDialog();
        openFile1.Multiselect = true;

        if (openFile1.ShowDialog() != DialogResult.Cancel)
        {
            foreach (string filename in openFile1.FileNames)
            {
                using (StreamReader sr = new StreamReader(filename))
                {
                    string line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        string[] _columns = line.Split(",".ToCharArray());
                        MyColumns mc = new MyColumns();
                        mc.Time = _columns[0];
                        mc.System_Description = _columns[1];
                        mc.User_Description = _columns[2];
                        list.Add(mc);
                    }
                }
            }
            DataTable ListAsDataTable = BuildDataTable<MyColumns>(list);
            DataView ListAsDataView = ListAsDataTable.DefaultView;
            this.dataGridView1.DataSource = view = ListAsDataView;
            this.dataGridView1.AllowUserToAddRows = false;
            dataGridView1.ClearSelection();
        }

        List<string> description = list.Select(x => x.System_Description).ToList<string>();
        this.dataGridView2.DataSource = description;

    }
class MyColumns
{
    public string Time { get; set; }
    public string System_Description { get; set; }
    public string User_Description { get; set; }
}

EDIT: 编辑:

I've read that DataBind() works for Web form, my app is desktop app. 我已经读过DataBind()适用于Web表单,我的应用程序是桌面应用程序。 What should I do now? 我现在应该怎么办?

this.dataGridView1.DataSource = new BindingSource(list);

I managed to solve this problem. 我设法解决了这个问题。 I did it this way, maybe it will help someone. 我是这样做的,也许它会帮助别人。 You can use DataTable and then bind DT to gridview. 您可以使用DataTable然后将DT绑定到gridview。

DataTable dt = new DataTable();
dt.Columns.Add("values");

foreach(string items in description)
{
    DataRow row = dt.NewRow();
    dt.Rows.Add(items);
}
this.dataGridView2.DataSource = dt;

You are only selecting the System_Description field using the following statement: 您只使用以下语句选择System_Description字段:

List<string> description = list.Select(x => x.System_Description).ToList<string>();

Then you are binding this list to the gridview: 然后,您将此列表绑定到gridview:

this.dataGridView2.DataSource = description;

If you want to bind the whole data to the gridview ; 如果要将整个数据绑定到gridview ; just bind the list as a datasource. 只需将列表绑定为数据源。

this.dataGridView2.DataSource = list;
this.dataGridView2.DataBind();

Use this 用这个

 this.dataGridView1.DataSource = description;

and add Bound field in gridview on aspx. 并在aspx上的gridview中添加Bound字段。

<asp:BoundField DataField="System_Description" HeaderText="System_Description"></asp:BoundField>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM