Alternative to DataGridView DataBindingComplete Event

后端 未结 2 1465
没有蜡笔的小新
没有蜡笔的小新 2021-01-24 23:56

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

2条回答
  •  旧巷少年郎
    2021-01-25 00:41

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

    Add a flag like Boolean isDataGridFormatted in your form.

    And check it like

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

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

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

    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.

    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.

    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.

提交回复
热议问题