替代的DataGridView DataBindingComplete事件 [英] Alternative to DataGridView DataBindingComplete Event

查看:1428
本文介绍了替代的DataGridView DataBindingComplete事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如前所述的这里 DataBindingComplete 事件中的DataGridView被激发每当数据源的内容发生变化,或属性,如数据源的变化。这将导致该方法的多次调用。结果
我目前使用 DataBindingComplete 事件做一些视觉格式化到我的形式。例如,我做的第一列文(列0)显示为行标题,然后隐藏该列(见下面的代码)。

As stated here the DataBindingComplete event for a DataGridView is fired whenever the contents of the data source change, or a property such as DataSource changes. This results in the method being called multiple times.
I am currently using the DataBindingComplete event to do some visual formatting to my form. For example, I make the text in the first column (column 0) appear as Row Headers and then hide that column (see code below).

private void grdComponents_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
    { 
        foreach (DataGridViewRow row in grdComponents.Rows)
        {
            row.HeaderCell.Value = row.Cells[0].Value.ToString();
        }
        grdComponents.Columns[0].Visible = false;

        // do more stuff...
    }



这是不必要的不​​止一次地执行这个代码,我期待把它放到哪里可能发生的地方。不幸的是,当我加入了片段到我的窗体的加载方法的末尾(之后我将没有工作的DataSource 我的DataGridView的),也没有在 DataSourceChanged 事件工作。

It is unnecessary to execute this code more than once, and I am looking to put it into a place where that can happen. Unfortunately it didn't work when I added the snippet to the end of my form's Load method (after I set the DataSource of my DataGridView), nor did it work in the DataSourceChanged event.

推荐答案

最简单的方法将只是一次执行这个代码:

The simplest way will be just to execute this code once:

添加像布尔isDataGridFormatted 在一个标志你的表单。

Add a flag like Boolean isDataGridFormatted in your form.

和检查它像

private void grdComponents_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{ 
    if (this.isDataGridFormatted )
        return;

    foreach (DataGridViewRow row in grdComponents.Rows)
    {
        row.HeaderCell.Value = row.Cells[0].Value.ToString();
    }
    grdComponents.Columns[0].Visible = false;

    // do more stuff...

    this.isDataGridFormatted  = false;         
}



好位将是形式在施工期间准备的DataGridView。据我了解你的列将你的程序的过程中不会改变,但你不想手动初始化一切。在初始化过程中,您可以加载一些虚拟的一个项目(单排)数据:

A bit better will be to prepare your DataGridView during the form construction. As I understand your columns won't change during the course of your program but you don't want to initialize everything manually. You could load some dummy one-item(one-row) data during the initialization:

private void Initialize_DataGridView()
{
    // Add dummy data to generate the columns
    this.dataGridView_Items.DataContext = new Item[]{ new Item {Id = 5, Value = 6}};

    // Make your formatting
    foreach (DataGridViewRow row in grdComponents.Rows)
    {
        row.HeaderCell.Value = row.Cells[0].Value.ToString();
    }

    grdComponents.Columns[0].Visible = false;

    // Reset the dummy data
    this.dataGridView_Items.DataContext = null; // Or new Item[]{};
}

...
public MyForm()
{
    Initialize();

    this.Initialize_DataGridView();   
}



我不知道,正是这样的代码将与的dataGridView工作,但它接近。足够的

I am not sure that exactly such code will work with dataGridView but it is close enough.

当然,事件本来是一个近乎理想的解决方案,但有几乎没有任何与列成功的自动生成的 http://msdn.microsoft.com/en-us/library/ system.windows.forms.datagridview_events(v = vs.110)的.aspx 除了 AutoGenerateColumnChanged 但是这不是我们所需要的。

Of course an event would have been a nearly ideal solution but there's hardly any that deals with successful autogeneration of columns http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview_events(v=vs.110).aspx except the AutoGenerateColumnChanged but that is not what we need.

尽管可以使用 Col​​umnAdded - 它可能会只执行一次自动生成的列的foreach,实际执行可能成为矫枉过正,将是比前面提到的方法直接连少

While it is possible to use the ColumnAdded - it will probably execute only once foreach of the autogenerated column, the actual implementation could become an overkill and will be even less direct than already mentioned approaches.

如果您将有一段时间,希望你可以创建自己的DataGridView的派生类,以布尔isDataGridFormatted 从表单和实施所有的初始化(或事件挂钩)自定义的DataGridView里面。

If you will have some time and desire you could create your own DataGridView derived class, take Boolean isDataGridFormatted from your form and implement all the initialization(or event hooking) inside the custom DataGridView.

这篇关于替代的DataGridView DataBindingComplete事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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