简体   繁体   中英

C#: Datagridview not displaying the data

I am working on winforms application. 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. 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.

For more details following code represents the properties which I set using properties 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).

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

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

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. 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.

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:

  1. Go the Designer mode
  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

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

'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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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