How to bind a DataTable selected columns to a DataGridView in Windows Forms
-
Hi, I want to bind only one column, from the collection of columns of the DataTable table, to a DataGidView.I tried with this code for (int c = 0; c <= i; c++) { dataGridView1.DataSource = dt1.Rows[i,1].ToString(); dataGridView1.Refresh(); } but Nothing is displayed in dataGridView1. Please reply me if anyone know the solution.
-
Hi, I want to bind only one column, from the collection of columns of the DataTable table, to a DataGidView.I tried with this code for (int c = 0; c <= i; c++) { dataGridView1.DataSource = dt1.Rows[i,1].ToString(); dataGridView1.Refresh(); } but Nothing is displayed in dataGridView1. Please reply me if anyone know the solution.
You don't loop through the rows. Instead you define a datasource for the DataGridView, create a column in it and define the column from the datatable to show. For example something like this:
DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();
column.DataPropertyName = "ColumnInDataTable";
column.HeaderText = "Header";
column.Name = "somename";
//
this.dataGridView1.Columns.Add(column);
this.dataGridView1.DataSource = dt1;Of course you are aware that you can do this also in the designer.
The need to optimize rises from a bad design.My articles[^]
-
Hi, I want to bind only one column, from the collection of columns of the DataTable table, to a DataGidView.I tried with this code for (int c = 0; c <= i; c++) { dataGridView1.DataSource = dt1.Rows[i,1].ToString(); dataGridView1.Refresh(); } but Nothing is displayed in dataGridView1. Please reply me if anyone know the solution.
Hi....... U have lots of options to do this.... First One: By Using BindingSource:::>> dataGridView1:>properties:>Choose Datasource:>{If u have already one then select it otherwise select Add Project Datasource} If u cann't follow this Just message me!!!!!! Second One: fill dataset according to ur needs.... DataView dv = new DataView(dt.Tables["Specify table name here"]); dataGridView1.DataSource = dv; dataGridView1.Refresh(); {Remember Donn't Add Columns to DataGridView } try this...... and reply me....