DataGridView显示被设置为不可见的列 [英] DataGridView shows columns that were set to non-visible

查看:534
本文介绍了DataGridView显示被设置为不可见的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个datagridview,每个都有一个单独的绑定源。然而,所有三个绑定源都从相同的数据表获取数据。

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

我使用以下逻辑控制用户看到的内容:在第一个网格上显示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);
}

当我在datatable中有很多列时,这可以正常工作。



问题如果我的datatable中的列少于10列,通常只能在第一个datagridview中显示。这确实发生了,但datatable中的第一个列始终显示在datagrid 2和3.我已经走过我的循环来查看条件是否错误,我发现它是正确的。因此,我很确定它必须是跟随之后的事件之一。我为我的网格注册了两个可能是cuplrits的事件: RowPostPaint CellPainting 。我评论了我在这些事件中所做的一切,仍然得到这个问题。我还有一些其他的,像DataError,CellValueChanged(内部空的),Scroll等,但我认为它们是无关的。



所以我想知道是否有另一个事件我没有注册,这可能是自己做的。

解决方案

由于默认行为,您看到此行为当绑定数据源时会发生这种情况。要修复,处理每个 DataGridView.DataBindingComplete 事件。我们将为每个使用相同的事件处理程序:

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

在该事件处理程序中,我们将像以前一样设置可见列。但是我们还将设置行标题的可见性以及滚动条 - 如果没有列可见。

  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<最大
anyVisible = anyVisible || view.Columns [i] .Visible;
}

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


I have 3 datagridviews, and each of them has a separate binding source. 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.

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

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

这篇关于DataGridView显示被设置为不可见的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆