0

i have a data grid view and details for a particular table.i update the details, But it doesn't get updated in the data grid view?my update statement works properly because, once i exit the project, it gets updated.

cmd.CommandText = "update emp_tbl set sal= '" & SalTextBox.Text & "' where id='" & id & "'"

table name: emp_tbl

datagridview: emp_tbldatagridview

update button :

cn.Open()

cmd.CommandText = "update emp_tbl set sal= '" & SalTextBox.Text & "' where id='" & id & "'"

cmd.ExecuteNonQuery()
cn.Close()
aksu
  • 5,221
  • 5
  • 24
  • 39
Ahla
  • 1
  • 1
  • 1
  • 5

2 Answers2

3

You have to re-bind it:

     BindingSource binding = new BindingSource(); //req. by win forms
     DataTable dt = new DataTable();
     dt.Load(sql_command.ExecuteReader());
     dgv.DataSource = dt;

This is the best way I've found to do it in win forms, .update doesn't work because it needs to actually re-pull the data from SQL.

RandomUs1r
  • 4,010
  • 1
  • 24
  • 44
  • Where do i write this coding? If i write it in a button named refresh, will it work? – Ahla Mar 05 '13 at 18:18
  • Yep, wherever you want to refresh the dgv. Adjust your object names appropriately. I use it on my submit button to reload the page with updated values. – RandomUs1r Mar 05 '13 at 18:20
  • Thanks a lot!! Will check and let you know! – Ahla Mar 05 '13 at 18:21
  • Do i need to import anything? – Ahla Mar 05 '13 at 18:22
  • If you are free, Could you pls explain to me just roughly what this code means? i replaced the code with this, where emp_tblbindingsource is my binding source! ` Emp_tblBindingSource = new BindingSource(); //req. by win forms` but*;* is it right? to use ; ? – Ahla Mar 05 '13 at 18:26
  • If you could pass your mail id, i could mail to you my print screens!! Because coding is big – Ahla Mar 05 '13 at 19:25
  • or mine is vbstudent2013@gmail.com – Ahla Mar 05 '13 at 19:31
  • its ok! i am sorry, i am doing a project so i needed to view this way with datagridview updated... Thanks for the help – Ahla Mar 05 '13 at 21:19
0

Just try to reuse your startup code in datagridview or recall the startup code from the data. It was the simple way at all. Cause your startup code is for binding your database to your datagridview. So, everytime you save, your code just save it and not work for rebind it again. So, what you need just try to rebind by recall the startup code

Kasnady
  • 2,249
  • 3
  • 25
  • 34
  • the datagridview shows the updated value only after i end my project! – Ahla Mar 07 '13 at 10:18
  • From reopen the project? is like that... then that is what i told about.. just try to recall your first startup code.. cause that is reload code.. – Kasnady Mar 08 '13 at 01:09
  • Because when i END my project and again run it, The values in my data grid view are already updated! – Ahla Mar 08 '13 at 09:42
  • first startup code means? my first form that shows when i run my project? – Ahla Mar 08 '13 at 09:43
  • Just like random said, that is what i mean. – Kasnady Mar 11 '13 at 01:40
  • what code did you use when you run your project? of course the code your use to run your project is calling your database to your project, isnt it? Then that is what i said your startup code, just reuse it again for the button to update. – Kasnady Mar 11 '13 at 06:35