• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 176
  • Last Modified:

Tricky DataGrid problem.

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)
2 Solutions

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
PoeticAudioAuthor Commented:
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.

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"])

 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.

Featured Post

New feature and membership benefit!

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

Tackle projects and never again get stuck behind a technical roadblock.
Join Now