【问题标题】:Add new blank row to datagridview c#将新的空白行添加到datagridview c#
【发布时间】:2016-02-23 12:55:23
【问题描述】:

我有一个DataGridView,它有一个DataTable 作为DataSource。现在我想在DataGridView 中插入一个新的空行并使其完全变黑(就像分隔线一样)。我试图复制一行并将其变为黑色:

Datagridview1.rows.insertcopies(2,2,1);

但是我收到了这个错误:

当控件是数据绑定时,行不能以编程方式添加到 datagridview 的行集合中。

我怎样才能保持DataGridView 的原样并插入一个空白行?

澄清

我想做一个分隔线。我找不到任何解决方案,所以我想做一个空行并使其变黑。使用:

for(int i=0;i<datagridview1.Rows.Count-1;i++)
{
    if(datagridview1.Rows[i].Cells[1].formattedvalue.ToString() != datagridview1.Rows[i+1].Cells[1].formattedvalue.ToString())
    {
        //here I need to make a divider.
    }
}

【问题讨论】:

  • 不确定 WinForms 是否有简单的方法。您可以尝试将数据绑定项包装到一些视图模型中,这些视图模型可能代表适当的行或分隔符,然后在适当的事件上处理数据绑定或分隔符行重绘,但这不是一个简单或好看的解决方案。
  • 您的数据源是什么样的?它是一个数据表?
  • 巴迪帕尔马吉。是的,我有一个数据表,可以从 excel 文件中获取她的数据。

标签: c# winforms datagridview insert


【解决方案1】:

虽然您可以添加一个空白行作为分隔符,但这对我来说更像是一种黑客行为。相反,我会为现有的单元格边框着色。

这实际上比我预期的要付出更多的努力——我认为这就像在行分隔符上设置颜色一样简单,但显然that's not how it works

但是你还是可以自己动手画的:

this.dataGridView1.CellPainting += DataGridView1_CellPainting;

同样,由于您对分隔线的绘制取决于单元格值,因此您需要处理:

this.dataGridView1.CellValueChanged += DataGridView1_CellValueChanged;

这个想法是检查您的条件(将所需列中的单元格值与同一列中的下一个单元格值进行比较),并在需要时为需要分隔符的行中的每个单元格绘制分隔符。当条件列中的单元格值发生更改时,应使其整行无效以触发行中的每个单元格重新绘制。同样,如果绘制分隔线的条件现在也发生了变化,则应该将之前的设置无效。

private void DataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if (e.RowIndex >= 0 && e.RowIndex != this.dataGridView1.Rows.Count - 1)
    {
        DataGridViewRow thisRow = this.dataGridView1.Rows[e.RowIndex];
        DataGridViewRow nextRow = this.dataGridView1.Rows[e.RowIndex + 1];

        if (thisRow.Cells[1].FormattedValue.ToString() != nextRow.Cells[1].FormattedValue.ToString())
        {
            e.Paint(e.ClipBounds, DataGridViewPaintParts.All);

            Rectangle divider = new Rectangle(e.CellBounds.X, e.CellBounds.Y + e.CellBounds.Height - 2, e.CellBounds.Width, 2);
            e.Graphics.FillRectangle(Brushes.Black, divider);

            e.Handled = true;
        }
    }
}

private void DataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == 1)
    {
        if (e.RowIndex > 0)
        {
            this.dataGridView1.InvalidateRow(e.RowIndex - 1);
        }

        this.dataGridView1.InvalidateRow(e.RowIndex);
    }
}

相反,您也可以使用RowPrePaint 而不是CellPainting 来执行此操作,但最终会出现以下可能性:

【讨论】:

    【解决方案2】:

    这个错误几乎是不言自明的,我能想到一个解决方法,它可能会给你最终结果,但它真的很“丑陋”,并且可能会在未来的操作中破坏数据,所以我不确定我会推荐它。

    DataRow empty = table.NewRow();
    table.Rows.Add(empty);
    

    【讨论】:

      【解决方案3】:

      您可以在DataTable中添加一个新行并再次绑定它! 像

              DataRow rd = ds.Tables[0].NewRow();
      
              ds.Tables[0].Rows.Add(rd);
      
              GridView1.DataSource = ds;
              GridView1.DataBind();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-06-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-11
        • 2011-11-03
        • 1970-01-01
        相关资源
        最近更新 更多