0

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?

user1056466
  • 597
  • 1
  • 7
  • 17
  • 2
    how 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
  • 1
    ok. 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 Answers2

2

Solution:

   DataView view = new DataView(dt1);
   DataTable dt2 = view.ToTable(true, "FirstColumn", "SecondColumn", "ThirdColumn");

Regard
Ali Muhammad

Ali
  • 86
  • 7
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