如何将datagridview的单元格值与变量匹配? [英] how to match datagridview's cell value with a variable ?

查看:99
本文介绍了如何将datagridview的单元格值与变量匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  foreach (DataGridViewRow Row  in  DGViewTWInv.Rows)
{
if (Row.Cells [ 0 ]。Value!= CardId)
{
代码这里
}
}





我需要检查一个值是否值假设数据网格视图中已存在滚动编号,而不是不需要任何操作,但如果滚动编号不匹配,则需要在数据网格视图中插入新行以及其他详细信息以及找到的滚动编号。

但问题出现的是第一行插入,因为foreach只允许进入其循环,如果它们是datagridview中的至少一行。

目前我通过允许用户来完成它在datagridview中添加默认为空行的行,这使得第一个l成为可能oop进入。

现在我现在不想要它,并且想要删除那个空的默认行。怎么做呢?

解决方案

循环的概念是错误的...每次遇到CardId不匹配的情况时,最终会添加一行。如果CardId是2并且你有一个0,1,2的网格,那么你会在遇到0时添加一行,然后当你遇到1时再添加一行。



如果datagridview为空(如你所说)你不会在foreach循环中输入代码 - 但是这告诉你CardId绝对不在数据中!所以在该循环之外添加行。这是一种方法...

  //  确保AllowUserToAddRows = false;  
int rowFound = -1; // 由于-1不能引用DGV中的行,这将表示未找到
foreach (DataGridViewRow row in DGViewTWInv.Rows)
{
if int .Parse(row.Cells [ 0 ]。Value.ToString())== CardId)
{
rowFound = row.Index; // 如果我们找到数据,请记下我们找到的位置
断裂; // 如果您不介意隐藏goto $,您可以选择退出循环b $ b}
}
// 现在检查我们是否已经拥有该ID如果没有将其添加到
如果(rowFound == -1)
this .DGViewTWInv.Rows.Add(CardId, 其他数据);


foreach (DataGridViewRow Row in DGViewTWInv.Rows)
                {
                    if (Row.Cells[0].Value != CardId)
                    {
                       Code Here
                    }
                }



I need to check if a value say a Roll Number is already present in the datagridview than no action required but if the Roll NUMBER doesn't match than I need to insert a new row in the datagridview with the other details along with that Roll Number Found.
But the issue coming is with the first row insertion as the foreach allows only to get in its loop if their is atleast one row in that datagridview.
Currently i have done it by allowing user to add rows which makes by default a null row in the datagridview which makes it possible for first loop entering.
Now I don't want it now and want to remove that null default row.How it can be done?

解决方案

The concept of your loop is wrong ... you will end up adding a row each time you encounter one where the CardId is not matched. If CardId is 2 and you have a grid with 0,1,2 then you will add a row when you encounter 0, then again when you encounter 1.

If the datagridview is empty (as you say) you won't enter the code within the foreach loop - but this tells you that the CardId definitely isn't in the data! So do the adding of the row outside of that loop. Here's one way of doing that ...

//Make sure that AllowUserToAddRows = false;
int rowFound = -1;  // As -1 cannot refer to a row in the DGV this will indicate "not found"
foreach (DataGridViewRow row in DGViewTWInv.Rows)
{
    if (int.Parse(row.Cells[0].Value.ToString()) == CardId)
    {
        rowFound = row.Index;   // If we find the data then note where we found it
        break;                  // You can opt to break out of the loop if you don't mind hidden "goto"
    }
}
// Now check to see if we already have that ID and if not add it in
if (rowFound == -1)
    this.DGViewTWInv.Rows.Add(CardId, "Other Data");


这篇关于如何将datagridview的单元格值与变量匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆