How do I get a datagrid to only show certain columns from a dataset?
-
I have the following code:
SqlConnection con = new SqlConnection("connection details"); con.Open(); string strSql = "select \* from Names"; SqlDataAdapter dadapter = new SqlDataAdapter(); dadapter.SelectCommand = new SqlCommand(strSql, con); DataSet dset = new DataSet(); dadapter.Fill(dset,"Names"); con.Close(); dataGridView1.DataSource = dset.Tables\["Names"\];
The dset looks as follows: ID Name 1 Terence 2 Richard 1. How can I get my datagrid to only show the Name and not the ID column? 2. Also if I have a column named First Name in my datafrid how can I get name to show up in that column and not in a new one or how can I assign custom names to my datagrid? Any help would be great thanks!
1. modify
string strSql = "select * from Names";
2. read http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcolumn.headertext.aspx[^] :)Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use < PRE > tags for code snippets, it preserves indentation, and improves readability.
-
I have the following code:
SqlConnection con = new SqlConnection("connection details"); con.Open(); string strSql = "select \* from Names"; SqlDataAdapter dadapter = new SqlDataAdapter(); dadapter.SelectCommand = new SqlCommand(strSql, con); DataSet dset = new DataSet(); dadapter.Fill(dset,"Names"); con.Close(); dataGridView1.DataSource = dset.Tables\["Names"\];
The dset looks as follows: ID Name 1 Terence 2 Richard 1. How can I get my datagrid to only show the Name and not the ID column? 2. Also if I have a column named First Name in my datafrid how can I get name to show up in that column and not in a new one or how can I assign custom names to my datagrid? Any help would be great thanks!
Terence van Schalkwyk wrote:
1. How can I get my datagrid to only show the Name and not the ID column?
Like this:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" >
<Columns>
<asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" ReadOnly="True" SortExpression="EmployeeID" />
<asp:TemplateField HeaderText="EmployeeID" SortExpression="EmployeeID" Visible="false">
</asp:TemplateField>Terence van Schalkwyk wrote:
2. Also if I have a column named First Name in my datafrid how can I get name to show up in that column and not in a new one or how can I assign custom names to my datagrid?
Similar logic as above.
Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.
-
I have the following code:
SqlConnection con = new SqlConnection("connection details"); con.Open(); string strSql = "select \* from Names"; SqlDataAdapter dadapter = new SqlDataAdapter(); dadapter.SelectCommand = new SqlCommand(strSql, con); DataSet dset = new DataSet(); dadapter.Fill(dset,"Names"); con.Close(); dataGridView1.DataSource = dset.Tables\["Names"\];
The dset looks as follows: ID Name 1 Terence 2 Richard 1. How can I get my datagrid to only show the Name and not the ID column? 2. Also if I have a column named First Name in my datafrid how can I get name to show up in that column and not in a new one or how can I assign custom names to my datagrid? Any help would be great thanks!
Terence van Schalkwyk wrote:
string strSql = "select * from Names";
1. You could change your query to fetch only the name.
Terence van Schalkwyk wrote:
dataGridView1
2. You could hide the first column inside the datagrid by using the visible property of the column.
dataGridView1.Columns(0).Visible = False
Quidquid latine dictum sit, altum videtur.
Whatever is said in Latin sounds profound. -
1. modify
string strSql = "select * from Names";
2. read http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcolumn.headertext.aspx[^] :)Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use < PRE > tags for code snippets, it preserves indentation, and improves readability.
Thanks for the info :) , I need the * as I need the ID for other things. I just dont want the ID to appear in the datagrid...
-
Terence van Schalkwyk wrote:
1. How can I get my datagrid to only show the Name and not the ID column?
Like this:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" >
<Columns>
<asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" ReadOnly="True" SortExpression="EmployeeID" />
<asp:TemplateField HeaderText="EmployeeID" SortExpression="EmployeeID" Visible="false">
</asp:TemplateField>Terence van Schalkwyk wrote:
2. Also if I have a column named First Name in my datafrid how can I get name to show up in that column and not in a new one or how can I assign custom names to my datagrid?
Similar logic as above.
Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.
I am doing it in C# windows application not in asp. Any idea how to do it?
-
Terence van Schalkwyk wrote:
string strSql = "select * from Names";
1. You could change your query to fetch only the name.
Terence van Schalkwyk wrote:
dataGridView1
2. You could hide the first column inside the datagrid by using the visible property of the column.
dataGridView1.Columns(0).Visible = False
Quidquid latine dictum sit, altum videtur.
Whatever is said in Latin sounds profound.Abhinav S wrote:
1. You could change your query to fetch only the name.
Not an option as I might need the id elsewhere
Abhinav S wrote:
2. You could hide the first column inside the datagrid by using the visible property of the column. dataGridView1.Columns(0).Visible = False
Worked perfectly by doing the following: dataGridView1.Columns[0].Visible = false; instead of () needed to use []. Thanks. Now just to name my Columns!
-
I have the following code:
SqlConnection con = new SqlConnection("connection details"); con.Open(); string strSql = "select \* from Names"; SqlDataAdapter dadapter = new SqlDataAdapter(); dadapter.SelectCommand = new SqlCommand(strSql, con); DataSet dset = new DataSet(); dadapter.Fill(dset,"Names"); con.Close(); dataGridView1.DataSource = dset.Tables\["Names"\];
The dset looks as follows: ID Name 1 Terence 2 Richard 1. How can I get my datagrid to only show the Name and not the ID column? 2. Also if I have a column named First Name in my datafrid how can I get name to show up in that column and not in a new one or how can I assign custom names to my datagrid? Any help would be great thanks!
Solution: dataGridView1.Columns[0].Visible = false; dataGridView1.Columns[1].HeaderText = "First Name"; Is this the best way to do it? Any downsides?
-
I have the following code:
SqlConnection con = new SqlConnection("connection details"); con.Open(); string strSql = "select \* from Names"; SqlDataAdapter dadapter = new SqlDataAdapter(); dadapter.SelectCommand = new SqlCommand(strSql, con); DataSet dset = new DataSet(); dadapter.Fill(dset,"Names"); con.Close(); dataGridView1.DataSource = dset.Tables\["Names"\];
The dset looks as follows: ID Name 1 Terence 2 Richard 1. How can I get my datagrid to only show the Name and not the ID column? 2. Also if I have a column named First Name in my datafrid how can I get name to show up in that column and not in a new one or how can I assign custom names to my datagrid? Any help would be great thanks!
Define only the columns that you want to display in the datagridview:
this.dataGridView.AutoGenerateColumns = false;
this.dataGridView.Columns.Clear();
this.dataGridView.Columns.AddRange(GetGridViewColumns());private DataGridViewColumn[] GetGridViewColumns()
{
DataGridViewTextBoxColumn id = new DataGridViewTextBoxColumn();
id .DataPropertyName = "ID";
id .HeaderText = "ID";
id .Name = "ID";
id .SortMode = DataGridViewColumnSortMode.Automatic;
id .ReadOnly = true;
id .Width = 100;
id .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
id .DefaultCellStyle.Format = "";DataGridViewTextBoxColumn name= new DataGridViewTextBoxColumn();
name.DataPropertyName = "Name";
name.HeaderText = "Name";
name.Name = "Name";
name.SortMode = DataGridViewColumnSortMode.Automatic;
name.ReadOnly = true;
name.Width = 100;
name.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
name.DefaultCellStyle.Format = "";return new DataGridViewColumn[] { id, name };
} -
Solution: dataGridView1.Columns[0].Visible = false; dataGridView1.Columns[1].HeaderText = "First Name"; Is this the best way to do it? Any downsides?
-
Define only the columns that you want to display in the datagridview:
this.dataGridView.AutoGenerateColumns = false;
this.dataGridView.Columns.Clear();
this.dataGridView.Columns.AddRange(GetGridViewColumns());private DataGridViewColumn[] GetGridViewColumns()
{
DataGridViewTextBoxColumn id = new DataGridViewTextBoxColumn();
id .DataPropertyName = "ID";
id .HeaderText = "ID";
id .Name = "ID";
id .SortMode = DataGridViewColumnSortMode.Automatic;
id .ReadOnly = true;
id .Width = 100;
id .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
id .DefaultCellStyle.Format = "";DataGridViewTextBoxColumn name= new DataGridViewTextBoxColumn();
name.DataPropertyName = "Name";
name.HeaderText = "Name";
name.Name = "Name";
name.SortMode = DataGridViewColumnSortMode.Automatic;
name.ReadOnly = true;
name.Width = 100;
name.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
name.DefaultCellStyle.Format = "";return new DataGridViewColumn[] { id, name };
}Thank you so much this is perfect. Just what I am looking for! Codeproject is such a great community!
-
I have the following code:
SqlConnection con = new SqlConnection("connection details"); con.Open(); string strSql = "select \* from Names"; SqlDataAdapter dadapter = new SqlDataAdapter(); dadapter.SelectCommand = new SqlCommand(strSql, con); DataSet dset = new DataSet(); dadapter.Fill(dset,"Names"); con.Close(); dataGridView1.DataSource = dset.Tables\["Names"\];
The dset looks as follows: ID Name 1 Terence 2 Richard 1. How can I get my datagrid to only show the Name and not the ID column? 2. Also if I have a column named First Name in my datafrid how can I get name to show up in that column and not in a new one or how can I assign custom names to my datagrid? Any help would be great thanks!
-
Thanks for the info :) , I need the * as I need the ID for other things. I just dont want the ID to appear in the datagrid...
Using "*" is considered bad practice. They only reason why you're seeing the ID in the datagrid is because you're relying on it's automatic features to create the columns for you. If you created the columns and bound them yourself, you get to pick and chose which columns you see in the grid. You've already been given links to the docs and samples that show this.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009...