Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to auto-select a newly added row in the DataGrid? [modified]

How to auto-select a newly added row in the DataGrid? [modified]

Scheduled Pinned Locked Moved C#
questioncsharpdelphihtmlcom
7 Posts 3 Posters 8 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Donnelly James
    wrote on last edited by
    #1

    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

    D S 2 Replies Last reply
    0
    • D Donnelly James

      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

      D Offline
      D Offline
      Dave Buhl
      wrote on last edited by
      #2

      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.

      D 1 Reply Last reply
      0
      • D Dave Buhl

        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.

        D Offline
        D Offline
        Donnelly James
        wrote on last edited by
        #3

        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#.

        D 1 Reply Last reply
        0
        • D Donnelly James

          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#.

          D Offline
          D Offline
          Dave Buhl
          wrote on last edited by
          #4

          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

          D 1 Reply Last reply
          0
          • D Dave Buhl

            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

            D Offline
            D Offline
            Donnelly James
            wrote on last edited by
            #5

            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.

            D 1 Reply Last reply
            0
            • D Donnelly James

              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.

              D Offline
              D Offline
              Donnelly James
              wrote on last edited by
              #6

              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; } }

              1 Reply Last reply
              0
              • D Donnelly James

                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

                S Offline
                S Offline
                sancyclops
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups