简体   繁体   中英

Add new blank row to datagridview c#

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

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:

另一个示威

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM