Adding a new row to a datagrid bound to a dataset.

G

Guest

Hello,
I have a problem whereby I have a datagrid and and extra field all bound to the same dataset. I can easily edit rows in the dataset by changing the bindingposition in the MyGrid_currentcellchanged event of Mygrid, using the following function


Private Function GetBindingPosition(ByVal IntMatchValue As Integer, ByVal MyMatchField As String, ByVal MyDataTable As DataTable) As Integer
Dim i As Integer = 0
Dim blnValueFnd As Boolean

While (i <= (MyDataTable.Rows.Count - 1)) And Not blnValueFnd
If CType(MyDataTable.Rows(i).Item(MyMatchField), Integer) = IntMatchValue Then
blnValueFnd = True
Return i
Else
i = i + 1
End If
End While
End Function

..This gets the position in the dataset of a keyfield and thus sets the dataset binding position to this value and thus the extra field displays the relevant value.

The problem occurs when I wish to add a row, I can't find the binding position of therow because it does not exist in the dataset yet. If I update it b4 setting the binding position then it uses the previous binding position. The extra field cannot be part of the datagrid because it is too large and looks better kept seperate.

Any ideas would be appreciated.

Geri
 
C

Cor Ligthert

Hi Geraldine,

I am not sure if I understand your problem.

However yesterday I made and sended a sample what looks if it goes about
your problmem.

Have a look to it, it is not your solution with this I think you can get
your solution..

Cor

\\\
Private WithEvents dv As DataView
Private dt As New DataTable
Private Sub Form7_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
dt.Columns.Add("Wayne")
dt.Columns.Add("Ken")
For i As Integer = 0 To 4
Dim dr As DataRow = dt.NewRow
dr(0) = (i + 1).ToString
dr(1) = Chr(i + 65)
dt.Rows.Add(dr)
Next
dv = New DataView(dt)
Me.DataGrid1.DataSource = dv
End Sub

Private Sub dv_ListChanged(ByVal sender As Object, _
ByVal e As System.ComponentModel.ListChangedEventArgs) Handles
dv.ListChanged
If dt.Rows.Count < dv.Count Then
MessageBox.Show("New Row " & dv.Count.ToString)
End If
End Sub
End Class
///
 

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