Click here to Skip to main content
15,567,415 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I have a column named Status inside a datagridview, now every row has a status of 1, i want to update them to 2. Here is the code i use
C#
 private void btnCommit_Click(object sender, EventArgs e)
{
    using (DataClasses2DataContext db = new DataClasses2DataContext())
    {
        if (MessageBox.Show("Do you want to commit it?","Yes",MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
        {
            int i = dgvDisplay.CurrentRow.Index;
            ID = dgvDisplay[7, i].Value.ToString();
                  
            db.CCSDD_StatusUpdate(2, ID);
            MessageBox.Show("Your Status has been updated!");
        }               
    }         
}

This works okay, however, this only updates one at a time.

Also i used a stored procedure to update a certain column
ALTER PROCEDURE [dbo].[CCSDD_StatusUpdate] 
	-- Add the parameters for the stored procedure here
	@status int,
	@id varchar(20)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	update CCSDD_StoreDirectDelivery1 set Status = @status 
	where DR# = @id
END


can anyone help me on how to update multiple rows?

What I have tried:

C#
 private void btnCommit_Click(object sender, EventArgs e)
{
    using (DataClasses2DataContext db = new DataClasses2DataContext())
    {
        if (MessageBox.Show("Do you want to commit it?","Yes",MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
        {
            int i = dgvDisplay.CurrentRow.Index;
            ID = dgvDisplay[7, i].Value.ToString();
                  
            db.CCSDD_StatusUpdate(2, ID);
            MessageBox.Show("Your Status has been updated!");
        }               
    }         
}
.
Posted
Updated 6-Nov-22 15:00pm
v2
Comments
Richard MacCutchan 25-Oct-22 3:45am    
It all depends on what you are trying to update them with. The obvious answer is to use a loop of some sort, but that may not apply to your specific update conditions.
Cruz Vince 25-Oct-22 4:21am    
I've been stuck on this problem for like a week now, hopefully someone will be able to provide a clear solution to this. But thanks on the suggestion!
Richard MacCutchan 25-Oct-22 4:30am    
You need to provide more information in your question. As I said, a simple loop can be used to process all the rows, but we do not know exactly what you want to update them with.
Cruz Vince 25-Oct-22 4:45am    
oh my bad, i have a column named Status. Now every row has a status of 1. I want to update them to 2. Based on my code above, it is working. But it cannot update multiple rows in one go.
Richard MacCutchan 25-Oct-22 4:50am    
You have two possibilities (or maybe three). Tell the user to select all rows that require updating and get the list of selected rows from the DGV, and update each of them. Loop through all rows and update all those with some specific condition. Loop through all rows and update them all. Only you can decide which way will fulfil your requirements.

1 solution

Here is the answer

C#
for (int i = 0; i < dgvDisplay.SelectedRows.Count; i++) 
         {
            int x = dgvDisplay.SelectedRows[i].Index;
                        

            string ID = dgvDisplay[9, x].Value.ToString();
            db.CCSDD_StatusUpdate(2, ID);
         }

           MessageBox.Show("Your Status has been updated!","Done",MessageBoxButtons.OK,MessageBoxIcon.Information); 



The 9 is where the column is.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900