I want to show only specify database table in datagridview.I had use

dataGridView1.AutoGenerateColumns = false;

but i don't know how to add the column.For example on of the column is "attn_id".How should i add it?
here my code to connect database

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

And is this the only way to show specific table from database?

Hi

AutoGenerateColumns simply tells the DataGridView whether it should use the column names from your data source as headers or not, it is not responsible for showing or hiding columns.

What does the GetRecords() method return? If it is a DataTable then you can strip out the columns that you don't want and bind to your DataGridView.

And is this the only way to show specific table from database?

No, there are many ways, but looking at your code, it appears that you have a DAL in place and you are probably supposed to use it, although I could be wrong.

Hi Yuki.

DataGridView.Columns is a collection that you can manipulate as you wish.

There's a couple ways of doing it, one way to just add one more columns is:

this.myDataGrid.Columns.Add(new DataGridTextColumn() {
    HeaderText = "My Column",
    Name = "My Column",
    DataPropertyName = "MyDataName"
});

Be aware that exists more than one type of DataGridViewColumn, see more here.

And are you developing Windows Forms on Visual Studio?

If you are, when you create something on with the VS UI, like adding columns to your grid, or a button, VS actually generates a file named 'YourFormName.Designer.cs'. In that file you can learn how VS creates all what you can see the on UI come true as code. It's a nice way to learn how to create programatically what VS gives you on the UI.

commented: Good advice. +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.