Click here to Skip to main content
15,920,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Actually, when I click on the rows or cells of the datagrid view, they are populating into the text boxes to edit, after I editing and clecked on the update, the datagridview is not changing instantly, if I close and run the form again, it is changing. My requirement is it should change immediately after I click on the update button. The code I am using for update click is:
C#
private void btnUpdate_Click(object sender, EventArgs e)
       {

           SqlConnection con = Helper.getconnection();
           SqlCommand cmd = new SqlCommand();
           cmd.Connection = con;
           cmd.CommandType = CommandType.Text;
           string PrjID = txtPrjID.Text;
           string PrjName = txtPrjNmae.Text;
           string Description = txtPrjdescription.Text;
           string Date = txtPrjDate.Text;
           string Size = txtPrjSize.Text;
           string Manager = txtPrjManager.Text;
           cmd.CommandText = "Update Projects set ProjectName= '" + PrjName + "', Description='" + Description + "', DateStarted='" + Date + "',TeamSize='" + Size + "',Manager='" + Manager + "' where ProjectID= " + PrjID + " ";
           MessageBox.Show("Project Details are updated");
           dataGridView2.Update();
           dataGridView2.Refresh();
           con.Open();
           cmd.ExecuteNonQuery();
           con.Close();

           }
Posted
Updated 9-Sep-13 0:01am
v3
Comments
[no name] 9-Sep-13 5:54am    
use DataBind() method of grid view.
Zubair Alie 9-Sep-13 7:09am    
Get updated result set from database and rebind your grid to display updated records.

Try this

http://www.aspdotnet-suresh.com/2011/02/how-to-inserteditupdate-and-delete-data.html[^]

Add your code inside Onrowupdating Event of gridview.
 
Share this answer
 
Comments
Thanks7872 9-Sep-13 6:00am    
Better if you point to why BindEmployeeDetails(); has been called there after update.
Gitanjali Singh 9-Sep-13 6:03am    
After upadting data inside gridview,we need to bind it to datasource again.
Thanks7872 9-Sep-13 13:09pm    
I know that but i thought it would be better if you update this in your answer.
make ur grid update and refresh after you execute your command


means first execute your command
i.e
cmd.ExecuteNonQuery();


then

then refresh your datagridview
 
Share this answer
 
I have solved the problem by using BindData();
 
Share this answer
 
Call your dataset to update the table at the end or your code on the update button (or wherever to update).
 
Share this answer
 

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