Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
how to use column changed event of datatable.
I am working with column changed event .my code is below.....
 
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 11-Jul-13 2:03am
Edited 11-Jul-13 20:17pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
 
foreach(DataRow row in _dt.Rows )
        {
            row["Age"] = "21";
        }
 

Thanks,
Ankush Bansal
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 6,569
1 Sergey Alexandrovich Kryukov 6,168
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 14 Jul 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100