Send a suggestion!

We're building a brand new version of the site, and we'd love to hear your ideas

Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

[4491] Winforms Data Binding Lessons Learned

Last post 05-22-2007 5:25 AM by davidhere40. 7 replies.
Page 1 of 1 (8 items)
Sort Posts: Previous Next
  • 01-01-1999 12:00 AM

    [4491] Winforms Data Binding Lessons Learned

    This thread is for discussions of Winforms Data Binding Lessons Learned.

    • Post Points: 35
  • Advertisement

    • Red Gate Software

    Advertisement

    Want to boost your .NET application performance?

    Some developers always seem to write efficient and lightening-fast code. What is their secret? It’s ANTS Profiler. “We improved the performance of the application up to 10 times” Dan Ports, Intrigma.

    Try it for yourself now.

  • 03-04-2004 7:41 AM In reply to

    • MODINNE
    • Not Ranked
    • Joined on 03-04-2004
    • New Member
    • Points 5

    great

    I think the program is cool and understanding
    • Post Points: 0
  • 07-13-2005 10:11 PM In reply to

    • grom
    • Not Ranked
    • Joined on 07-13-2005
    • New Member
    • Points 30

    your dataview index does not sync with cm index

    I tried your code but the dv index is out of sync with the bm index. Please explain. You can email me at glenn_r@shaw.ca.


    Here my challenge. I have reference to a datarow in a datatable. I want the set the bindingcontext position to that datarow. How can I do this? When I sort the dataview it goes out of sync with the datatable index returned when I use the dv find method.

    Thanks,
    Glenn
    • Post Points: 10
  • 07-27-2005 2:46 PM In reply to

    • Jo_dc
    • Not Ranked
    • Joined on 07-27-2005
    • New Member
    • Points 5
    try  
    Private view As  DataView = m_ds.Tables("Stuff").DefaultView

    But anyway I think that the trick is that the field ID is PK
    • Post Points: 0
  • 09-24-2005 6:54 PM In reply to

    • jmeile
    • Not Ranked
    • Joined on 07-26-2005
    • New Member
    • Points 5

    You save me from getting crazy

    Well, not really. I'm exagerating a litte bit

    Anyway, I just want to thank you for this nice and realy usefull article.
    I not only got the relationships shown in a DataGrid, but also I could
    modify them through the DataSet.
    • Post Points: 0
  • 03-31-2006 2:50 AM In reply to

    • dloiac1
    • Not Ranked
    • Joined on 03-31-2006
    • United States
    • New Member
    • Points 5

    Re: [4491] Winforms Data Binding Lessons Learned

    I'm getting this error message: "Object reference not set to an instance of an object." when I try to run the code below. I've spent a couple days trying to figure out what it is, but I'm kind of new to vb.net and programming. I have two tables that look something like below (not all fields are shown). They should be related via the userID fields as a "many tasks to each user" relation. Any help on how to do this or why there is an error when I run the code would be greatly appreciated.

     

    Table: Users                        Table:Tasks                  

    userID                                 userID

    userName                           taskID

    password                           taskName

     

    Dim

    connection As New OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0; data source=" & Application.StartupPath & "\blueteam2.mdb")

    Dim adpUsers As New OleDbDataAdapter("SELECT * FROM Users,Tasks,Projects", connection)

    Dim ds As New DataSet

    Try

    adpUsers.Fill(ds, "Users")

    ds.Relations.Add("UserTasks", ds.Tables("Users").Columns("userID"), ds.Tables("Tasks").Columns("userID"))

    dgUsers.SetDataBinding(ds, "Users.userID")

    dgTasks.SetDataBinding(ds, "Tasks.userID")

    Catch ex As Exception

    MessageBox.Show(ex.Message, "Admin Main Form Load")

    End Try

     

    • Post Points: 5
  • 10-04-2006 2:57 PM In reply to

    • nhrones
    • Not Ranked
    • Joined on 10-04-2006
    • New Member
    • Points 5

    Re: your dataview index does not sync with cm index

    Use a BindingSource control then use its Find method to get the index of the row.
    • Post Points: 5
  • 05-22-2007 5:25 AM In reply to

    • davidhere40
    • Not Ranked
    • Joined on 05-22-2007
    • United States
    • New Member
    • Points 5

    Re: [4491] A MUCH easier way

    I didn't like any of the solutions to this problem until I came up with this one...

    The solution is to bind the entire Child table to a bindingSource and that to a dataGridView.  In this case the child table would be the courses that the soldier registered for.

    Then, whenever a soldier is selected, store the selected row in a global variable, such as lastSelectedSoldierRow. Then, when that changes, you do a select on the table that connects the two. The registers table in this case. For example registerDataTable.Select("soldier_ID = " + lastSelectedSoldierRow.ID.ToString());

    Take all those rows and add the course Ids to a globally declared stringBuilder. Whenever a different soldier is selected, add -1 to the stringBuilder as an initializer. Then, for each additional course ID added to the stringBuilder, add a comma to the stringBuilder before it. You'll end up with something like "1,2,3,4,5,6". Next, set the child table's bindingSource.Filter to a that string like so: Filter = "ID in (1,2,3,4,5)".

    That will filter all the courses, down to the ones which belong to the soldier. Whenever a new course is added, append the ID to the stringBuilder and reset the filter.

    Here's an example scrap of source code from my own project. It's messy, but may be helpful to somebody if the above wasn't enough.

    StringBuilder settingsFilterSB = new StringBuilder();
            private void dataGridViewComputers_SelectionChanged(object sender, EventArgs e)
            {
                if (this.dataGridViewComputers.SelectedRows.Count == 1 || this.dataGridViewComputers.SelectedCells.Count > 0)
                {
                    object dataBoundItem;
                    if (this.dataGridViewComputers.SelectedRows.Count == 1)
                        dataBoundItem = this.dataGridViewComputers.SelectedRows[0].DataBoundItem;
                    else
                        dataBoundItem = this.dataGridViewComputers.Rows[this.dataGridViewComputers.SelectedCells[0].RowIndex].DataBoundItem;
                    lastSelectedPcRow = (CallistoDataSet.ComputerOrGroupRow)((DataRowView)dataBoundItem).Row;

                    settingsFilterSB = new StringBuilder();
                    settingsFilterSB.Append("-1");
                    int selectedPcId = lastSelectedPcRow.ID;
                    foreach(CallistoDataSet.ComputerOrGroup2SettingsRow row in this.callistoDataSet.ComputerOrGroup2Settings.Rows)
                    {
                        if(row.ComputerOrGroup_ID == selectedPcId)
                            settingsFilterSB.Append("," + row.ComputerSettings_ID.ToString());
                    }
                   
                    this.computerSettingsBindingSource.Filter = "ID in (" + settingsFilterSB.ToString() + ")";


































    • Post Points: 5
Page 1 of 1 (8 items)