Solved

Capturing row change event using Datagrids in VB.NET

Posted on 2004-04-15
5
1,016 Views
Last Modified: 2012-05-04
I am getting stumped here.  Silly me, I figured Microsoft would have created a simple event for when a row is selected in a datagrid.

Here is where I am stuck:

I have 2 grids (GRID A and GRID B) on the same form.  GRID A is bound to a table in a dataset.  GRID B is bound to a dataview derrived from a different table in the same dataset.  

Selecting a row in GRID A changes the filter on the dataview which changes the contents of GRID B accoringly.  This works fine.  I get the bindingcontext of the appropriate table that GRID A is bound to and add a handler for the PossitionChanged event.  No problems.

The idea now is that when a row is selected from GRID B, various data entry controls will be updated.  I tried using the same bindingcontext methiod for this but it doesn't seem to be working.  It must have something to do with the fact that I am bound to a dataview and not teh table itself.  How can I capture the row change on GRID B so I can correctly update my controls?

I'm setting the points for this pretty high as I need help as soon as possible.
0
Comment
Question by:T_Herman
  • 3
  • 2
5 Comments
 
LVL 28

Accepted Solution

by:
iboutchkine earned 300 total points
ID: 10836352
To catch the changing of a row, get the binding manager and handle its
PositionChanged event.

Private bindingManager As BindingManagerBase
.....
.....
bindingManager = Me.BindingContext(dataGrid1.DataSource)
AddHandler bindingManager.PositionChanged, AddressOf RowChanged

.....
.....

Private Sub RowChanged(sender As Object, e As System.EventArgs)
       Console.WriteLine(("RowChanged " + bindingManager.Position.ToString()))
       Dim lastRow As Boolean = bindingManager.Count > CType(dataGrid1.DataSource, DataTable).Rows.Count
       If lastRow Then
              Console.WriteLine("lastRow")
       End If
End Sub 'RowChanged

0
 
LVL 28

Expert Comment

by:iboutchkine
ID: 10836366
Another good example

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "
#End Region

    Private WithEvents myDataTable As New DataTable("Test")
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        DataGrid1.DataSource = myDataTable
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        myDataTable.Columns.Add("Forename", System.Type.GetType("System.String"))
        myDataTable.Columns.Add("Status", System.Type.GetType("System.Boolean"))
        myDataTable.Columns.Add("Lastname", System.Type.GetType("System.String"))
        myDataTable.Rows.Add(New Object() {"Mary", True, "Sullivan"})
        myDataTable.Rows.Add(New Object() {"John", False, "Peterson"})
        myDataTable.Rows.Add(New Object() {"Jim", True, "Bergman"})
        myDataTable.AcceptChanges()
    End Sub
    Private Sub myRowChanging(ByVal sender As Object, ByVal e As DataRowChangeEventArgs) Handles myDataTable.RowChanging
        MessageBox.Show("RowChanging")
    End Sub
    Private Sub myColumnChanging(ByVal sender As Object, ByVal e As DataColumnChangeEventArgs) Handles myDataTable.ColumnChanging
        MessageBox.Show("ColumnChanging")
    End Sub

End Class

0
 
LVL 1

Author Comment

by:T_Herman
ID: 10836444
Your second example seems to deal with capturing the event of adding rows or columns.  I'm fine with that.  Your previous example however is what I need.  Not sure how I missed it.  It's basically exactly what I was doing.  I didn't use the grid.source though, I used the actual name of the dataview.

I just have to trap if there are any rows or not.  I'm doing a quick test to be sure and then i'll most likely approve.  Now I wonder if I gave you too many points as I will have more questions and only a few points left.  :)
0
 
LVL 28

Expert Comment

by:iboutchkine
ID: 10836510
If you onky to check are there any rows you can

DataTable.DefaultView.Count - quantity of rows
or
DataSet.Tables("MyTable").DefaultView.Count


another=============
Dim numRows As Integer = _
dataGridDetails.BindingContext(dataGridDetails.DataSource, dataGridDetails.DataMember).Count
0
 
LVL 1

Author Comment

by:T_Herman
ID: 10836591
Yeah, just found those.  Ok, it works.  I have to tweak it a bit to work out just like I need but it seems to be working fine.  Thanks again for the help.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now