【问题标题】:Adding customize DatagridviewRow using binding source in c#在 c# 中使用绑定源添加自定义 DatagridviewRow
【发布时间】:2012-02-11 19:24:59
【问题描述】:

有没有办法使用 c# 添加自定义 DatagridviewRow?

我已经为这些行做了一个类。

class CustomizeRow: System.Windows.Forms.DataGridViewRow
{
    ...
}

现在我可以添加一些我的自定义行

        DataGridViewColumn column = new DataGridViewTextBoxColumn();
        dataGridView1.Columns.Add(column);

        CustomizeRow row = new CustomizeRow();
        dataGridView1.Rows.Add(row);

但是当我使用绑定源添加这样的行时,我该如何处理

        bs = new BindingSource();
        bs.DataSource = dataSet1.Tables["Table"];
        dataGridView1.DataSource = bs;

【问题讨论】:

  • 你可以尝试使用onrowdatabound事件吗?
  • 以前的贡献是解决方案。 Customrow rowtemp = new CustomRow(); dataGridView1.RowTemplate = rowtemp;

标签: c# .net datagridview customization


【解决方案1】:

您需要使用DataGridView.RowTemplate

来自 RowTemplate 上的 MSDN 页面:

显示外部数据时需要设置行模板 使用从 DataGridViewRow 类派生的自定义类型。和 未绑定的 DataGridView 控件,您可以以编程方式填充 带有自定义行类型实例的行集合。什么时候 显示外部数据,但是会生成行 自动,但它们基于行模板,您可以 设置为您的自定义行类型的实例。

所以你可以像这样简单地设置模板行:

CustomizeRow row = new CustomizeRow(); 
dataGridView1.RowTemplate = row;

【讨论】:

  • @Marius - 谢谢,不是故意唠叨 :) 但一些新用户不知道事情是如何运作的。欢迎使用 StackOverflow
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-03
相关资源
最近更新 更多