在一个DataGridView显示一个按钮列

问题描述:

在一个双赢的形式应用我有一个数据绑定并显示在DataGridView的最后一列允许用户编辑内容后,每个记录保存一个命令按钮列问题。

In a Win forms application I have a problem with data binding and showing a command button column in the DataGridView as the last column allowing users to save each records after editing the content.

这是在演示类完成结合部

This is the binding part done in the presenter class

private void ShowEarnings()
{
    BindingSource BS = new BindingSource();
    var wageInfo = _WageManager.PrepareEarnings(_Model); // wageInfo contains a list of earnings objects.
    BS.DataSource = wageInfo.GetEarnigsList(); //Earnings List contained in WageInfo object is returned.           
    _View.EarningDetails = BS;
}

在Form_Load事件中我的命令按钮列添加到DGV

In the form_load event I add a command button column to the DGV as follows

private void ShowSaveEarningsButton()
{
    DataGridViewButtonColumn col = new DataGridViewButtonColumn();
    col.UseColumnTextForButtonValue = true;
    col.Text = "SAVE";
    col.Name = "ACTION";
    dgEmployeeEarnings.Columns.Add(col);
}

当加载窗体的最后一列显示的标题 ACTION 和空白按钮在单元格中(第一截屏)所示。当 ShowEarnings()方法被调用时,DGV填充现在的按钮栏显示为第3列(第二截屏)。但应显示为总是最后一列!

When the form is loaded the last column is shown with the header ACTION and a blank button is shown in the cell (first screen shot). When the ShowEarnings() method is called, the DGV is populated and now the button column is shown as the 3rd column (second screen shot). But it should be shown as the last column always!.

如何排序了这一点吗?

How to sort this out please?

请看清屏幕截图

编辑:如果我绑定后添加列是正常显示。但我想它第一次!

if I add the column after the binding it is shown properly. But I want it first!

订阅的 DataBindingComplete 事件和复位在的DisplayIndex 属性。

Subscribe the DataBindingComplete event and reset the DisplayIndex property.

void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
    dgEmployeeEarnings.Columns["ACTION"].DisplayIndex = dgEmployeeEarnings.Columns.Count - 1;
}