datagrid selected rows loop
-
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
-
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
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 )
-
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 )
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
-
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
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.noPlease refer to the Forum Guidelines for appropriate posting.