Question Record is added to the tableadapter , but how to refresh the datagridview

Joined
Mar 25, 2009
Messages
23
Programming Experience
Beginner
Hello

if a DataGridView is bounded to a DATASET to display a table , a tableAdapter is Used to Add a Record to the table , how to refresh the contents of the dataGridview to display the table after addition?

this is my code
VB.NET:
Dim nc As New NewCaseDialog
        nc.ShowDialog()
        If nc.DialogResult = Windows.Forms.DialogResult.OK Then
            CasesTableAdapter1.Insert(nc.PhoneNumber, nc.StudentName, nc.Studentbranch, nc.Studentstage, nc.Studentlevel, nc.Studentclass, nc.CallerName, nc.Relation, nc.CaseTitle, nc.StudentSpecialCases, nc.CaseDetails, True, nc.CaseNotes)
            CasesTableAdapter1.Update(GDBDataSet1.Cases)
        End If
        nc = Nothing
DataGridView1.Refresh()

it adds but the datagridview is not refreshed
 
You're going about this the wrong way. You don't use the TableAdapter to add the record directly to the database when you've already got a DataTable. Get rid of that Insert line and instead add a new row to your DataTable. The Update line will then save that new row to the database.
 
yes , thank you , it works

GDBDATAset1.cases.addcasesrow(....)
casestableadapter1.update

Thanks alot
 
Last edited:
why the index is minus?

VB.NET:
GDBDATAset1.cases.addcasesrow(....)
casestableadapter1.update

the row is added, the datagridview is refreshed

but , the table contains an autonumber field which is the pK , this field is one of the displayed fields in the datagridview

after the row is added and the grid is refreshed , this cell value is (-1,-2,...)
the value is correct in the database , if i close the form and open it again , it displays correct values , how could i realy refresh the gridview?
 
You shouldn't be showing the ID in the grid to begin with I don't think. Also, is it really necessary to save the row straight after adding it? Can you not simply allow the changes to accumulate and then save them all as a batch? Maybe, maybe not. It would depend on the circumstances. That said, the answer to your question depends on what database you're using.
 
realy i dont need to display the caseid , but i did because of the following situation:

- if a user double clicks the datagridview , then a new dialog will be shown
- the caseid of the selected row should be sent to the constructor of the new dialog
- the only way to do that (as i know) is to send the value of the first cell of the selected row
VB.NET:
Dim cdd As New CaseDetailsDialog(CLng(DataGridView1.SelectedRows(0).Cells(0).Value))

- if another way is known , it will be great , i dont need to display the caseid

- I use Ms Access 2007
 
You should be binding your data to the grid through a BindingSource. When the user selects a row in the grid you can get the bound DataRowView from the BindingSource's Current property. You can then get the ID from that:
VB.NET:
Dim id As Integer = CInt(DirectCast(myBindingSource.Current, DataRowView)("ID"))
 
Back
Top