简体   繁体   English

C#:Datagridview 不显示数据

[英]C#: Datagridview not displaying the data

I am working on winforms application.我正在研究 winforms 应用程序。 On my form I simply drag and drop a DataGridView control and then set some of its properties using the properties window. Following is the code which I am using to populate my DataGridView.在我的窗体上,我只需拖放一个 DataGridView 控件,然后使用属性 window 设置它的一些属性。以下是我用来填充 DataGridView 的代码。 I wrote this code inside the constructor.我在构造函数中编写了这段代码。

List<MyCustomClass> lst = new List<MyCustomClass>();
lst = LoadList(/*some params here*/);//now uptil this point everything works i.e the list contains values as desribed.
dataGridView1.DataSource = lst;

The problem is that when i run the program nothing is displayed in my DataGridView.问题是当我运行该程序时,我的 DataGridView 中没有显示任何内容。

For more details following code represents the properties which I set using properties window有关详细信息,以下代码表示我使用属性 window 设置的属性

        this.dataGridView1.AllowUserToAddRows = false;
        this.dataGridView1.AllowUserToDeleteRows = false;
        this.dataGridView1.AllowUserToResizeRows = false;
        this.dataGridView1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                    | System.Windows.Forms.AnchorStyles.Right)));
        this.dataGridView1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
        this.dataGridView1.BackgroundColor = System.Drawing.Color.White;
        this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        this.dataGridView1.GridColor = System.Drawing.Color.White;
        this.dataGridView1.Location = new System.Drawing.Point(2, 329);
        this.dataGridView1.Margin = new System.Windows.Forms.Padding(2);
        this.dataGridView1.MultiSelect = false;
        this.dataGridView1.Name = "dataGridView1";
        this.dataGridView1.RowHeadersVisible = false;
        this.dataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
        this.dataGridView1.Size = new System.Drawing.Size(334, 106);
        this.dataGridView1.TabIndex = 0;

You have to create Data Columns for your datagrid.您必须为您的数据网格创建数据列。 Make sure you also set 'DataPropertyName' property of each column with the respected DataSource item's property (ie, property of 'MyCustomClass' class).确保您还使用受尊重的 DataSource 项的属性(即“MyCustomClass”类的属性)设置了每列的“DataPropertyName”属性。

I've got nearly the same thing today, the problem was that my clas had public fields, not properties, since i've changed them to public auto properties - worked well for me.我今天有几乎相同的东西,问题是我的班级有公共领域,而不是属性,因为我已经将它们更改为公共汽车属性 - 对我来说效果很好。

Add this code before assigning the DataSource and you should be fine在分配 DataSource 之前添加此代码,您应该没问题

dataGridView1.AutoGenerateColumns = true;

EDIT编辑

Also check if you have public properties which would be used to display the contents of the Class as columns in the DataGridView还要检查您是否有公共属性,这些属性将用于将 Class 的内容显示为 DataGridView 中的列

When I'm binding in designer have the same problem.当我在设计器中绑定时有同样的问题。 But if I'm binding in constructor it works well但是如果我在构造函数中绑定它会很好

dataGridView1.DataSource = personBindingSource;
personBindingSource.DataSource = persons;

I was facing the exact similar issue.我面临着完全相似的问题。 This usually happens when your custom class has private properties due to which the Data Grid View is unable to view them.这通常发生在您的自定义 class 具有私有属性时,数据网格视图因此无法查看它们。 You will need to create properties on your custom class fields and make them public so that when you set your data source from the custom class list, the data is visible to the Data Grid View and the data can flow through.您将需要在自定义 class 字段上创建属性并将其公开,以便当您从自定义 class 列表设置数据源时,数据对数据网格视图可见并且数据可以流过。

In my case, I had no getters to properties!就我而言,我没有获取属性的方法!

I am on VS2022 w/ VB.Net 7. My situation was the number of rows matches the datasource, but nothing is displayed: Here's my solution:我在 VS2022 w/VB.Net 7 上。我的情况是行数与数据源匹配,但没有显示任何内容:这是我的解决方案:

  1. Go the Designer mode Go 设计师模式
  2. Click on the Edit Columns (in the Property panel)单击编辑列(在属性面板中)
  3. For each of the column, type in the column name in your data source in the DataPropertyName对于每一列,在DataPropertyName中输入数据源中的列名

Now, if you are like me, you have a ComboList is the generated during the program runtime, you will need to do this to assign the DataSource for your combo现在,如果你像我一样,你有一个 ComboList 是在程序运行时生成的,你需要这样做来为你的组合分配 DataSource

'mstrCellList has been populated
'the bottomline is you have to convert to the ComboBoxColumn in order to 
'access the DataSource property
CType(vsFlexRouteStep.Columns(Cell), DataGridViewComboBoxColumn).DataSource _
  = mstrCellList

Good luck!祝你好运!

Put this at the end:把这个放在最后:

datagridview1.Databind();

This should do.这应该做。

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

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