Solved

Capturing row change event using Datagrids in VB.NET

Posted on 2004-04-15
5
1,023 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

680 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