与数据表相比,验证多个文本框 [英] Validate multiple textboxes compared to a datatable
问题描述
我有一个带有多个文本框的表单..我想检查自打开表单后是否有任何值被更改..我使用了数据表并通过数据库中的存储过程中的数据填充它...以及在文本框代码比较datatabl中的合适列和编辑文本之间的值..如果它改变了文本框背面颜色变为黄色...
代码适用于一个文本框...但由于我有很多文本框,所以我想要一个方法来比较每个文本框的数据表中的列,而不是逐个重复每个文本框的代码...例如文本框 txtName
与数据表中名为 fullName
的第一列相比较,文本框 txtNickName
与第二列相比较昵称
.....等
我的尝试:
private void txtName_Validated(object sender,EventArgs e)
{
DataTable dt = Person.DETECT_CHANGES(txtName.Text,txtNickName.Text); //这里可能有很多列
foreach(dt.Rows中的DataRow行)
{
if((txtName.Text).Trim()!= row [fullName ] .ToString())
{
txtName.BackColor = Color.Yellow;
break;
}
else
{
txtName.BackColor = Color.White;
}
}
此Google搜索应该有所帮助: c#脏状态数据表编辑 - Google搜索 [ ^ ]
声明TextBox []
数组中的所有文本框,并针对参考值迭代每个值dataTable
并根据值状态(文本已更改/未更改)更改文本框的颜色
。
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; break;further } else { txtName.BackColor = Color.White; } }
This Google Search should help: c# dirty state datatable edits - Google Search[^]
Declare all the textboxes in aTextBox[]
array and iterate for each value with respect to the reference value in thedataTable
and based on the value state (text changed/ not changed) change thecolor
of textbox.
这篇关于与数据表相比,验证多个文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!