无法使用WinForm应用程序中的BindingList将数据绑定到DataGridView

时间:2015-06-16 20:57:50

标签: c# excel winforms datagridview

我正在从excel文件(.csv)中读取数据并将该数据存储到List<String>。我想将此数据显示给DataGridView,但由于某种原因,我看到length of String中的Grid总数。

以下是我的代码:

CODE:

private void btnImportFile_Click(object sender, EventArgs e)
{
    OpenFileDialog dialog = new OpenFileDialog();
    dialog.Filter = "CSV (Comma delimited) (*.csv)|*.csv";
    dialog.FilterIndex = 1;
    dialog.Multiselect = false;

    var userClickedOK = dialog.ShowDialog();

    if (userClickedOK.ToString().Equals("OK"))
    {
        this.lblImportedFileName.Text = dialog.SafeFileName;

        System.IO.Stream fileStream = dialog.OpenFile();

        using (System.IO.StreamReader reader = new System.IO.StreamReader(fileStream))
        {
            var list = new List<String>();
            var line = String.Empty;

            while ((line = reader.ReadLine()) != null) //Reading value and adding it to the list.
            {
                list.Add(line);

            }

            BindingList<String> view = new BindingList<String>(list);
            this.gridExcelResults.DataSource = view; // Binding list to grid

            //Also tried
            this.gridExcelResults.DataSource = list; // This also gives me same result.

        }
    }
}

输出:

enter image description here

EXCEL DATA:

enter image description here

预期结果:

我只想在Grid上显示这三个字段。我在这里做错了什么?

1 个答案:

答案 0 :(得分:3)

看到这个问题: How to bind a List<string> to a DataGridView control?

在您的情况下,您使用的是BindingList<string>,但逻辑是相同的。 DataGridView正在寻找要绑定的对象属性,String但是,只有属性长度。