【问题标题】:Showing a button column in a DataGridView在 DataGridView 中显示按钮列
【发布时间】:2014-08-16 10:54:13
【问题描述】:

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

这是在presenter类中完成的绑定部分

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 添加了一个命令按钮列,如下所示

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

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

请问如何解决?

请查看屏幕截图。

编辑:如果我在绑定之后添加列,它会正确显示。但我要先!

【问题讨论】:

  • 尝试设置column.DisplayIndex = lastIndex
  • @Sriram Sakthivel,没用!问题是即使它最初显示为最后一列,绑定后到第三列?

标签: c# .net data-binding datagridview mvp


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2014-06-28
    • 1970-01-01
    • 1970-01-01
    • 2011-10-02
    • 1970-01-01
    • 2012-03-21
    • 2012-09-13
    • 1970-01-01
    • 2016-07-15
    相关资源
    最近更新 更多