Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Changes in database and update datagridview problem
 

Changes in database and update datagridview problem

hi,


i have a WinForm datagridview (dgvUser) that is populated with data from a SQLEXPRESS db. On the same form I collect user input using textboxes and after user clicked on OK button, those information will be saved to the database. At this point, everything is perfect. At the same time, I want the dgvUser to update itself with the new info. I have read others' posts regarding this issue and tried some suggestions from posters. From what i see, the datagridview was refreshed but the data wasn't updated. Anyone with solutions or suggestion please help. Million thanks.

Regards,
Sha
shahrul  Wednesday, September 20, 2006 1:54 AM
private void dgvUserUpdate()
{

this.tblUserBindingSource.EndEdit();

dgvUser.DataSource = dmsDataSet1.tblUser;

dgvUser.Refresh();


}

Your dgv has data which is in a virtual memory and you need to re update that data by updating your dataset again.

Public Shared Sub SaveSource(ByRef bs As BindingSource, ByRef dt As DataTable, ByRef da As Object)

Try

bs.EndEdit()

da.Update(dt)

da.Fill(dt)'----- after my update method, i fill my table again.

MsgBox(dt.TableName & " Record(s) saved")

Catch ex As Exception

MsgBox(ex.Message)

da.Fill(dt)

End Try

End Sub

moreover cmd.ExecuteNonQuery(); affected your database directly, leaving your dataset untouched,


hrubesh  Wednesday, September 20, 2006 4:56 AM

To updata database, try following 2 ways, hope it works for you.

this.Validate();

this.customersBindingSource.EndEdit(); this.customersTableAdapter.Update(this.northwindDataSet.Customers);
or something like this:

cm =(CurrencyManager)this.BindingContext[northwindDataSet,"Customers"];

cm.EndCurrentEdit();
Wang Chi  Wednesday, September 20, 2006 2:03 AM

hi wang chi,


I want to update the datagridview, and not the database. I already saved data from user input textboxes directly into the db. All I want is to update/refresh the datagridview with latest row from the db. This is how I add user input data to the db:

SqlConnection con = new SqlConnection("myConnectionString");
string myQuery = "Insert into tblUser values ('" + TextBox1.Text + "'," + "'" + TextBox2.Text + "')";
SqlCommand cmd = new SqlCommand(myQuery, con);

try
{
con.Open();
cmd.ExecuteNonQuery();
}
catch (System.Exception err)
{
//Catch exception here
}
finally
{
dgvUserUpdate();
con.Close();
}

private void dgvUserUpdate()
{

this.tblUserBindingSource.EndEdit();

dgvUser.DataSource = dmsDataSet1.tblUser;

dgvUser.Refresh();


}


Here as you can see, method named dgvUserUpdate() is specially created to update/refresh the datagridview but I can't make it loaded with latest info. I guess there is something wrong with my codes for that method.

shahrul  Wednesday, September 20, 2006 3:39 AM
private void dgvUserUpdate()
{

this.tblUserBindingSource.EndEdit();

dgvUser.DataSource = dmsDataSet1.tblUser;

dgvUser.Refresh();


}

Your dgv has data which is in a virtual memory and you need to re update that data by updating your dataset again.

Public Shared Sub SaveSource(ByRef bs As BindingSource, ByRef dt As DataTable, ByRef da As Object)

Try

bs.EndEdit()

da.Update(dt)

da.Fill(dt)'----- after my update method, i fill my table again.

MsgBox(dt.TableName & " Record(s) saved")

Catch ex As Exception

MsgBox(ex.Message)

da.Fill(dt)

End Try

End Sub

moreover cmd.ExecuteNonQuery(); affected your database directly, leaving your dataset untouched,


hrubesh  Wednesday, September 20, 2006 4:56 AM
Hello,

i am new to the .net world. Was hoping someone can help me. This issue is very urgent for my project.

I am using vb.net 2005 . I have two frames/windows, say F1 and F2 . F1 has a DataGridView ( call it dg) and a button ( call it add_btn) . whenever the user clicks add_btn he gets F2. F2 has couple of text fields and a button. when the user clicks the button it takes all the text fields values and insert them in the data base. I managed to insert the values in the database. All I am trying to do is refresh the data grid in F1.

Here is my code:

Dim dbDs As DataSet = connToDb.recordsFromDB(sqlConn)
F1.dbDG.DataSource = dbDs
Console.WriteLine("number of rows: " & F1.dbDs.Tables(0).Rows.Count) 'we have the right number of rows
F1.DataMember = F1..dbDs.Tables(0).TableName
F1.dbDG.Refresh()

I failed to refresh my datagreidview

Can you please help with this issue. please provide code
gorbag  Sunday, August 23, 2009 6:15 AM

You can use google to search for other answers

Custom Search

More Threads

• Auto general Row ?
• print datagridview cell format
• How do I access my BindingList<T> data?
• On Escape key pressed, exit winform
• How do I validate an dataviewgrid and also allow canceling?
• drag drop cells, dgv to dgv
• dataGridView cellMouseClick event
• Binding object that contains object to DataGridView combobox column
• Remove the X from a from
• how to trigger BindingCompleteState.DataError