[2008] DataGridView Control Binding with Select
When I run this:
DG1.DataSource = dtU
I get the result I expect.
However when I run this:
DG1.DataSource = dtU.Select("CaseNumber = 0")
I get the right number of rows, but the following columns (non existent in the database):
RowError - all rows blank
RowState - all rows "Unchanged"
Table - all rows blank
HasError - all rows have an unchecked checkbox
Is what I am trying to do possible? If so what am I doing wrong?
Re: [2008] DataGridView Control Binding with Select
First, you need to understand how the DataSource property works. It will accept either an IList or an IListSource object. If you assign an IList object to the DataSource then it will loop through the items and display them. If you assign an IListSource object then it will call the object's GetList method to get an IList, then it will do as for an IList.
As the name suggests, the IListSource interface exists to act as a source for an IList. The DataView class implements the IList interface, while the DataTable implements the IListSource interface. When you assign a DataTable to a DataSource property its GetList method is called, which returns its DefaultView, which is a DataView. It is the contents of that DataView that gets displayed in the grid. each item in a DataView is a DataRowView.
When you call Select on a DataTable it returns an array of DataRow objects. There's no DataView associated with that array. In fact, the Array class itself implements IList, so when you assign that array to the DataSource it will get the DataRows themselves and display them in the grid. The DataRow class has RowError, RowState, Table and HasError properties.
Rather than this:
vb.net Code:
DG1.DataSource = dtU.Select("CaseNumber = 0")
you should be doing this:
vb.net Code:
dtU.DefaultView.RowFilter = "CaseNumber = 0"
DG1.DataSource = dtU
First you filetr the DefaultView, then you bind the table as normal.
That said, you really should be using a BindingSource. You bind the DataTable to the BindingSource and the BindingSource to the grid. You would then set the Filter property of the BindingSource.