Solved

Capturing row change event using Datagrids in VB.NET

Posted on 2004-04-15
5
1,025 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
reading excel file in .net 2 46
Asp.Net Session Question 2 49
Easiest intro into .NET CORE? 2 32
Unable to Install .NET 3.5 on Windows 2012 server 4 14
IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
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…
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

739 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