简体   繁体   English

DataGridView显示设置为不可见的列

[英]DataGridView shows columns that were set to non-visible

I have 3 datagridviews, and each of them has a separate binding source. 我有3个datagridviews,每个视图都有一个单独的绑定源。 All three binding sources however get data from the same datatable. 但是,所有三个绑定源都从同一数据表获取数据。

        bindingSource1.DataSource = mytable;
        bindingSource2.DataSource = mytable;
        bindingSource3.DataSource = mytable;
        dataGridView1.DataSource = bindingSource1;
        dataGridView2.DataSource = bindingSource2;
        dataGridView3.DataSource = bindingSource3;

I control what the user sees with the following logic: Show 10 first columns on the first grid, the next 10 on the second, and the next 10 on the third. 我用以下逻辑控制用户看到的内容:在第一个网格上显示10个第一列,在第二个网格上显示接下来的10列,在第三个网格上显示接下来的10列。

for (int i = 0; i < mytable.Columns.Count; i++)
        {
            dataGridView1.Columns[i].Visible = i < 10;
            dataGridView2.Columns[i].Visible = (i >= 10 && i < 20);
            dataGridView3.Columns[i].Visible = (i >= 20 && i < 30);
        }

This works fine when I have many columns in the datatable. 当我在数据表中有很多列时,这可以很好地工作。

Problem If I have less than 10 columns in my datatable, normally they should only be shown in the first datagridview. 问题如果我的数据表中的列少于10个,则通常只应在第一个datagridview中显示它们。 This does happen, but also the first column of the datatable is always shown in datagrid 2 and 3. I have stepped through my loop to see whether a condition is wrong, and I found it to be correct. 确实发生了这种情况,但是datatable的第一列始终显示在datagrid 2和3中。我已经遍历循环以查看条件是否错误,并且发现它是正确的。 Therefore, I am pretty sure it must be one of the events that follow this. 因此,我很确定这一定是随之而来的事件之一。 I have two events registed for my grids that could be the cuplrits: RowPostPaint and CellPainting . 我为我的网格注册了两个事件,可能是cuplrits: RowPostPaintCellPainting I commented out everything that I was doing in these events and still get this problem. 我注释掉了我在这些事件中所做的一切,但仍然遇到此问题。 I also have some others, like DataError, CellValueChanged(empty inside), Scroll etc, but I think they are irrelevant. 我也有其他一些,例如DataError,CellValueChanged(empty inside),Scroll等,但是我认为它们是无关紧要的。

So I am wondering whether there is another event which I haven't registered, which could be doing this by itself. 因此,我想知道是否还有另一个我尚未注册的事件,它可能是自己完成的。

You're seeing this behavior because of a default behavior that happens when binding the data source. 由于绑定数据源时会发生默认行为,因此您会看到此行为。 To fix, handle each DataGridView.DataBindingComplete event. 要修复,请处理每个DataGridView.DataBindingComplete事件。 We will use the same event handler for each: 我们将为每个使用相同的事件处理程序:

dataGridView1.DataBindingComplete += DataGridView_DataBindingComplete;
dataGridView2.DataBindingComplete += DataGridView_DataBindingComplete;
dataGridView3.DataBindingComplete += DataGridView_DataBindingComplete;

In that event handler, we will set the visible columns as before. 在该事件处理程序中,我们将像以前一样设置可见列。 But we will also set the row headers' visibility as well as scroll bars - in case no columns are visible. 但是,如果没有可见的列,我们还将设置行标题的可见性以及滚动条。

private void DataGridView_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
    DataGridView view = sender as DataGridView;
    bool anyVisible = false;
    int max = 0, min = 0;

    if (view == this.dataGridView1)
    {
        min = 0;
        max = 10;
    }
    else if (view == this.dataGridView2)
    {
        min = 10;
        max = 20;
    }
    else if (view == this.dataGridView3)
    {
        min = 20;
        max = 30;
    }

    for (int i = 0; i < this.table.Columns.Count; i++)
    {
        view.Columns[i].Visible = i >= min && i < max;
        anyVisible = anyVisible || view.Columns[i].Visible;
    }

    view.RowHeadersVisible = anyVisible;
    view.ScrollBars = anyVisible ? ScrollBars.Both : ScrollBars.None;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM