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. Database & SysAdmin
  3. Database
  4. Getting underlying table-column value from a selected DataGridRow [Solved]

Getting underlying table-column value from a selected DataGridRow [Solved]

Scheduled Pinned Locked Moved Database
databasevisual-studiocsharpdesignregex
6 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.
  • A Offline
    A Offline
    AussieLew
    wrote on last edited by
    #1

    (Posted on c# forum - better suited to DB) I have an Access db with a number of related tables. Some tables have a boolean column, eg HTReadOnly, to determine whether it is a system row (true) or not (false). If HTReadOnly is true the user cannot edit the table row. In a databound DataGridView, if the user chooses to Edit a row I need to check the underlying table column value, HTReadOnly for the current row and allow editing if false. My current solution, using static methods, although reusable for different DataGridViews, requires some manual checking of design time values to protect against run time errors. eg. DataRow,column name and column type are valid etc If I could get the row index of the table row matching the DataGridViewRow I could use...

    // int pos is the required table row index matching the selected DatGridRow

    if(!MyDataSet.MyTable1[pos].HTReadOnly ) { //Allow editing of the current row }
    else { // Don't allow editing };

    The benefit of this method (in my mind) is the IDE and intellisense will provide the table names and check the type etc. So I know they are correct. I tried to get pos from....

    int pos = MyTable1bindingSource.Position;

    however this seems to return the position of the row in the DataGridView not the table, as it doesn't match the table row when DGV sorted. I can't seem to find a method or property for either, DataGridView, bindingSource, or TableAdapter that will return the record row index matching the currently selected DataGridViewRow. Any Ideas? Should I just be content getting the value of the DataGridView current row cell for my boolean value? I read somewhere that it is better to get the actual data rather than the data loaded into the DGV, not sure why? My current method utilizes various statics methods with DataGridViewRow dgvr = dgv.CurrentRow; DataRowView drv = (DataRowView)dgvr.DataBoundItem; to be able to get the column value eg retVal = (bool)drv.Row[colName] after various tests. Any help would be appreciated. Thanks...

    modified on Wednesday, January 13, 2010 1:54 AM

    M 1 Reply Last reply
    0
    • A AussieLew

      (Posted on c# forum - better suited to DB) I have an Access db with a number of related tables. Some tables have a boolean column, eg HTReadOnly, to determine whether it is a system row (true) or not (false). If HTReadOnly is true the user cannot edit the table row. In a databound DataGridView, if the user chooses to Edit a row I need to check the underlying table column value, HTReadOnly for the current row and allow editing if false. My current solution, using static methods, although reusable for different DataGridViews, requires some manual checking of design time values to protect against run time errors. eg. DataRow,column name and column type are valid etc If I could get the row index of the table row matching the DataGridViewRow I could use...

      // int pos is the required table row index matching the selected DatGridRow

      if(!MyDataSet.MyTable1[pos].HTReadOnly ) { //Allow editing of the current row }
      else { // Don't allow editing };

      The benefit of this method (in my mind) is the IDE and intellisense will provide the table names and check the type etc. So I know they are correct. I tried to get pos from....

      int pos = MyTable1bindingSource.Position;

      however this seems to return the position of the row in the DataGridView not the table, as it doesn't match the table row when DGV sorted. I can't seem to find a method or property for either, DataGridView, bindingSource, or TableAdapter that will return the record row index matching the currently selected DataGridViewRow. Any Ideas? Should I just be content getting the value of the DataGridView current row cell for my boolean value? I read somewhere that it is better to get the actual data rather than the data loaded into the DGV, not sure why? My current method utilizes various statics methods with DataGridViewRow dgvr = dgv.CurrentRow; DataRowView drv = (DataRowView)dgvr.DataBoundItem; to be able to get the column value eg retVal = (bool)drv.Row[colName] after various tests. Any help would be appreciated. Thanks...

      modified on Wednesday, January 13, 2010 1:54 AM

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      ok, I'm game to take a shot at this, IMHO your design of allowing the user to edit into the DGV is wrong, in all but the most trivial cases I force the user to double click the list control item (DGV, listview etc) and pop a dialog that allows the crud work to be done on the single row. Trap the results from the dialog and reload the list control if the data has been modified/deleted. The double click event will give you the id from the datarow. Having said that have you tried the cellformat event to deal with each row as it is displayed. DGV has a myriad of event and you need to get the one that will allow you to identify the row and make it read only before the user gets at it. Search for alternate row colouring and you will see the events that can be adopted for this. I assume your Boolean flag is in the DGV, probably a hidden column!

      Never underestimate the power of human stupidity RAH

      A 1 Reply Last reply
      0
      • M Mycroft Holmes

        ok, I'm game to take a shot at this, IMHO your design of allowing the user to edit into the DGV is wrong, in all but the most trivial cases I force the user to double click the list control item (DGV, listview etc) and pop a dialog that allows the crud work to be done on the single row. Trap the results from the dialog and reload the list control if the data has been modified/deleted. The double click event will give you the id from the datarow. Having said that have you tried the cellformat event to deal with each row as it is displayed. DGV has a myriad of event and you need to get the one that will allow you to identify the row and make it read only before the user gets at it. Search for alternate row colouring and you will see the events that can be adopted for this. I assume your Boolean flag is in the DGV, probably a hidden column!

        Never underestimate the power of human stupidity RAH

        A Offline
        A Offline
        AussieLew
        wrote on last edited by
        #3

        Thanks for your reply! To expand a bit further... I have set up the DGV so that no edits , updates etc can be done in the DGV. So the whole DGV is ReadOnly. I am providing the edit/add fields ( as databound textboxes etc )separately elsewhere on the form. These normally display a bit more info than the DGV itself, and are all ReadOnly unless an Edit button (or row double click - yet to be done) is clicked. If the Boolean flag (as you said,in a hidden column) allows edits, I change all the appropriate TextBox.ReadOnly values to allow changes there. (There are some rows that I don't want the user ever to edit or delete) **(1)**So, if it is considered to be best practice to get the column value from the table itself rather than the DGV , I need to get the row index of the table that matches the current row in the DGV. (2) Also being able to use MyDataSet.MyTable1[pos].HTReadOnly ) syntax allow for error checking in the IDE in case of wrong column name or wrong type etc Otherwise I can get the DGV row/column value from ( off the top of my head) with

        DataGridViewRow dgvr = table1DataGridView.CurrentRow;
        DataRowView drv = (DataRowView )dgvr.DataBoundItem;
        bool isReadOnly = drv.Row["htreadonly"];

        By using drv.Row["htreadonly"]; am I in fact looking directly at the data? If so it leaves only point (2) which I think is significant instead of coding (as at present) to catch runtime errors. Hope this serves to clarify, not confuse.... LJL

        M 1 Reply Last reply
        0
        • A AussieLew

          Thanks for your reply! To expand a bit further... I have set up the DGV so that no edits , updates etc can be done in the DGV. So the whole DGV is ReadOnly. I am providing the edit/add fields ( as databound textboxes etc )separately elsewhere on the form. These normally display a bit more info than the DGV itself, and are all ReadOnly unless an Edit button (or row double click - yet to be done) is clicked. If the Boolean flag (as you said,in a hidden column) allows edits, I change all the appropriate TextBox.ReadOnly values to allow changes there. (There are some rows that I don't want the user ever to edit or delete) **(1)**So, if it is considered to be best practice to get the column value from the table itself rather than the DGV , I need to get the row index of the table that matches the current row in the DGV. (2) Also being able to use MyDataSet.MyTable1[pos].HTReadOnly ) syntax allow for error checking in the IDE in case of wrong column name or wrong type etc Otherwise I can get the DGV row/column value from ( off the top of my head) with

          DataGridViewRow dgvr = table1DataGridView.CurrentRow;
          DataRowView drv = (DataRowView )dgvr.DataBoundItem;
          bool isReadOnly = drv.Row["htreadonly"];

          By using drv.Row["htreadonly"]; am I in fact looking directly at the data? If so it leaves only point (2) which I think is significant instead of coding (as at present) to catch runtime errors. Hope this serves to clarify, not confuse.... LJL

          M Offline
          M Offline
          Mycroft Holmes
          wrote on last edited by
          #4

          I am not sure I agree with (1) that you should be getting the data from the underlying datatable, I don't distinguish between the 2 as a datasource for editing. Then again I never edit into the underlying datasource.

          AussieLew wrote:

          I am providing the edit/add fields ( as databound textboxes etc )separately elsewhere on the form

          When you get he data for this do you use the DGV row or go back to the database get a row to bind to the textboxes.

          Never underestimate the power of human stupidity RAH

          A 1 Reply Last reply
          0
          • M Mycroft Holmes

            I am not sure I agree with (1) that you should be getting the data from the underlying datatable, I don't distinguish between the 2 as a datasource for editing. Then again I never edit into the underlying datasource.

            AussieLew wrote:

            I am providing the edit/add fields ( as databound textboxes etc )separately elsewhere on the form

            When you get he data for this do you use the DGV row or go back to the database get a row to bind to the textboxes.

            Never underestimate the power of human stupidity RAH

            A Offline
            A Offline
            AussieLew
            wrote on last edited by
            #5

            Mycroft Holmes wrote:

            I am not sure I agree with (1) that you should be getting the data from the underlying datatable,

            You are probably right, unless there is a reason they may not always match? Something I read somewhere that made sense at the time! I guess also that this would become more unwieldy in the case of a DGV based on multi table queries? (That one is coming up soon..)

            Mycroft Holmes wrote:

            When you get he data for this do you use the DGV row or go back to the database get a row to bind to the textboxes.

            The textboxes etc are bound to the same bindingSource as the DGV's DataSource and change as the DGV row is selected etc. I worked out how I can do what I (feel I) want to do. I needed to go one more level and get the DataRow.

            // This can probably be more elegant/streamlined however it helps me visually. (Open to suggestions..)
            DataGridViewRow dgvr = MyTable1DataGridView.CurrentRow; // returns a DatGridViewRow
            DataRowView drv = (DataRowView)dgvr.DataBoundItem; // Cast as DataRowView - dgvr.DataBoundItem returns an object
            DataRow dr = (DataRow)drv.Row;
            int pos = dr.Table.Rows.IndexOf(dr);
            bool boolColumnValue = MyDataSet.MyTable1[pos].HTReadOnly;// IDE checks and suggests
            // so no need to test that column name or column type is correct

            if (boolColumnValue)
            {
            // Do stuff if true
            }
            else
            {
            // Do NOT do stuff if false
            }

            Ah...It's been an interesting exercise, learnt a lot..There's nothing like a real life project for giving incentive to learn. Now all I need to determine is the "best" way to do the updates and deletes etc. There seems to be many classes where this can done. Thanks for your help, it's been fun! LJL

            M 1 Reply Last reply
            0
            • A AussieLew

              Mycroft Holmes wrote:

              I am not sure I agree with (1) that you should be getting the data from the underlying datatable,

              You are probably right, unless there is a reason they may not always match? Something I read somewhere that made sense at the time! I guess also that this would become more unwieldy in the case of a DGV based on multi table queries? (That one is coming up soon..)

              Mycroft Holmes wrote:

              When you get he data for this do you use the DGV row or go back to the database get a row to bind to the textboxes.

              The textboxes etc are bound to the same bindingSource as the DGV's DataSource and change as the DGV row is selected etc. I worked out how I can do what I (feel I) want to do. I needed to go one more level and get the DataRow.

              // This can probably be more elegant/streamlined however it helps me visually. (Open to suggestions..)
              DataGridViewRow dgvr = MyTable1DataGridView.CurrentRow; // returns a DatGridViewRow
              DataRowView drv = (DataRowView)dgvr.DataBoundItem; // Cast as DataRowView - dgvr.DataBoundItem returns an object
              DataRow dr = (DataRow)drv.Row;
              int pos = dr.Table.Rows.IndexOf(dr);
              bool boolColumnValue = MyDataSet.MyTable1[pos].HTReadOnly;// IDE checks and suggests
              // so no need to test that column name or column type is correct

              if (boolColumnValue)
              {
              // Do stuff if true
              }
              else
              {
              // Do NOT do stuff if false
              }

              Ah...It's been an interesting exercise, learnt a lot..There's nothing like a real life project for giving incentive to learn. Now all I need to determine is the "best" way to do the updates and deletes etc. There seems to be many classes where this can done. Thanks for your help, it's been fun! LJL

              M Offline
              M Offline
              Mycroft Holmes
              wrote on last edited by
              #6

              Your challenge now is to get the bool check to one line/statement. Breaking the steps up is the correct way to go when understanding the process of achieving a goal, well done. Then finding and understanding the correct syntax that achieves the 5 lines of code in 1 statement is the real bonus :) !

              Never underestimate the power of human stupidity RAH

              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