迭代数据表并使用 if 语句修改值

发布于 2024-12-06 04:51:26 字数 531 浏览 0 评论 0原文

我有一个数据表,我尝试使用 for every 循环和 if 语句来修改某些值,但是每当我尝试使用 if 语句时什么也没有发生。如果我删除 if 语句,显然只会更改每一行。

我做错了什么?我已经尝试在 ItemArray.GetValue() 上使用不同的值,但仍然没有运气。

            int x = 0;
            foreach ( DataRow myRow in dt.Rows )
            {
                if (dt.Rows [x].ItemArray.GetValue(1).ToString()=="IP")
                {
                    myRow.BeginEdit ( );
                    myRow [ "grd" ] = " ";
                    myRow.EndEdit ( );
                }
                x++;
            }

I have a datatable that I am trying to use a for each loop and an if statement to modify certain values however whenever I try to use the if statement nothing happens. If I remove the if statement if obviously just changes every row.

What am I doing wrong? I have tried with different values on the ItemArray.GetValue() and still no luck.

            int x = 0;
            foreach ( DataRow myRow in dt.Rows )
            {
                if (dt.Rows [x].ItemArray.GetValue(1).ToString()=="IP")
                {
                    myRow.BeginEdit ( );
                    myRow [ "grd" ] = " ";
                    myRow.EndEdit ( );
                }
                x++;
            }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

吃素的狼 2024-12-13 04:51:26

对于初学者来说,变量 x 是多余的。您可以简单地参考:

if (myRow.ItemArray.GetValue(1).ToString()=="IP")

这应该会减少代码中的一些混乱。

if 语句上放置一个断点,以查看循环每次迭代中 GetValue(1).ToString() 的实际值。您可能检索到错误的值。

For starters, the variable x is superfluous. You can simply refer to:

if (myRow.ItemArray.GetValue(1).ToString()=="IP")

That should reduce some of the confusion in the code.

Put a Breakpoint on the if statement to see what the actual value of GetValue(1).ToString() is in each iteration of the loop. You may be retrieving the wrong value.

一指流沙 2024-12-13 04:51:26

确保您正在初始化 DataTable 列,更重要的是确保您在索引 = 1 处插入一个值。我运行了以下代码,并且在评估“if”语句时没有遇到任何问题。我还删除了您正在使用的计数器,因为在每个循环中都不需要它,因为您可以直接访问每行的 ItemArray。

  DataTable table = new DataTable();
  table.Columns.Add("ID");    //Allows for storage at GetValue(0)
  table.Columns.Add("NAME");  //Allows for storage at GetValue(1)

  //GetValue(0) will return 128|256; GetValue(1) will return IP | OP.
  table.Rows.Add(new object[] {"128", "IP"});  
  table.Rows.Add(new object[] {"256", "OP"});

  foreach(DataRow row in table.Rows)
  {
        string name = row.ItemArray.GetValue(1).ToString();

        if(name == "IP")
        {
             Console.WriteLine("IP was found");
             //Of course do your edits here.
        }
  }

Be sure that you are initializing your DataTable columns, and more importantly make sure that you are inserting a value at index = 1. I ran the following code and had no trouble evaluating the "if" statement. I also removed the counter you were using, because it is not needed in this particular for each loop since you can access each row's ItemArray directly.

  DataTable table = new DataTable();
  table.Columns.Add("ID");    //Allows for storage at GetValue(0)
  table.Columns.Add("NAME");  //Allows for storage at GetValue(1)

  //GetValue(0) will return 128|256; GetValue(1) will return IP | OP.
  table.Rows.Add(new object[] {"128", "IP"});  
  table.Rows.Add(new object[] {"256", "OP"});

  foreach(DataRow row in table.Rows)
  {
        string name = row.ItemArray.GetValue(1).ToString();

        if(name == "IP")
        {
             Console.WriteLine("IP was found");
             //Of course do your edits here.
        }
  }
带上头具痛哭 2024-12-13 04:51:26

数组中的值是多少?真的是“IP”吗?或者是以下之一:“ip”、“Ip”、“iP”?如果大小写不重要,对字符串使用 .ToUpper() 可能会有所帮助。

回答第二个问题:如果您使用数据适配器,那么您只需填充数据集并提取数据集中的第一个表即可。

DataSet ds = new DataSet();
dataAdapter.fill(ds);
DataTable myTable = ds.Tables[0];

What is the value from the array? Is it really "IP"? Or is it one of the following: "ip", "Ip", "iP"? It may help to .ToUpper() on the string if the case does not matter.

To answer your second question: If you use the data adapter then you can just fill a dataset and pull out the first table in the dataset.

DataSet ds = new DataSet();
dataAdapter.fill(ds);
DataTable myTable = ds.Tables[0];
忘羡 2024-12-13 04:51:26

C# 区分大小写,因此很可能实际值不是大写的“IP”,或者可能包含空格等。尝试与 string.Compare 进行比较,使用重载版本,在该版本中可以将 false 设置为区分大小写。

顺便说一句 - 有几种方法可以检查可能更清晰的值。

首先,您可以使用 DataTable 公开的 Select 方法将行过滤为仅索引为 1 的列中值为“IP”的行。您将需要使用列名称,因此我假设它名为“本示例中的“Column1”:

foreach ( DataRow myRow in dt.Rows.Select("Column1 = 'IP'") )
{
    myRow.BeginEdit();
    myRow["grd"] = " ";
    myRow.EndEdit();
}

或者,您可以使用 LINQ 语句,如下所示:

foreach (DataRow myRow in dt.Rows.Cast<System.Data.DataRow>().Where(r => string.Compare(r[1].ToString(), "IP", true) == 0)
{
    myRow.BeginEdit();
    myRow["grd"] = " ";
    myRow.EndEdit();
}

正如 Hand-E-Food 所指出的,“x”变量在您的示例中不提供任何功能。

C# is case-sensitive, so it is most likely that the actual value is not capitalized "IP", or possibly contains space, etc. Try comparing with string.Compare, using the overloaded version where you can base false for case sensitivity.

BTW - There are a couple of ways to check the value that could be a bit cleaner.

First, you can use the Select method exposed by the DataTable to filter the rows to only those with a value of "IP" in the column at index 1. You will need to use the column name, so I will assume it is named "Column1" for this example:

foreach ( DataRow myRow in dt.Rows.Select("Column1 = 'IP'") )
{
    myRow.BeginEdit();
    myRow["grd"] = " ";
    myRow.EndEdit();
}

Alternatively, you could use a LINQ statement as follows:

foreach (DataRow myRow in dt.Rows.Cast<System.Data.DataRow>().Where(r => string.Compare(r[1].ToString(), "IP", true) == 0)
{
    myRow.BeginEdit();
    myRow["grd"] = " ";
    myRow.EndEdit();
}

As noted by Hand-E-Food, the "x" variable provides no functionality in your example.

许你一世情深 2024-12-13 04:51:26

事实证明我需要添加trim()并且它起作用了。

    foreach ( DataRow row in dt.Rows )
            {
                string name = row.ItemArray.GetValue ( 2 ).ToString ( ).ToUpper().Trim();

                if ( name == "IP" )
                {
                    row.BeginEdit();
                    row [ "grd" ] = "-";
                    row.EndEdit();
                }
            }

Turns out I needed to add the trim() and it worked.

    foreach ( DataRow row in dt.Rows )
            {
                string name = row.ItemArray.GetValue ( 2 ).ToString ( ).ToUpper().Trim();

                if ( name == "IP" )
                {
                    row.BeginEdit();
                    row [ "grd" ] = "-";
                    row.EndEdit();
                }
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文