I have some code in my C# application which extracts data from a SQL database.
This DataTable
is used in various places throughout my application, and is displayed in multiple DataGridViews
.
However, I want to show only some of the columns in the Datatable
, not all of them.
Also the columns I want to display are different in each of the DataGridViews
.
e.g.
- Show Columns 1-3 in DataGridView1
- Show Columns 4-6 in DataGridView2
- Show Columns 7-12 in DataGridView3
etc
I could of course just write different code to extract the required fields from SQL for each of the DataGridViews
, but this doesn't seem like a very concise solution.
It would be better (I think) if I could use the one DataTable
extracted from SQL, and apply multiple filters to it in the application for displaying different Columns in each of the DataGridViews
.
I looked at using DataView
but whilst this has a Sort
method, it doesn't seem to have a Filter
method
e.g.
DataTable table = GetDateFromSql();
DataView view = new DataView(table);
view.Sort = "FieldName";
Ideally I'd like to be able to do something like -
view.Filter = "SELECT cola, colB, colC";
myDataGridView.DataSource = view;
I know there is a RowFilter
method, but in effect, its the Columns I want to filter.
How can this be done?