Add new blank row to datagridview c#

ⅰ亾dé卋堺 提交于 2021-02-10 23:36:10

问题


I have a DataGridView that has a DataTable as the DataSource. Now I want to insert a new empty row to the DataGridView and make it totally black (like a divider line). I have tried to copy one line and make it black with this:

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

But I'm getting this error:

rows cannot be programmatically added to the datagridview's rows collection when the control is data-bound.

How can I keep the DataGridView the way she is and just insert a blank row?

Clarification:

I want to make a divider. I couldn't find any solution to do it so I thought to do a blank line and make it black. Using:

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

回答1:


While you could add a blank row as a divider, this seems more like a hack to me. Instead, I would color the existing cell borders.

This actually took more effort than I expected - I thought it would be as easy as setting a color on the row divider, but evidently that's not how it works.

But you can still paint it yourself by handling:

this.dataGridView1.CellPainting += DataGridView1_CellPainting;

Likewise, since your painting of the divider is dependent upon cell values, you'll want to handle:

this.dataGridView1.CellValueChanged += DataGridView1_CellValueChanged;

The idea is to check your condition (comparing a cell value in the desired column to the next cell value in that same column) and when needed, draw your divider for each cell in the row that needs the divider. When a cell value in the conditional column is changed its whole row should be invalidated to trigger every cell in the row to repaint. Likewise, the previous should be invalidated just in case it's condition to draw the divider has now changed too.

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

Conversely, you could also do this with RowPrePaint instead of CellPainting, but then you end up with the following possibility:




回答2:


The error is pretty much self explanatory, there's a workaround I can think of that might give you the end result, however it's really "ugly" and might disrupt the data in future manipulation, so I'm not sure I would recommend it.

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



回答3:


You can add a new row in the DataTable and bind it again! Something like

        DataRow rd = ds.Tables[0].NewRow();

        ds.Tables[0].Rows.Add(rd);

        GridView1.DataSource = ds;
        GridView1.DataBind();


来源:https://stackoverflow.com/questions/35578195/add-new-blank-row-to-datagridview-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!