C#根据匹配的文本框字符串更改DataGrid单元格值
我有一个包含三列的datagrid: 套件编号,零件号和数量 我正在编写一个条形码扫描仪winform应用程序,当一个零件号被扫描到一个文本框中时,如果该零件号与DataGrid中的零件号匹配,则C#根据匹配的文本框字符串更改DataGrid单元格值,c#,winforms,loops,datagrid,C#,Winforms,Loops,Datagrid,我有一个包含三列的datagrid: 套件编号,零件号和数量 我正在编写一个条形码扫描仪winform应用程序,当一个零件号被扫描到一个文本框中时,如果该零件号与DataGrid中的零件号匹配,则数量将减少1。一旦数量达到0,该行将高亮显示为绿色,并且不允许再减少数量 做这件事最好的方法是什么?我的第一个想法是搜索数据网格的循环。有什么更有效的方法吗?您在那里尝试过每个循环吗 foreach (DataGridViewRow item in this.dataGridView1.Rows) {
数量将减少1。一旦数量
达到0,该行将高亮显示为绿色,并且不允许再减少数量
做这件事最好的方法是什么?我的第一个想法是搜索数据网格的循环。有什么更有效的方法吗?您在那里尝试过每个循环吗
foreach (DataGridViewRow item in this.dataGridView1.Rows)
{
if (item.Cells[1].Value.ToString() == txtPart.Text)
{
item.ReadOnly = false;
this.dataGridView1.CurrentCell = convert it to int and minus -1;
}
}
同样的方法,如果数量值为零,您可以检查它是否为绿色。①获得零件号输入后,使用LINQ查找具有特定零件号的项目。
②检查数量的值。如果!>0,将行的背景变为绿色或其他颜色
YourItemSourceType temp = YourDataGridItmesSource.Where(ThatItemYouWant => ThaItemYouWant.PartNumber == InputPartNumber).FirstOrDefault();
//①:Get the item with specific Part Number.
//use'?.' in case there is no match for InputPartNumber.
if(temp?.Qty !< 0)
{
temp.Qty --;
NotifyTheChange();
//update your view if you need to update it manually
}
else
CalculateTheItemPositionAndPaintItGreen();
//②
YourItemSourceType temp=YourDataGridItmesSource.Where(ThatItemYouWant=>ThatItemYouWant.PartNumber==InputPartNumber).FirstOrDefault();
//①:获取具有特定零件号的项目。
//如果InputPartNumber不匹配,请使用“?”。
如果(温度?.Qty!<0)
{
临时数量--;
通知更改();
//如果需要手动更新视图,请更新视图
}
其他的
计算项目位置并将其涂成绿色();
//②
是否有非LINQ选项?我不想使用它,因为我对C相当陌生。许多有用的工具都是为了简化问题而发明的,LINQ也是,因为它可以优雅地处理简单和复杂的情况(更容易阅读,更少的bug)。所以这就发生在我找到零件号的按键事件中<代码>字符串搜索值=textBox2.Text;int rowIndex=-1;foreach(iCBOMHDataGridView.Rows中的DataGridViewRow行){if(row.Cells[“0”].Value.ToString().Equals(searchValue)){}else{}