I have a Table, called Table1, with 3 columns:
1) id – An integer and the primary key
2) Name – A string
3) Flagged – A boolean
In most cases any rows of Table1 will be have Flagged set to false. I’m interested in knowing when any row has a value set to true. For now, I’ve simply been using the query
Table1.Select(“Flagged = TRUE”)
However, I thought it may be possible to determine this using events.
The code below with the DataColumnChangeEventHandler will give me a notification for any column changes. The Notification_ColumnChanged() method gets called three times since there are three lines in the method UpdateTable().
I would prefer to only get a notification once and only if any rows in the Flagged column are modified and therefore I don’t need to call a Select() method. I was wondering if this is possible? Perhaps DataViews and events based on those would be a better way to achieve this? I’m interested in any suggestions.
Thanks.
using System;
using System.Data;
namespace ConsoleApplication1
{
public class Program
{
private TestDataSet _testDataSet;
public Program()
{
_testDataSet = new TestDataSet();
_testDataSet.Table1.AddTable1Row(1, "name1", false);
_testDataSet.Table1.AddTable1Row(2, "name2", false);
_testDataSet.Table1.AcceptChanges();
_testDataSet.Table1.ColumnChanged += new DataColumnChangeEventHandler(Notification_ColumnChanged);
}
private void Notification_ColumnChanged(object sender, DataColumnChangeEventArgs e)
{
Console.WriteLine("Notification_ColumnChanged");
}
public void UpdateTable()
{
Console.WriteLine("Updating");
_testDataSet.Table1[0].Flagged = true;
_testDataSet.Table1[1].Flagged = true;
_testDataSet.Table1[1].Name = "name2";
}
public static void Main(string[] args)
{
Program p = new Program();
p.UpdateTable();
}
}
}
It is not possible because the ColumnChanged event (and also RowChanged event) works on entire row. You can solve testing what column is changed into Notification_ColumnChanged, but it will be fires for any columns.