开发者

Iterate thru a datatable and modify values using an if statement

I have a datatable that I am trying t开发者_如何学JAVAo 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++;
            }


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.


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.
        }
  }


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];


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.


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();
                }
            }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜