I have a data table with 9 columns in it. I want to display 3 columns of my selected choice by giving the column name in datagrid view 1 and other in data grid view 2. Is there any way to do this?
Asked
Active
Viewed 108 times
0
-
2how do you want to select columns ? By user input or hard coded ? – SpiderCode Dec 06 '13 at 12:13
-
i have this filled datatable with 9 columns i want to display for e.g person name ,address,phone number in gridview 1 and others in 2. – user1056466 Dec 06 '13 at 12:17
-
1ok. is it your web application or window application ? – SpiderCode Dec 06 '13 at 12:20
-
possible duplicate of [How to hide a column (GridView) but still access its value?](http://stackoverflow.com/questions/5376278/how-to-hide-a-column-gridview-but-still-access-its-value) – RajeshKdev Dec 06 '13 at 12:24
2 Answers
2
Solution:
DataView view = new DataView(dt1);
DataTable dt2 = view.ToTable(true, "FirstColumn", "SecondColumn", "ThirdColumn");
Regard
Ali Muhammad

Ali
- 86
- 7
-
-
Are you sure there is any overload accepting those arguments? I've just checked it and there is not any matched overload. – King King Dec 06 '13 at 12:30
-
DataView view = new DataView(dt1); DataTable dt2 = view.ToTable(true, "FirstColumn", "SecondColumn", "ThirdColumn"); – Ali Dec 06 '13 at 12:33
-
But **it's different** in your code, can't you see that? You should update in your code. – King King Dec 06 '13 at 12:38
2
DataGridView has methods with which you can set visibility of the columns. Bind the gridviews to same datasource and hide the columns you want in respective grids:
dataGridView1.Columns[0].Visible = false;
or
dataGridView1.Columns["ColumnName"].Visible = false;
You can try something like this:
int[] arr1 = new int[] { 0, 2 };
foreach (int ColIndex in arr1)
dataGridView1.Columns[ColIndex].Visible = false;
int[] arr2 = new int[] { 1, 3 };
foreach (int ColIndex in arr2)
dataGridView2.Columns[ColIndex].Visible = false;

Sadique
- 22,572
- 7
- 65
- 91
-
Second would be better. I would suggest that first. `dataGridView1.Columns["ColumnName"].Visible = false;` :) – RajeshKdev Dec 06 '13 at 12:23
-
1