Home » VB.NetRSS

Hiding Columns in a DataGridView using BindingSource

Hi,

I bind a dataTable to a data grid view by using a binding Source as follows:

adapterEmployees =

 

newSqlDataAdapter("select * from Employee", con);

commandBuilder=

 

newSqlCommandBuilder(adapterEmployees);

adapterEmployees.Fill(dtEmployees);

bSourceEmployees=

 

newBindingSource();

bSourceEmployees.DataSource=dtEmployees;

dgvEmployees.DataSource=bSourceEmployees;

I'd like to hide some of the columns. Retrieving the specific wanted-to-be-shown columns, won't be good enough for my case, as I need the dataSource to know all the columns.

 

Thanks in advance

 

 

2 Answers Found

 

Answer 1

Hello,

The columns  in a DataGridView have a visible property.  You could iterate over the columns of the dgv and only set certain columns visible. Ex.

foreach(DataGridViewColumn column in dgvEmployees.Columns)
{
  if(SOME_CONDITIONAL)
    column.Visible = true;
  else
    column.Visible = false;
}

 

Answer 2

Turn off auto-generate columns  and define them yourself. Then you can just define the columns you need, regardless of which columns are in the datasource.

Just be sure to set the DataPropertyName for each column to the field in the bound dataset that you want to bind  to.

Hope this helps.

 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter