Tricky DataGrid problem.

Posted on 2006-05-17
Last Modified: 2010-04-16
Hello Experts,

I have a tricky situation here, and i'm not sure the best way to go about solving it.

Okay on my form I have a datagrid which is bound to TableA in DataSetA (no the names aren't really that nondescriptive, just keeping things simple here).
One of the columns in TableA is a bool column, we'll call it isChecked.

So now we have isChecked in the datagrid. What needs to happen, is when the user checks isChecked, it needs to take that corresponding row and insert it into TableB. When that value is unchecked it needs to remove that row from TableB.

The problem is detecting which row in the table has been checked/unchecked based on the user checking/unchecking isChecked in the datagrid. and then doing the logic to either add it or remove it from the other table

I know of ways to solve this, but they seem a bit kludge, for instance I could get all of the rows that have HasChanges, walk through them, get the checked value from there, and then use some logic to either throw them in TableB (if they don't exist there already) or attemtping to remove them from TableB if the value is unchecked. There has to be another way! (or is there... this just doesn't seem very elegant to me)
Question by:PoeticAudio
    LVL 23

    Expert Comment


    It may be a cludge, but the code isn't very complex:

    foreach(DataRow dr in TableA)

    private void AddRowToTableB(DataRow dr)
        //logic for detecting if row exists and adding new row
    LVL 6

    Author Comment

    thanks TAD,

    that's the idea I pretty much have going on right now, I just thought there might be way to handle it a little different.

    So far, I have something along the lines of (psuedocode, method names and such are different in actual app)

    DataTable tblChangedRows = TableA.GetChanges(DataRowState.Modified)

    private void DoTableAModifiedRowLogic(DataTable tblModified)
        foreach(DataRow row in tblModified.Rows)
                  //logic to add row, if needed
                  //logic to remove row

    which is pretty much what you're doing. The reason i don't like this method is because it performs that logic if they modify anything in the datagrid. So say they change a dates in 10 rows, but don't actually mess with "isChecked" then it will still run through the adding and removing logic because we are just checking for changes. Even though the performance probably will not take a huge hit, it just doesn't seem right.

    LVL 25

    Assisted Solution

    What about attaching an event handler to the RowChanged() event?  If you DataSet is strongly typed, you also get strongly-typed versions of the event, so you could do something like this:

    // somewhere in initialization
    TableA.RowAChanged  += new DataSetA.RowARowChangeEventHandler(TableA_RowARowChanged);

    // somewhere else
    void TableA_RowARowChanged(object sender, DataSetA.RowARowChangeEvent e)
      DataSetA.RowA row = e.Row;

    private void AddRowToTableB(DataSetA.RowA row )
        //logic for detecting if row exists and adding new row

    private void RemoveRowFromTableB(DataSetA.RowA row)
        //logic for detecting if row exists and deleting row

    If your dataset is not strongly tpyed, just change to generic agruments and use row accessors (i.e. row["IsChecked"])
    LVL 12

    Accepted Solution


     Sure, this last method is more efficient than the others, but you will not not exactly when 'isChecked' column was changed. A better way is creating a handler for ColumnChanged event of the datatable. Here you can check if the changed column is 'isChecked' and if so, you have the modified row in method's DataColumnChangeEventArgs parameter, you can simply add it (or delete) in TableB.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Internet Business Fax to Email Made Easy - With eFax Corporate (, 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…

    758 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

    10 Experts available now in Live!

    Get 1:1 Help Now