Reputation: 10805
i have a grid view where each row has a check box so for all those roes which are checked i want to save in data base how can i achieve this?
do i need to perform separate database operation for each datarow or i can send all row to database at once do i need to write some special query?
or i can have some temporary table then i can insert all at once please enlighten me on this
Upvotes: 2
Views: 356
Reputation: 19862
You can use a for statement or a foreach statement to do this.
Iterate through each of your rows
foreach (DataGridViewRow dr in myDataGridView.Rows)
{
if(dr.Cells[0].Value != null) //Cells[0] Because in cell 0th cell we have added checkbox
{
//Do something with the row
}
}
taken from http://www.dotnetspark.com/kb/151-add-checkbox-inside-datagridview-windows.aspx
But if we are talking about more than 100 or 200 rows, you should use a mechanism svanryckeghem has mentioned or a BULK INSERT.
Upvotes: 2
Reputation: 923
It probably depends on what database engine you are using.
If you want to send all the rows at once, SQL Server supports XML input parameters. You could turn the selected rows into an XML document, send that to a stored procedure, and have that procedure insert the rows from there.
SQL Server 2008 even supports table-value parameters (http://msdn.microsoft.com/en-us/library/bb510489.aspx). You could then pass a DataTable object to your stored procedure (http://msdn.microsoft.com/en-us/library/bb675163.aspx).
Upvotes: 1