Datagrid & Datatable

E

elziko

I have a DataTable bound to a DataGrid. When a user clicks on a row on the
DataGrid I use:

MyDataTable.Rows(MyDatagrid.CurrentRowIndex).Item(0)

to do some work with the value in the first cell for the currently selected
row. However, when I delete a row (using the delete key oin the grid) and
try and do the above on the row that has now taken the deleed roe's position
in the grid I get the following exception:

An unhandled exception of type 'System.Data.DeletedRowInaccessibleException'
occurred in system.data.dll
Additional information: Deleted row information cannot be accessed through
the row.

I also note that the Rows.Count of the data table remains the same even
after I have used the delete key to delete a row. I think they must be
connected issues. Any ideas on what I should do?
 
C

Cor Ligthert

Hi Elziko,

With what you now are doing goes everything wrong when the user starts to
sort the grid.

Maybe this sample I made about a week ago will help you.

(You have instead of the listview to use the datagrid and no
displaymember).

Cor
\\\\
Private Sub FillDatasetAndBindings()
ds.Clear()
Dim sqlString As String = "Select * from countries"
da = New OleDb.OleDbDataAdapter(sqlString, conn)
da.Fill(ds)
Dim dt As DataTable = ds.Tables(0)
dv = New DataView(dt)
dv.Sort = "Id"
cma = DirectCast(BindingContext(dv), CurrencyManager)
Dim cmb As New OleDb.OleDbCommandBuilder(da)
TextBox1.DataBindings.Clear()
TextBox2.DataBindings.Clear()
Dim mybinding As Binding = New Binding("text", dv, "Id")
TextBox1.DataBindings.Add(mybinding)
TextBox2.DataBindings.Add("text", dv, "Name")
ListBox1.DataSource = dv
ListBox1.DisplayMember = "Name"
If ds.Tables(0).Rows.Count = 0 Then
cma.AddNew()
TextBox1.Focus()
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
cma.AddNew()
TextBox1.Focus()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
dv(cma.Position).Delete()
End Sub
Private Sub Button3_Click_1(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button3.Click
cma.EndCurrentEdit()
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button4.Click
cma.EndCurrentEdit()
If ds.HasChanges Then
Try
da.Update(ds)
Catch ex As OleDb.OleDbException
MessageBox.Show(ex.Message, "OleDbException")
Exit Sub
Catch ex As Exception
MessageBox.Show(ex.Message, "GeneralException")
Exit Sub
End Try
End If
End Sub
///
 
E

elziko

Thanks for your reply.
(You have instead of the listview to use the datagrid and no
displaymember).

Its not a sorting problem but I suspect my problem may get worse with
sorting. Are you suggesting that I use a listview? I need to use the editing
& fomatting functionalty provided by the data grid. I then use the u[adte
method on a data adapter to persist the data back to a database after any
deletes, uppdates or inserts have taken place.

Surely what I'm doing is exactly what you should be able to to with a
DataAdapter, DataTable & DataGrid?

I think an aswer to the following question owuld help me:

After a user has selected a row in a DataGrid how go you get the data stored
in the respective row from the DtaaTable?
 
C

Cor Ligthert

Its not a sorting problem but I suspect my problem may get worse with
sorting. Are you suggesting that I use a listview? I need to use the editing
& fomatting functionalty provided by the data grid. I then use the u[adte
method on a data adapter to persist the data back to a database after any
deletes, uppdates or inserts have taken place.

No in the sample you can change the listbox by the datagrid that is all.
You can keep using the datagrid.
Surely what I'm doing is exactly what you should be able to to with a
DataAdapter, DataTable & DataGrid?

I think an aswer to the following question owuld help me:

After a user has selected a row in a DataGrid how go you get the data stored
in the respective row from the DtaaTable?
That goes when there is a row change, when you see my sample with the
currencymanager you can force that with a cma.endcurrentedit.

(bindingcontext(TheDataSource).endcurrentedit works as well).

I hope this makes it more clear.

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top