I have 10 columns in table, but i want to display only 4 columns in datagrid in c#, how can i do it?

545 Views Asked by At

I can't found solution... i have set 4 columns in DataGrid view but when i am using SqlDatAdapter and DataTable it displaying all columns .

3

There are 3 best solutions below

0
On

This happens when the AutoGenerateColumns property is set to true (which is the default value). To turn it off, set AutoGenerateColumns to false.

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.autogeneratecolumns.aspx

0
On

What Babak Naffas said and you need to define your columns of the items that you want to show instead...

see: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datagrid.aspx

There is an example toward the end of the article that show how to do this.

0
On

First, set GridView's property AutoGenerateColumns=false. During database connectivity, select only the four columns that you are needed and fill the DataAdapter with the DataTable. Then it will work.