开发者

Unexpected result in deleting DataGridView Columns

        DataTable dt = new DataTable();
        dt.Columns.Add("col1");
        dt.Columns.Add("col2");
        dt.Columns.Add("col3");
        dt.Columns.Add("col4");
        dt.Columns.Add("col5");
        dataGridView1.DataSource = dt;
        dataGridView1.Columns.RemoveAt(3);
        dataGridView1.Columns.RemoveAt(2);
        dataGridView1.Columns.RemoveAt(0);

In this program, I created a DataTable with 5 columns. This will be the DataSource of the DataGridView. Some columns in the DataTable doesn't need to be seen by the user, but will be used by the program later on.

After the last line, the columns are arranged as : col2, col5, col1, col3, col4. Why does it appear like this? Shouldn't it be removed from the DataGridView? What should I do to make it appear as "col2, col5"?

EDIT: I want to remove some Columns from the DataGridView, yet still be available in the DataTable. Also, it works inside an event (like Button_Click)

EDIT: I still haven't figured out why this is happening. I have no choice but to create another thread (BackgroundWorker) to do this...

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        DataTable dt = new DataTable();
        int i;
        dataGridView1.Invoke((MethodInvoker)delegate
        {
            dt.Columns.Add("col1");
            dt.Columns.Add("col2");
            dt.Columns.Add("col3");
            dt.Columns.Add("col4");
            dt.Columns.Add("col5");
            dataGridView1.DataSource = dt;
            dataGridView1.Columns.RemoveAt(3);
            dataGridView1.Col开发者_StackOverflowumns.RemoveAt(2);
            dataGridView1.Columns.RemoveAt(0);
        });
    }

As far as I can tell, Haris Hasan was right; that the Form containing the DataGridView should appear at least once. But I'm still confused why...


As far as I know you will have to display DataGridView once before you can remove any column. Try this by displaying DataGridView one time and then delete the columns and see what happens


Instead of removing the columns you could try setting their visibility...

        dataGridView1.DataSource = dt;
        dataGridView1.Columns[3].Visible = false;
        dataGridView1.Columns[2].Visible = false;
        dataGridView1.Columns[0].Visible = false;


You can use DisplayIndex property of the DataGridView to order the columns. In order to hide a column you can set false to the Visible property of a particular column.

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


If you don't want columns in the grid for all of the columns in the table, set datagridview.AutoGenerateColumns to false before binding the datatable to the datasource. Then create and bind the datagridview columns you actually want to have.

AutoGenerateColumns defaults to true, and yet it very seldom gives us what we really want. Take the time to build your datagridview columns exactly as you want and don't trust the default processing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜