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. datagrid selected rows loop

datagrid selected rows loop

Scheduled Pinned Locked Moved C#
databasetutorial
4 Posts 3 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.
  • J Offline
    J Offline
    jnallen
    wrote on last edited by
    #1

    i need to get a cell value from each of the selected datagrid rows. i will then put these values in a t-sql delete from statement. i can't seem to find any clear direction on how to loop through a selected set in a datagrid and then get a particular cell value. any re-direction, or comments would be greatly appreciated.

    look out, noob wreck'n havoc

    C 1 Reply Last reply
    0
    • J jnallen

      i need to get a cell value from each of the selected datagrid rows. i will then put these values in a t-sql delete from statement. i can't seem to find any clear direction on how to loop through a selected set in a datagrid and then get a particular cell value. any re-direction, or comments would be greatly appreciated.

      look out, noob wreck'n havoc

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      I believe it's a case of doing something like foreach(DataGridRow row in grid.Rows) { string field = row["columnName"].ToString(); string orThisWay = row[0].ToString(); } You can cast to string, if it's a string, or use the 'as' operator ( better ), but tostring will always give you some sort of string.

      Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

      J 1 Reply Last reply
      0
      • C Christian Graus

        I believe it's a case of doing something like foreach(DataGridRow row in grid.Rows) { string field = row["columnName"].ToString(); string orThisWay = row[0].ToString(); } You can cast to string, if it's a string, or use the 'as' operator ( better ), but tostring will always give you some sort of string.

        Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

        J Offline
        J Offline
        jnallen
        wrote on last edited by
        #3

        I can't use DataGridRow due to the protection level. I did try this though, which works for a single selected row (now to iterate this through all the selected rows???): int delRow = (int)dataGrid.CurrentCell.Value; _connStr = this.getDBConnection("net"); SqlConnection conn = new SqlConnection(_connStr); string sql = "delete from gisLinkedDocument where LinkKey in (@delRow)"; SqlCommand cmd = new SqlCommand(sql, conn); SqlParameter parm = cmd.Parameters.Add("@delRow", SqlDbType.Int); parm.Value = delRow; try { conn.Open(); cmd.ExecuteNonQuery(); } catch { MessageBox.Show("The link was NOT deleted"); } finally { if (conn.State != ConnectionState.Closed) conn.Close(); }

        look out, noob wreck'n havoc

        L 1 Reply Last reply
        0
        • J jnallen

          I can't use DataGridRow due to the protection level. I did try this though, which works for a single selected row (now to iterate this through all the selected rows???): int delRow = (int)dataGrid.CurrentCell.Value; _connStr = this.getDBConnection("net"); SqlConnection conn = new SqlConnection(_connStr); string sql = "delete from gisLinkedDocument where LinkKey in (@delRow)"; SqlCommand cmd = new SqlCommand(sql, conn); SqlParameter parm = cmd.Parameters.Add("@delRow", SqlDbType.Int); parm.Value = delRow; try { conn.Open(); cmd.ExecuteNonQuery(); } catch { MessageBox.Show("The link was NOT deleted"); } finally { if (conn.State != ConnectionState.Closed) conn.Close(); }

          look out, noob wreck'n havoc

          L Offline
          L Offline
          Larantz
          wrote on last edited by
          #4

          I belive you can do something according to:

          List<int> selectedRowIndexes = new List<int>();

          foreach(DataGridViewRow row in dataGrid.SelectedRows)
          {
          selectedRowIndexes.Add(row.Index);
          }

          //To save bandwith and cputime you should make one query for deleting all the rows
          //instead of deleting one at a time.
          ... code for deleting the indexes ...

          Happy coding! -Larantz- -- modified at 18:00 Friday 7th September, 2007 Fixed missing <> for the generic list.

          for those about to code, we salute you
          http://www.itverket.no

          Please refer to the Forum Guidelines for appropriate posting.

          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