显示DataGridView中的按钮列 [英] Showing a button column in a DataGridView

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

问题描述

在一个Win表单应用程序中,我遇到数据绑定问题,并在DataGridView中显示一个命令按钮列作为最后一列,允许用户在编辑内容后保存每个记录。



这是演示者课程中完成的绑定部分

  private void ShowEarnings()
{
BindingSource BS = new BindingSource();
var wageInfo = _WageManager.PrepareEarnings(_Model); //工资信息包含收益对象列表。
BS.DataSource = wageInfo.GetEarnigsList(); //返回WageInfo对象中包含的收入列表。
_View.EarningDetails = BS;
}

在form_load事件中,我向DGV添加了一个命令按钮列,如下所示: / p>

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

加载表单时,最后一列显示为标题 ACTION ,单元格中显示一个空白按钮(第一个屏幕截图)。当调用 ShowEarnings()方法时,DGV将被填充,现在按钮列显示为第3列(第二个屏幕截图)。但应该显示为最后一列!



如何排序?



请看屏幕截图。





编辑:正确显示。但是我想要它首先!

解决方案

订阅 DataBindingComplete 事件并重置 DisplayIndex 属性。

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


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

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

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?

Please see the screen shots.

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

解决方案

Subscribe the DataBindingComplete event and reset the DisplayIndex property.

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

这篇关于显示DataGridView中的按钮列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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