Solved

Capturing row change event using Datagrids in VB.NET

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Tool Box 2 35
What .NET URL re-routing tool did I use? 2 37
VB.NET Repostiory Pattern 7 13
VB.NET 2008 Populate DataModel with DataTable 36 7
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

920 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

17 Experts available now in Live!

Get 1:1 Help Now