Missy Missy - 14 days ago 28
C# Question

ArrayList as a Source of DataGridView Not Showing Data

I have a DataGridView that has a view as it's data source (SQL Server). No matter what I do, I cannot make that view read/write so I'm trying to work around it. I need to make one column editable so I thought I could make an ArrayList and set that as the data source. Now I get no data in my DGV although my ArrayList does contain 35 rows and 4 columns of data. Here is my code:

// get the data from the Table adapter, put it into an ArrayList then display it in the DataGridView -- called from Load()

private void FillReportDistributionTX()
{
DataTable dt = this.dLV_ReportDistributionTableAdapter.GetData();
DGVlist = ConvertDTtoArray(ref dt);
dLV_UserReportsDataGridView.DataSource = DGVlist;
dLV_UserReportsDataGridView.Refresh();
}

// convert to Array
public ArrayList ConvertDTtoArray(ref DataTable dt)
{
ArrayList converted = new ArrayList(dt.Rows.Count);
foreach (DataRow row in dt.Rows)
{
converted.Add(row);
}

return converted;

}


This has got me totally stumped. Any help or advice would be greatly appreciated.

Answer

When you set an array list which contains some DataRow as DataSource of a DataGridView you will see these columns:

RowError | RowState | Table | HasErrors

That's because the DataGridView will show browsable properties of DataRow.

But why for the same rows in DataTable it shows columns as expected?

It's because the DataView of the DataTable which is used for data binding, implement ITypedList interface which provides functionality to discover the schema for a bindable list, where the properties available for binding differ from the public properties of the object to bind to. In fact it returns a PropertyDescriptorCollection containing some property descriptors which are created based on Columns of DataTable.