简体   繁体   中英

How to add a new row to the datagridview and edit it in C# winform?

I have set the datasource of datagridview as dataset. I want to add a new row to this datagridview. This new row should be editable and I need to save this data to my DB. How can I achieve this?

// Datasource is set

dataGridView.DataSource = ds.Tables[0];

// Add new row

 private void button_add_Click(object sender, EventArgs e)
 {
     DataTable dt = dataGridView.DataSource as DataTable;
     dt.Rows.Add();
     dataGridView.DataSource = dt;
 }

// This creates a new row but it is not editable.

PS I am a beginner in C#

Edit: Can anyone please provide a link/example where I can directly add, edit, delete from datagridview without using any form?

private void button_add_Click(object sender, EventArgs e) {
    foreach (DataGridViewRow row in DataGridView1.Rows) {
        if (// condition for true) {
            row.Rows[row that should be editable].ReadOnly = true;
        } else if(// condition for false) {
            row.Rows[row that schould not be editable].ReadOnly = false;
        }
        var value = row.Cells[cellindex].Value;
    }
}

I presume your problem here, is that you want the rest of the DataGridView to be ReadOnly? Unfortunately the two properties AllowUserToAddRows and ReadOnly are contradictory, if ReadOnly is true, then AllowUserToAddRows will indeed give a new row, but you cannot edit it.

So if you really want to do this then you need to be a little creative. There is more than one way, but something like this should work. Set AllowUserToAddRows to true and ReadOnly to false. Add a private field:

private int addedRowIndex = -1;

Next add a couple of events:

private void dataGridView1_UserAddedRow(object sender, DataGridViewRowEventArgs e)
{
    addedRowIndex = dataGridView1.NewRowIndex - 1;
}

and

private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
    {
        if (!dataGridView1.Rows[e.RowIndex].IsNewRow)
        {
            if (addedRowIndex != e.RowIndex)
            {
                e.Cancel = true;
            }
        }
    }
}

This will have the effect of cancelling all changes to old rows.

Please note however that I never do anything like this myself. I have many, many forms with DataGridViews and they are always ReadOnly true and AllowUserToAddRows false. To add a new Record, I provide a button which opens a Modal Form allowing the user to enter the new record. This record is then saved to the database and the form closed, at which point the DataGridView gets refreshed. I find this makes it far easier to validate entries prior to saving. Not only that but it is much easier to provide specialised controls for particular data types, dates being a very good example. I know people like the convenience of a spreadsheet type "look and feel" but a database application should put data integrity first.

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