Click here to Skip to main content
15,066,062 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a form with multible textboxes .. and i want to check if any value were changed since open form .. i used a datatable and fill it by data from a stored procedure in database .. and in Validated event in the textbox the code compare the value between the suitable column in datatabl and the editing text .. if it changed the textbox backcolor turned to yellow …

the code works fine for one textbox … but since I have many textboxes so i want a method to compare every textbox by its column in the datatable without repeated the code for every textbox one by one ... for example the textbox txtName compared to the first column in the datatable called fullName and the textbox txtNickName compared by the second column called nickname ..... etc

What I have tried:

private void txtName_Validated(object sender, EventArgs e)
            DataTable dt = Person.DETECT_CHANGES(txtName.Text, txtNickName.Text); //could be many columns here

            foreach (DataRow row in dt.Rows)
                if ((txtName.Text).Trim() != row["fullName"].ToString())
                    txtName.BackColor = Color.Yellow;
                    txtName.BackColor = Color.White;
Updated 18-Sep-17 17:28pm

This Google Search should help: c# dirty state datatable edits - Google Search[^]
Medo2015 19-Sep-17 5:13am
I'll try and come here again ... thank you
Declare all the textboxes in a TextBox[] array and iterate for each value with respect to the reference value in the dataTable and based on the value state (text changed/ not changed) change the color of textbox.
Medo2015 19-Sep-17 5:00am
That exactly what i want ... could you help me with code please ?
Karthik_Mahalingam 19-Sep-17 6:37am
use this as reference and try, i have not tested it.
 Dictionary<string, TextBox> dict = new Dictionary<string, TextBox>();
            dict.Add("fullName", TextBox1);
            dict.Add("col1", TextBox2);
            dict.Add("col2", TextBox3);

            foreach (var item in dict)
                string refValue = dt.Rows[0][item.Key].ToString();
                if(item.Value.Text != refValue)
                    item.Value.BackColor = System.Drawing.Color.Yellow;

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