DATAGridviews Help related data

scumboss

New member
Joined
Apr 29, 2008
Messages
1
Programming Experience
Beginner
Hi All

I have two DATAgridviews on my form as you can see from the following screenshot when a user navigates through each record in the first DATAgridview I want the second DATAgridview at the bottom to show the corresponding courses using the ApplicationID field. Any ideas? Help much appreciated.

Screenshot: -

24negs5.jpg
 
The quick and dirty way would be to set the RowFilter property of the DataView which is the DataSource of the lower DataGridView.

Use something like this :

VB.NET:
public class Form1 extends Form
    private viewDetails as DataView = nothing

    public sub New() 
        InitializeComponent();

        AddHandler this.Load, new EventHandler(Form1_Load)
        AddHandler binderMain.PositionChanged, new EventHandler(binderMain_PositionChanged)
    end sub

    private sub Form1_Load(sender as Object, e as EventArgs)
        viewDetails = new DataView(tableDetails)
        gridDetails.DataSource = viewDetails
    end sub

    private sub binderMain_PositionChanged(sender as Object, e as EventArgs)

        gridDetails.RowFilter = "ApplicationId = '" + DirectCast(binderMain.Current, DataRowView)("ApplicationId") + "'"
    end sub

    ' Rest of class
end class

In this code,
tableDetails is the DataTable containing your lower DataGridView's data,
binderMain is the BindingSource you are using for the data of your upper DataGridView,
you must replace the first "ApplicationId" by the primary key of your details table and the second by the foreign key of your main table.

If you bind the DataSource of the DataGridView in the Designer, you must get the DataView that is set to its DataSource but I don't remember if it creates a new DataView or if it uses the DataTable's DefaultDataView.

This is only one of the many ways of doing this. If you've done it another way, it's probably ok! :)

I wrote this by memory so if it doesn't work you may have to correct a few typing mistakes.
 
Last edited:

Latest posts

Back
Top