Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,may i know is that any way to show selected database column to datagridview?Here is my code to connect database and show in datagridview but it show all database column
C#
private void tmst_timein_new_Load(object sender, EventArgs e)
        {
         try
         {
            using (TMPB_attn_DAL dalObj = new TMPB_attn_DAL())
          {
              dataGridView1.DataSource = dalObj.GetRecords().Cast<TMPB_attn>().ToList();
              dataGridView1.Refresh();
                    
                }
            }
            catch (Exception es)
            {
                MessageBox.Show(es.Message);

            }
        }

I just want a few column to display only!but is using this code is too much i need to hide it out!!!
C#
dataGridView1.Columns["attn_id"].Visible = false;

Please teach me thank you!
Posted

You should set AutoGenerateColumns=false and add the specific column manually.
 
Share this answer
 
in your aspx page set dataGridView1 AutoGenerateColumn = false;
then in BoundColumn set DataField = "attn_id";
and then update your code as below.

C#
private void tmst_timein_new_Load(object sender, EventArgs e)
        {
         try
         {
            using (TMPB_attn_DAL dalObj = new TMPB_attn_DAL())
          {
              dataGridView1.DataSource = dalObj.GetRecords();
              dataGridView1.DataBind();

                }
            }
            catch (Exception es)
            {
                MessageBox.Show(es.Message);

            }
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900