C # DataGridView Adding a row - when checking, how do I know that the row is new and not yet committed?

I have a client with a DataGridView problem in a windows application. They raise the CellValidated event, but they want to have a different check for the cell if it is in a row that is already passed back to the data source than if it was a row that is first added (and not yet committed (the user has not left a row) I tried the IsNewRow property, but as soon as you start typing a line, another "new line" is added, so the line you are working with is no longer considered a new line. You know that the line has not been committed yet, because you can click Esc to cancel edits and the whole line goes away.

Is there a way to find out if the edited row is really a β€œnew row” in the sense that it was not passed back to the data source?

+6
c # winforms datagridview
source share
4 answers

GenericMeatUnit's answer did not work for me, but I had enough to find a way to make it work. Here is how I am doing the verification now:

if (this.DATASET.DATATABLE.Rows.Count == e.RowIndex) 

This if statement works because before leaving the row in the DataGridView, it actually does not yet exist in the datatable, so the number of rows in the datatable will be RowIndex for the new row, since RowIndex is zero based.

0
source share

One of the ways that I have achieved in the past is to use the id property of my list-bound objects.

For example, if I have a BindingList<User> , where the user looks something like this:

 public class Names { public string Name { get; set; } public int id { get; set; } } 

Then I can link my list as follows:

 dataGridView1.AutoGenerateColumns = false; _users = new BindingList<User>(); _users .Add(new Names() { Name = "joe", id=1 }); _users .Add(new Names() { Name = "pete", id = 2 }); bindingSource1.DataSource = _names; DataGridViewTextBoxColumn col1 = new DataGridViewTextBoxColumn(); col1.DataPropertyName = "Name"; dataGridView1.Columns.Add(col1); dataGridView1.DataSource = _users; 

Then, when the DataGridView provides a new row, it will have the identifier 0 (the default value for an integer).

Each object that comes from the database has a non-zero identifier.

There might also be a way to achieve this using BindingSources, but I quickly looked through the properties there and nothing came out of me.

+3
source share

What is the data source in this case?

If it is datatable, then you can easily test the DataRowState property in the data rows to see if they are new or existing. After you have checked your datarows, you can then call Accept on the table to commit these rows. In this situation, there is no need to intervene between the grid and its data.

Of course, this does not mean that your data is actually tied to the database, if it is stored there. This will be another step.

In addition, I usually avoid writing directly to a datagridview; instead, I do it read-only and a pop-up add / enter screen pops up that can do any kind of check.

I do not know if this was useful to you - if I missed this question, please let me know.

+3
source share

This should help people who don't use DataTable objects:

In the CellBeginEdit event, set the Tag property to some value that you will use to differentiate the "new" row

 private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e) { if (e.RowIndex == dataGridView1.NewRowIndex) dataGridView1.Rows[e.RowIndex].Tag = true; } 

Then in your checking event you can check this value:

 if (dataGridView1.Rows[e.RowIndex].Tag is bool && (bool) dataGridView1.Rows[e.RowIndex].Tag) { // new row code // after it added, mark it as 'not new' dataGridView1.Rows[e.RowIndex].Tag = false; } else { // existing row code } 
0
source share

All Articles