George Trevour Dsouza
George Trevour Dsouza

Reputation: 171

DataGridView and DataBinding with custom columns

My requirement is as follows, I am calling a procedure and I'm populating a DataTable. Assume that the procedure is returning emp_id, emp_name, designation and status. When I use DataGridView1.DataSource = <Name of the DataTable>, I get all the columns populated into the DataGridView. How can I display only selected columns in the DataGridView. For e.g, I want to show only emp_id and emp_name.

Similarly, can you please help me as to how I can increase the width of the columns of the DataGridView by writing code.

Upvotes: 1

Views: 7310

Answers (3)

Bobby
Bobby

Reputation: 11576

You can set the AutoGenerateColumns-Property of the Grid to False and add the columns you want by hand. Don't forget that you need to set the DataPropertyName of each Grid-Column to the name of the DataTable-Column.

Edit: If you want to change the width of the columns, simply set the 'Width' or the 'FillMode'-Property.

Upvotes: 2

T3hc13h
T3hc13h

Reputation: 602

The opposite of Bobby's method is to allow the DataGridView autogenerate the columns and then hide your columns by using dataGrid.Columns("designation").Visible = False.. One of the advantages of this method is that you don't have to hardcode which columns your hiding.

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

You can quite easily select and customize the columns on the grid.

Upvotes: -1

Related Questions