Click here to Skip to main content
15,896,207 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to use column changed event of datatable.
I am working with column changed event .my code is below.....

C#
private void getDataTable()
       {
           DataTable _dt = new DataTable();
           _dt.Columns.Add("Emp_Name");
           _dt.Columns.Add("Age");
           _dt.Rows.Add("Anamika", "21");
           _dt.Rows.Add("anu", "18");
           _dt.Rows.Add("aman", "21");
           _dt.Rows.Add("Ankush", "19");


           _dt.ColumnChanged += new DataColumnChangeEventHandler(_dt_ColumnChanged);
           _dt.Rows.Add("amika", "21");
           _dt.Rows.Add("akansha", "19");
           _dt.AcceptChanges();
       }

       private static void _dt_ColumnChanged(object sender, DataColumnChangeEventArgs e)
       {
           Console.Write(e.Column.ColumnName + " changed to '" + e.ProposedValue + "'\n");
       }



But my code does not show result.and when i put the breakpoints on _dt_ColumnChanged method then this delegate method not called.
pl z give me solution of this.
Thanx and Regard
Posted
Updated 11-Jul-13 19:17pm
v2

Hi,

As specified in this MSDN[^] link, ColumChanged event will
Occurs after a value has been changed for the specified DataColumn in a DataRow

That means event will be raised when something changed in the row. In your example, you are adding rows in to table, and not changing the values of any column.

If you just add following piece of code in your getDataTableMethod then you would hit the breakpoints in _dt_ColumnChanged

C#
foreach(DataRow row in _dt.Rows )
        {
            row["Age"] = "21";
        }



Thanks,
Ankush Bansal
 
Share this answer
 
At the end of getDataTable(), the object _dt goes out of scope and therefore dies.

Very likely, there is another DataTable in your application and that is the one that you actually want to change.

Or you want to bind _dt to a DataGridView[^] that is visible on one of your application's forms as its DataSource[^].
In this case, bound to another object, the instance _dt, although no longer reachable through that identifier, will live on and show its values through the DataGridView.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900