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. Maching the row in a datagrid to a row in a DataTable

Maching the row in a datagrid to a row in a DataTable

Scheduled Pinned Locked Moved C#
questioncss
10 Posts 2 Posters 0 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.
  • S Offline
    S Offline
    stephen woolhead
    wrote on last edited by
    #1

    I have a dataTable set as the Datasource of of a dataGrid via a DataView. The Table contains an ID field and a Description field, of which only the Description is displayed in the grid. I want to be able to programatically retrive the ID from the DataTable based on a row in in the DataGrid. How can I do this, even if the grid has been sorted? Thanks Stephen.

    P 1 Reply Last reply
    0
    • S stephen woolhead

      I have a dataTable set as the Datasource of of a dataGrid via a DataView. The Table contains an ID field and a Description field, of which only the Description is displayed in the grid. I want to be able to programatically retrive the ID from the DataTable based on a row in in the DataGrid. How can I do this, even if the grid has been sorted? Thanks Stephen.

      P Offline
      P Offline
      Paul Riley
      wrote on last edited by
      #2

      If the ID is still bound to a column which is marked as not visible then you should still be able to access it as if it was visible. Paul

      S 1 Reply Last reply
      0
      • P Paul Riley

        If the ID is still bound to a column which is marked as not visible then you should still be able to access it as if it was visible. Paul

        S Offline
        S Offline
        stephen woolhead
        wrote on last edited by
        #3

        Thanks for the quick reply, had over looked that simple idea! Anyway, I would stil like to be able to get back to the row in the dataTable, is it possible? Thanks Stephen.

        P 1 Reply Last reply
        0
        • S stephen woolhead

          Thanks for the quick reply, had over looked that simple idea! Anyway, I would stil like to be able to get back to the row in the dataTable, is it possible? Thanks Stephen.

          P Offline
          P Offline
          Paul Riley
          wrote on last edited by
          #4

          Not sure exactly what you mean. Can you expand on the question a little... give an example if possible. Paul

          S 1 Reply Last reply
          0
          • P Paul Riley

            Not sure exactly what you mean. Can you expand on the question a little... give an example if possible. Paul

            S Offline
            S Offline
            stephen woolhead
            wrote on last edited by
            #5

            OK, say I had code that looked like this.. DataSet ds = GetMyDataSet () ; DataTable dt = ds.Tables["Results"] ; DataView dv = new DataView (dt) ; dv.AllowNew = false ; dataGrid1.DataSource = dv ; And somewhere else I want to delete the current row in response to a button click from the tool bar. I could do.. DataView dv = (DataView )dataGrid1.DataSource ; DataTable dt = dv.Table ; dt.Rows[dg.CurrentCell.RowNumber].Delete ; But if the grid has been sorted by using the column headers this will no longer work as the grid row number no longer matches the row number in the dataTable. What I would like to do is find a way to go from a row in the datagrid to the row in the datasource. The delete is just an example, it is the row mapping that is realy of intreast to me. Thanks Stephen.

            P 1 Reply Last reply
            0
            • S stephen woolhead

              OK, say I had code that looked like this.. DataSet ds = GetMyDataSet () ; DataTable dt = ds.Tables["Results"] ; DataView dv = new DataView (dt) ; dv.AllowNew = false ; dataGrid1.DataSource = dv ; And somewhere else I want to delete the current row in response to a button click from the tool bar. I could do.. DataView dv = (DataView )dataGrid1.DataSource ; DataTable dt = dv.Table ; dt.Rows[dg.CurrentCell.RowNumber].Delete ; But if the grid has been sorted by using the column headers this will no longer work as the grid row number no longer matches the row number in the dataTable. What I would like to do is find a way to go from a row in the datagrid to the row in the datasource. The delete is just an example, it is the row mapping that is realy of intreast to me. Thanks Stephen.

              P Offline
              P Offline
              Paul Riley
              wrote on last edited by
              #6

              You could search through the DataTable for the record with the same ID column. But I feel you're missing some of the power of DataSets. Try Adding a DataSet design to your project, define the layout of your dataset and set the ID as a Primary key. This generates a DataSet-derived class with a lot more power, exposing methods like Search() or FindByXXX() [where XXX is the Primary Key name]. If you then want to duplicate your changes in some sort of database, take a good look at DataAdapters. None of this is simple stuff when you first come to look at it but once you've done it a couple of times, you can start knocking them together in no time. Trust me, it's worth the initial effort. Paul

              S 1 Reply Last reply
              0
              • P Paul Riley

                You could search through the DataTable for the record with the same ID column. But I feel you're missing some of the power of DataSets. Try Adding a DataSet design to your project, define the layout of your dataset and set the ID as a Primary key. This generates a DataSet-derived class with a lot more power, exposing methods like Search() or FindByXXX() [where XXX is the Primary Key name]. If you then want to duplicate your changes in some sort of database, take a good look at DataAdapters. None of this is simple stuff when you first come to look at it but once you've done it a couple of times, you can start knocking them together in no time. Trust me, it's worth the initial effort. Paul

                S Offline
                S Offline
                stephen woolhead
                wrote on last edited by
                #7

                I kept the example short in an attempt to get the point of my question cross, is there a way to map the selected row in a datagrid to the related row in the underlying dataset. I am unable to use DataAdapters as the original datasource is not supported by ADO.NET I could use the Hidden ID column and search by Primary key method to find the row in the dataset, it just seems wrong when there must be a direct link back to the row in the dataset somewhere. The grid must know what row it is displaying to be able to update when the row is changed! Also, on a related note, I want to do the same thing in DataGridColumnStyle derived class that uses columns in the DataTable that are not displayed in the grid. In this the edit and comit methods provide a row number in the datagrid, but I am at a loss on how to map that back into the dataset. Stephen.

                P 1 Reply Last reply
                0
                • S stephen woolhead

                  I kept the example short in an attempt to get the point of my question cross, is there a way to map the selected row in a datagrid to the related row in the underlying dataset. I am unable to use DataAdapters as the original datasource is not supported by ADO.NET I could use the Hidden ID column and search by Primary key method to find the row in the dataset, it just seems wrong when there must be a direct link back to the row in the dataset somewhere. The grid must know what row it is displaying to be able to update when the row is changed! Also, on a related note, I want to do the same thing in DataGridColumnStyle derived class that uses columns in the DataTable that are not displayed in the grid. In this the edit and comit methods provide a row number in the datagrid, but I am at a loss on how to map that back into the dataset. Stephen.

                  P Offline
                  P Offline
                  Paul Riley
                  wrote on last edited by
                  #8

                  stephen woolhead wrote: I could use the Hidden ID column and search by Primary key method to find the row in the dataset, it just seems wrong when there must be a direct link back to the row in the dataset somewhere. There isn't, I'm afraid. DataSets and DataGrids are two completely unrelated things, Daatabinding just allows you to link them. You have to create your own link between records like that. stephen woolhead wrote: I am unable to use DataAdapters as the original datasource is not supported by ADO.NET Not even using ODBC.NET[^]? stephen woolhead wrote: Also, on a related note, I want to do the same thing in DataGridColumnStyle derived class that uses columns in the DataTable that are not displayed in the grid. In this the edit and comit methods provide a row number in the datagrid, but I am at a loss on how to map that back into the dataset. Through a primary key again, I suspect. You're stepping a little beyond my range of experience here though :) Paul

                  S 1 Reply Last reply
                  0
                  • P Paul Riley

                    stephen woolhead wrote: I could use the Hidden ID column and search by Primary key method to find the row in the dataset, it just seems wrong when there must be a direct link back to the row in the dataset somewhere. There isn't, I'm afraid. DataSets and DataGrids are two completely unrelated things, Daatabinding just allows you to link them. You have to create your own link between records like that. stephen woolhead wrote: I am unable to use DataAdapters as the original datasource is not supported by ADO.NET Not even using ODBC.NET[^]? stephen woolhead wrote: Also, on a related note, I want to do the same thing in DataGridColumnStyle derived class that uses columns in the DataTable that are not displayed in the grid. In this the edit and comit methods provide a row number in the datagrid, but I am at a loss on how to map that back into the dataset. Through a primary key again, I suspect. You're stepping a little beyond my range of experience here though :) Paul

                    S Offline
                    S Offline
                    stephen woolhead
                    wrote on last edited by
                    #9

                    Paul Riley wrote: Not even using ODBC.NET[^]? I need to access a password protected Paradox table (not by choice :)), and as far as I know there is no way to do that with ODBC. So I have a Managed C++ dll that uses the BDE directly to build me a Dataset. Paul Riley wrote: stephen woolhead wrote: I could use the Hidden ID column and search by Primary key method to find the row in the dataset, it just seems wrong when there must be a direct link back to the row in the dataset somewhere. There isn't, I'm afraid. DataSets and DataGrids are two completely unrelated things, Daatabinding just allows you to link them. You have to create your own link between records like that. I have fianlly worked it out, looks something like this using the previous delete example... BindingManagerBase cm = this.BindingContext[dg.DataSource] ; DataRowView drv = (DataRowView )cm.Current ; ((DataRow )drv.Row).Delete () ; I found this was a good link if you MSDN installed ms-help://MS.MSDNQTR.2002APR.1033/vbcon/html/vbconConsumersOfDataOnWindowsForms.htm[^] While there is no direct link between the dataSet and the DataGrid, there are container level objects that you can access that are looking after it for you. Anyway, thanks for all your replies, they really helped. Stephen.

                    P 1 Reply Last reply
                    0
                    • S stephen woolhead

                      Paul Riley wrote: Not even using ODBC.NET[^]? I need to access a password protected Paradox table (not by choice :)), and as far as I know there is no way to do that with ODBC. So I have a Managed C++ dll that uses the BDE directly to build me a Dataset. Paul Riley wrote: stephen woolhead wrote: I could use the Hidden ID column and search by Primary key method to find the row in the dataset, it just seems wrong when there must be a direct link back to the row in the dataset somewhere. There isn't, I'm afraid. DataSets and DataGrids are two completely unrelated things, Daatabinding just allows you to link them. You have to create your own link between records like that. I have fianlly worked it out, looks something like this using the previous delete example... BindingManagerBase cm = this.BindingContext[dg.DataSource] ; DataRowView drv = (DataRowView )cm.Current ; ((DataRow )drv.Row).Delete () ; I found this was a good link if you MSDN installed ms-help://MS.MSDNQTR.2002APR.1033/vbcon/html/vbconConsumersOfDataOnWindowsForms.htm[^] While there is no direct link between the dataSet and the DataGrid, there are container level objects that you can access that are looking after it for you. Anyway, thanks for all your replies, they really helped. Stephen.

                      P Offline
                      P Offline
                      Paul Riley
                      wrote on last edited by
                      #10

                      stephen woolhead wrote: I need to access a password protected Paradox table (not by choice ), and as far as I know there is no way to do that with ODBC. Ick! No idea, but I suspect you're right. stephen woolhead wrote: I have fianlly worked it out ...snip...While there is no direct link between the dataSet and the DataGrid, there are container level objects that you can access that are looking after it for you. I'm impressed. Didn't know such a thing existed, thanks. Glad to be of some help (if only pointing your mind in the right direction) and it seems I've learned something too :) Paul

                      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