DataGridView and DataBinding with custom columns
My requirement is as follows, I am calling a procedure and I'm populating a DataTable
. As开发者_如何学JAVAsume 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.
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.
You can quite easily select and customize the columns on the grid.
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.
精彩评论