How to auto-select a newly added row in the DataGrid? [modified]
-
If anyone is familiar with Delphi and it's TADOQuery component, I would like to do something similar to the TADOQuery.Locate method. If your not familiar with Delphi's TADOQuery.Locate, I want to be able to programatically select a newly added row. So the user can have a visual reference that a post was successful. I read online sometime a ago that the TableAdapter.Select() method was the way to go but I can't seem to get it to work. Anyone? I found this answer here http://www.akadia.com/services/dotnet\_find\_methods.html but it requires your tables to have primary keys and just my luck none of my tables have primary keys. By the way I didn't create these tables. How do I do the find without primary keys? I don't want to have to add them to all my Tables.
modified on Friday, April 16, 2010 4:53 PM
-
If anyone is familiar with Delphi and it's TADOQuery component, I would like to do something similar to the TADOQuery.Locate method. If your not familiar with Delphi's TADOQuery.Locate, I want to be able to programatically select a newly added row. So the user can have a visual reference that a post was successful. I read online sometime a ago that the TableAdapter.Select() method was the way to go but I can't seem to get it to work. Anyone? I found this answer here http://www.akadia.com/services/dotnet\_find\_methods.html but it requires your tables to have primary keys and just my luck none of my tables have primary keys. By the way I didn't create these tables. How do I do the find without primary keys? I don't want to have to add them to all my Tables.
modified on Friday, April 16, 2010 4:53 PM
I am making a couple of assumptions here that may or may not be accurate. 1. Your datastore is a SQL database 2. You have no identity column (primary key is one type of identity column). Without an identity column defined, SQL databases methods to return the last inserted record (@@identity or Scope_Identity()) will return NULL. Soooo you need another way to skin that cat. One way, and there could be many options would be to make a clone of your current datatable before refreshing the data from the database and then compare the records to find the new one. A way to do that would be something like:
datable1.merge(datatable2);
datatable3 = datatable2.GetChanges();Then you could find the row or rows from datatable3 in your table and make that or them your current selection. Hope this is helpful.
-
I am making a couple of assumptions here that may or may not be accurate. 1. Your datastore is a SQL database 2. You have no identity column (primary key is one type of identity column). Without an identity column defined, SQL databases methods to return the last inserted record (@@identity or Scope_Identity()) will return NULL. Soooo you need another way to skin that cat. One way, and there could be many options would be to make a clone of your current datatable before refreshing the data from the database and then compare the records to find the new one. A way to do that would be something like:
datable1.merge(datatable2);
datatable3 = datatable2.GetChanges();Then you could find the row or rows from datatable3 in your table and make that or them your current selection. Hope this is helpful.
It's an SQL Server database and we do have identity columns but they are no primary key columns. I am able to use @@Identity and Scope_Identity() in the Delphi App and it works fine. I just need to figure out how to do it in C#.
-
It's an SQL Server database and we do have identity columns but they are no primary key columns. I am able to use @@Identity and Scope_Identity() in the Delphi App and it works fine. I just need to figure out how to do it in C#.
This may work for you assuming you are dynamically generating your sql and submitting through an ADO query object. Add a statement such as "Select Scope_Identity() From Table MyTable;" to the end of your dynamic sql. Then instead of submitting as ExecuteNonQuery(), run it as a regular query and the return value should be the row you added. ymmv
-
This may work for you assuming you are dynamically generating your sql and submitting through an ADO query object. Add a statement such as "Select Scope_Identity() From Table MyTable;" to the end of your dynamic sql. Then instead of submitting as ExecuteNonQuery(), run it as a regular query and the return value should be the row you added. ymmv
I'm sorry I'm taking so long to reply to this thread, I'm a little busy. Ok so everything you said works but now how do I select the newly added row. I've tried several ways so far. e.g. this.bindingsource.contains(NewID), this.bindingsource.find(NewID). this.bindingsource.FindRows(NewID). But they never select any row except the very first row.
-
I'm sorry I'm taking so long to reply to this thread, I'm a little busy. Ok so everything you said works but now how do I select the newly added row. I've tried several ways so far. e.g. this.bindingsource.contains(NewID), this.bindingsource.find(NewID). this.bindingsource.FindRows(NewID). But they never select any row except the very first row.
After searching long and hard I finally found a way to do it. Makes me wonder why I didn't think of this before. for ( int i = 0; i < GridContact.Rows.Count; i++ ) { if ((int)GridContact.Rows[i].Cells[1].Value == ID ) { GridContact.ClearSelection(); GridContact.Rows[i].Selected = true; } }
-
If anyone is familiar with Delphi and it's TADOQuery component, I would like to do something similar to the TADOQuery.Locate method. If your not familiar with Delphi's TADOQuery.Locate, I want to be able to programatically select a newly added row. So the user can have a visual reference that a post was successful. I read online sometime a ago that the TableAdapter.Select() method was the way to go but I can't seem to get it to work. Anyone? I found this answer here http://www.akadia.com/services/dotnet\_find\_methods.html but it requires your tables to have primary keys and just my luck none of my tables have primary keys. By the way I didn't create these tables. How do I do the find without primary keys? I don't want to have to add them to all my Tables.
modified on Friday, April 16, 2010 4:53 PM
After searching long and hard I finally found a way to do it. Makes me wonder why I didn't think of this before. for ( int i = 0; i < GridContact.Rows.Count; i++ ) { if ((int)GridContact.Rows[i].Cells[1].Value == ID ) { GridContact.ClearSelection(); GridContact.Rows[i].Selected = true; } } http://www.c-sharpcorner.com/Forums/Thread/84582/how-to-auto-select-a-newly-added-row-in-the-datagrid.aspx