Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Capturing row change event using Datagrids in VB.NET

Posted on 2004-04-15
5
Medium Priority
?
1,029 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 1200 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

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…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Screencast - Getting to Know the Pipeline
Suggested Courses

916 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