Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How do I get a datagrid to only show certain columns from a dataset?

How do I get a datagrid to only show certain columns from a dataset?

Scheduled Pinned Locked Moved C#
questionhelp
13 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Terence van Schalkwyk

    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!

    L Offline
    L Offline
    Luc Pattyn
    wrote on last edited by
    #2

    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.

    T 1 Reply Last reply
    0
    • T Terence van Schalkwyk

      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!

      M Offline
      M Offline
      meeram395
      wrote on last edited by
      #3

      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.

      T 1 Reply Last reply
      0
      • T Terence van Schalkwyk

        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!

        A Offline
        A Offline
        Abhinav S
        wrote on last edited by
        #4

        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.

        T 1 Reply Last reply
        0
        • L Luc Pattyn

          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.

          T Offline
          T Offline
          Terence van Schalkwyk
          wrote on last edited by
          #5

          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...

          D 1 Reply Last reply
          0
          • M meeram395

            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.

            T Offline
            T Offline
            Terence van Schalkwyk
            wrote on last edited by
            #6

            I am doing it in C# windows application not in asp. Any idea how to do it?

            1 Reply Last reply
            0
            • A Abhinav S

              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.

              T Offline
              T Offline
              Terence van Schalkwyk
              wrote on last edited by
              #7

              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!

              1 Reply Last reply
              0
              • T Terence van Schalkwyk

                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!

                T Offline
                T Offline
                Terence van Schalkwyk
                wrote on last edited by
                #8

                Solution: dataGridView1.Columns[0].Visible = false; dataGridView1.Columns[1].HeaderText = "First Name"; Is this the best way to do it? Any downsides?

                D 1 Reply Last reply
                0
                • T Terence van Schalkwyk

                  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!

                  D Offline
                  D Offline
                  darkelv
                  wrote on last edited by
                  #9

                  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 };
                  }

                  T 1 Reply Last reply
                  0
                  • T Terence van Schalkwyk

                    Solution: dataGridView1.Columns[0].Visible = false; dataGridView1.Columns[1].HeaderText = "First Name"; Is this the best way to do it? Any downsides?

                    D Offline
                    D Offline
                    darkelv
                    wrote on last edited by
                    #10

                    If someone added another column, or insert a column before the firstcolumn, the datagridview will display wrongly. Generally I will define the columns to be displayed and their order, instead of hiding the columns that I do not want to display.

                    1 Reply Last reply
                    0
                    • D darkelv

                      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 };
                      }

                      T Offline
                      T Offline
                      Terence van Schalkwyk
                      wrote on last edited by
                      #11

                      Thank you so much this is perfect. Just what I am looking for! Codeproject is such a great community!

                      1 Reply Last reply
                      0
                      • T Terence van Schalkwyk

                        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!

                        B Offline
                        B Offline
                        Bigdeak
                        wrote on last edited by
                        #12

                        try this after you set the DataSource:

                        dataGridView1.DataSource = dset.Tables["Names"];

                        dataGridView1.Columns["ID"].Visible = false;

                        Hope it could help you.

                        1 Reply Last reply
                        0
                        • T Terence van Schalkwyk

                          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...

                          D Offline
                          D Offline
                          Dave Kreskowiak
                          wrote on last edited by
                          #13

                          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...

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups