Home > Enterprise >  C# Datagridview method Rows.Add works incorrectly - it add line on previous, not on last
C# Datagridview method Rows.Add works incorrectly - it add line on previous, not on last

Time:05-22

I have a problem with datagridview.Rows.Add method. I want to add a new line but in the last line, not in previous. I don't know why the method Add doesn't work in this case. My code:

        dataGridView1.RowCount = 3;
        dataGridView1.ColumnCount = 4;

        dataGridView1.Rows[0].Cells[0].Value = 1;
        dataGridView1.Rows[0].Cells[1].Value = 2;
        dataGridView1.Rows[0].Cells[2].Value = 3;
        dataGridView1.Rows[0].Cells[3].Value = 4;

        dataGridView1.Rows[1].Cells[0].Value = 5;
        dataGridView1.Rows[1].Cells[1].Value = 6;
        dataGridView1.Rows[1].Cells[2].Value = 7;
        dataGridView1.Rows[1].Cells[3].Value = 8;

        dataGridView1.Rows[2].Cells[0].Value = 9;
        dataGridView1.Rows[2].Cells[1].Value = 10;
        dataGridView1.Rows[2].Cells[2].Value = 11;
        dataGridView1.Rows[2].Cells[3].Value = 12;


        dataGridView1.Rows.Add(13,14,15,16);

And result:

enter image description here

I will be thrilled for any help or advice!

CodePudding user response:

A better idea is not to set RowCount when you will be entering new rows. So if open to a alternate read on.

In the example below there is no assertion to validate row.Cells[3].Value is an int but in the 2nd block shows how to check correctly.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        dataGridView1.ColumnCount = 4;

        for (int index = 0; index < dataGridView1.Columns.Count; index  )
        {
            dataGridView1.Columns[index].HeaderText = $"Column {index  1}";
        }

        dataGridView1.Rows.Add(1, 2, 3, 4);

    }

    private void button1_Click(object sender, EventArgs e)
    {

        if (dataGridView1.Rows.Count == 1)
        {
            dataGridView1.Rows.Add(1, 2, 3, 4);
        }

        for (int index = 0; index < 4; index  )
        {
            var row = dataGridView1.Rows.Cast<DataGridViewRow>()
                .LastOrDefault(gridRow => !gridRow.IsNewRow);

            var lastValue = Convert.ToInt32(row.Cells[3].Value)   1;

            dataGridView1.Rows.Add(
                lastValue, 
                lastValue  = 1, 
                lastValue  = 1, 
                lastValue  = 1);
        }

    }
}

Assert cell value can represent an int

for (int index = 0; index < 4; index  )
{
    var row = dataGridView1.Rows.Cast<DataGridViewRow>()
        .LastOrDefault(gridRow => !gridRow.IsNewRow);

    if (int.TryParse(row.Cells[3].Value.ToString(), out var lastValue))
    {
        lastValue   = 1;

        dataGridView1.Rows.Add(
            lastValue,
            lastValue  = 1,
            lastValue  = 1,
            lastValue  = 1);
    }

}

enter image description here

CodePudding user response:

This is the correct way to insert a line in a grid view:

dataGridView1.Rows.Add(13,14,15,16);

The last line marked with * is in fact a placeholder for new lines and it is better not to insert it directly tempe place to make new row

  • Related