I have these fields on Customer DataTable: ID,title,Name,Addrs,email,Fax and this code to bind the DataGridView:
So if you don't want to modify your query string (as @Neolisk noticed this is generally a bad practice to use Select * but this is another debat), and so you get more columns than what you want to display:
Solution1 (Ideal if there are a lot of columns in datatable and you want to display just some of them)
You need to set AutoGenerateColumns property to false. Default is True, so DataGridView will create a column for all columns in the datatable.
Then, you add a DatagridiviewColumn for each column you want to display.
To avoid to have to add a celltemplate for the DatagriviewColumn (see this) you would prefer to add a strongly typed column (DataGridViewTextBoxColumn for example in order to display String values). In order to bind the column with the source, you set the DataPropertyName property, that needs to match with the ColumnName of the column inDataTable.
So code would be:
dgvheader.AutoGenerateColumns = False
dgvHeader.Columns.Add(New DataGridViewTextBoxColumn() With {.HeaderText = "Title", .DataPropertyName = "title"})
dgvHeader.Columns.Add(New DataGridViewTextBoxColumn() With {.HeaderText = "Name", .DataPropertyName = "Name"})
dgvHeader.Columns.Add(New DataGridViewTextBoxColumn() With {.HeaderText = "Adresse", .DataPropertyName = "Addrs"})
dgvHeader.DataSource = dsNota.Tables("Customers")
Solution2 (Ideal if there are a lot of columns in datatable, and you want to Hide just some of them, and so you want to keep the benefit of AutoGenerateColumns)
Set AutoGenerateColumns property to true (or do nothing since default is True)
Hook the DataGridView.DataBindingComplete Event in order to hide some columns autogenerated:
Private Sub dataGridView1_DataBindingComplete(ByVal sender As Object, ByVal e As DataGridViewBindingCompleteEventArgs) Handles dgvHeader.DataBindingComplete
With dgvHeader
.Columns("Fax").Visible = False
End With
End Sub
I know this post goes way back, but I had the same problem in C# and this is how I solved it which works really well.
1 - Build a DataSet with your SQL query
private void LoadDS()
{
// this method gets data from my database
// DS is a DataSet in the properties of my form
DS = LoadData();
}
2 - Get the DataView filtered the way you want
For this I use the RowFilter property of the DataView. I created a GetFiltersToString() method that formats every filter control I have in a string that matches the RowFilter syntax (more information about the syntax here, and msdn definition of RowFilter here)
public void RefreshDGV()
{
// Get the corresponding dataview
DV = new DataView(DS.Tables[0], rowFilter, "SORTINGCOLUMN Desc", DataViewRowState.CurrentRows);
// Display it in a datagridview
DGV.DataSource = DV;
}
I find that this solution allows user to change the filtering much more easily.
You have to explicitly add the columns to your grid.
If the gridview autogeneratecolumns attribute is set to true change it to false and then do as Raimond suggested. Example:
<asp:GridView ID="gvSearchResults" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="title" HeaderText="Title" />
</Columns>
</asp:GridView>