The DataGridView is incredibly helpful for binding data sources to a grid, but sometimes working with it is a bit tricky. If you use the DataGridView control sparingly (or you’re just plain forgetful), you might run into an issue where you can see rows in the grid, but all the cells are empty!
The Problem
Not to worry! If you’re seeing the correct number of rows, you probably just have some visibility problems with the class being used.
In my case, I was trying to bind a list of proxies (List) like this:
private void BindProxyGrid(IList proxies)
{
var proxiesBindingList = new BindingList(proxies);
dgvProxies.DataSource = proxiesBindingList;
}
And my proxy class looked something like this:
public class Proxy
{
public string Ip;
public int Port;
}
The Solution
So why didn’t it work? Because the DataGridView requires properties and I was using public fields. Updating the class to use properties fixed the problem!
public class Proxy
{
public string Ip { get; set; }
public int Port { get; set; }
}
It works!
The Lesson Learned
If you’re using a BindingList with your DataGridView, you may need to adjust the visibility for any class members you want to display in the grid; for example, the DataGridView can’t access public fields or properties marked internal.
Happy coding!