简体   繁体   English

如何在数据网格视图中显示项目列表 c#

[英]how to show list of items into data grid view c#

I have a Windows Forms Application I am working on and am using the language C#.我有一个 Windows Forms 应用程序我正在处理并使用语言 C#。 I have a list of items that I want to put into a Data Grid View.我有一个要放入数据网格视图的项目列表。 Here is the code I tried:这是我尝试过的代码:

        List<double> m2 = T.ALLDATIX;
        var list = new BindingList<double>(m2);
        var source = new BindingSource(list, null);

        dataGridView1.DataSource = source ;

Did you try:你试过了吗:

var bindingSource = new BindingSource(){ DataSource = m2 };
dataGridView.DataSource = bindingSource;

See docs for reference . 请参阅文档以供参考

Can you try this?你能试试这个吗? I assume that the problem is, that the dataGridView does need objects (containing properties) to work properly.我认为问题在于 dataGridView 确实需要对象(包含属性)才能正常工作。

// Define a container class
class Container
{
   public double Data { get; set; }
} 

// convert list of double to BindingList of Container Objects
var list = T.ALLDATIX.Select(q => new Container { Data = q }).ToBindingList();

// Assign data
grid.DataSource = new BindingSource(list, null);

THIS LINE SOLVED THE PROBLEM这条线解决了问题

       this.dataGridView1.DataSource = m2.Select(k => new { Value = k }).ToList();

Probably you should use the datasource.可能您应该使用数据源。 Try something like this:尝试这样的事情:

dataGridView.DataSource = list.ToList();

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

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