Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how can we update selected row from datagridview and update in database..
.can any one help me
Posted
Updated 9-Jun-11 23:06pm
v2
Comments
Wayne Gaylard 10-Jun-11 5:10am    
What have you tried?
Rob Branaghan 10-Jun-11 5:29am    
Exactly what have you tried?
How about an example?
Sunasara Imdadhusen 10-Jun-11 5:46am    
didn't try!
BobJanova 10-Jun-11 5:54am    
If the DGV is data bound to a DataAdapter this should just work. So you have to be more specific about your problem and what you have tried so far.
Ragi Gopi 10-Jun-11 7:06am    
tried a lot nothing works....
if any one can help..
plz help...

once you set the datagridview properties to allowuser to add rows , update rows add a button to save your changes that have been done in the datagridview such as :
        private void button1_Click(object sender, EventArgs e)
        {

            string connstr = "Data Source=************";
            SqlConnection myconn = new SqlConnection(connstr);
            myconn.Open();
// here we are moving in all the rows 
            foreach (DataGridViewRow dgRow in dataGridView1.Rows)
            {
                string Id = dgRow.Cells[0].Value.ToString();
                int isEdit = Convert.ToInt32(dgRow.Cells[1].Value);
                //bool isDelete = Convert.ToBoolean(dgRow.Cells[1].Value);
                int isDelete = Convert.ToInt32(dgRow.Cells[2].Value);
                // bool  isView= Convert.ToBoolean(dgRow.Cells[4].Value);
                int isADD = Convert.ToInt32(dgRow.Cells[3].Value);
                string strQuery = "update Roles set CanEdit = '" + isEdit.ToString() + "', CanDelete='" + isDelete.ToString() + "', CanAdd='" + isADD.ToString() + "' where ID=" + Id;
                //update database with above query

                SqlCommand cmdSelect = new SqlCommand(strQuery, myconn);
                cmdSelect.ExecuteNonQuery();
            }
            myconn.Close();
        }


hope this will help you ...
regards...
 
Share this answer
 
Comments
RakeshMeena 14-Jun-11 1:03am    
Bad solution. Reasons:
1. You are considering that all the rows are modified which may not be the case.
2. If you have say 10 rows, are you going to call DB 10 times? BAD.
See my answer.
Try this link. This may be in VB.net but it does give an idea as to how to proceed.
 
Share this answer
 
Comments
bkhtyar 19-Jul-12 2:17am    
tank

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