我正在尝试将列表绑定到gridview
. 情况是这样的:我从 .txt 文件中获取数据,后来我把它放在第一个列表中List<Mycolumns>
。我在创建的列表中有数据(有 3 个单独的列)。我正在从名为的列之一中获取数据System_Description
。现在我想在 中显示这些数据gridview
,但我得到的只是每行的长度。我应该如何解决它?这是我的代码。
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; }
}
编辑:
我已经阅读了DataBind()
适用于 Web 表单的内容,我的应用程序是桌面应用程序。我现在该怎么办?