Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# DataGridView Adding a Row - When validating, how do I know the row is new and not yet committed?

I have a client having a problem with a DataGridView in a Windows app. They are calling the CellValidated event, but they want to have different validation for the cell if it is in a row that is already committed back to the datasource than if it is a row that is first being added (and not yet committed (the user hasn't left the row yet). I tried the IsNewRow property, but as soon as you start typing in the row, another "new row" is added, so the row you are working with is no longer considered the new row. I know the row has not been committed yet because you can hit Esc to cancel editing, and the entire row goes away.

Is there a way to tell if the currently edited row is actually the "new row" in the sense that it hasn't been committed back to the datasource?

like image 897
Charles Boyung Avatar asked Feb 03 '10 06:02

Charles Boyung


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


2 Answers

One way I've achieved something similar in the past is to utilise the id property of my objects that are bound to the list.

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

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

I can then bind my list like this:

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 an id of 0 (the default value for an integer).

Every object that comes from the database has a non zero id.

There may also be a way to achieve this using BindingSources but I had a quick look over the properties there and nothing leaped out at me.

like image 82
David Hall Avatar answered Oct 12 '22 23:10

David Hall


What is the data source in this case?

If it's a datatable, then you can easily test the DataRowState property of the datatable's rows to see if they are new or existing. Once you've validated your datarows, you can then call an Accept on the table to commit those rows. There is no need to interfere between the grid and it's datatable, in this situation.

Of course, this doesn't mean your data is actually committed to a database, if that's where it's finally stored. That would be another step.

Also, I usually avoid directly writing to a datagridview; instead, I make it read-only and pop up an add/entry screen which can perform any validation required.

I don't know if any of this has been of use to you - if I've missed the point please let me know.

like image 21
GenericMeatUnit Avatar answered Oct 13 '22 00:10

GenericMeatUnit