C# 如何编辑数据表中的一行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19629644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 15:27:55  来源:igfitidea点击:

How to Edit a row in the datatable

c#datatable

提问by folk

I have created a data table. It has 3 column Product_id, Product_nameand Product_price

我已经创建了一个数据表。它有 3 列Product_idProduct_nameProduct_price

    Datatable table= new DataTable("Product");

    table.Columns.Add("Product_id", typeof(int));
    table.Columns.Add("Product_name", typeof(string));
    table.Columns.Add("Product_price", typeof(string));

    table.Rows.Add(1, "abc", "100");
    table.Rows.Add(2, "xyz", "200");

Now I want to find by index, and update that row.

现在我想按索引查找,并更新该行。

say for e.g.

说例如

I want to change value of Product_namecolumn to "cde" that has the Product_idcolumn value : 2.

我想将Product_name列的值更改为具有Product_id列值的“cde” :2。

采纳答案by Tafari

First you need to find a row with id == 2 then change the name so:

首先,您需要找到 id == 2 的行,然后将名称更改为:

foreach(DataRow dr in table.Rows) // search whole table
{
    if(dr["Product_id"] == 2) // if id==2
    {
        dr["Product_name"] = "cde"; //change the name
        //break; break or not depending on you
    }
}

You could also try these solutions:

您也可以尝试以下解决方案:

table.Rows[1]["Product_name"] = "cde" // not recommended as it selects 2nd row as I know that it has id 2

Or:

或者:

DataRow dr = table.Select("Product_id=2").FirstOrDefault(); // finds all rows with id==2 and selects first or null if haven't found any
if(dr != null)
{
    dr["Product_name"] = "cde"; //changes the Product_name
}

回答by Zahir Khan

You can traverse through the DataTable like below and set the value

您可以像下面这样遍历数据表并设置值

foreach(DataTable thisTable in dataSet.Tables)
{
    foreach(DataRow row in thisTable.Rows)
    {
        row["Product_name"] = "cde";
    }
}

OR

或者

thisTable.Rows[1]["Product_name"] = "cde";

Hope this helps

希望这可以帮助

回答by Rahul

Try this I am also not 100 % sure

试试这个 我也不是 100% 确定

        for( int i = 0 ;i< dt.Rows.Count; i++)
        {
           If(dt.Rows[i].Product_id == 2)
           {
              dt.Rows[i].Columns["Product_name"].ColumnName = "cde";
           }
        }

回答by idursun

You can find that row with

您可以找到该行

DataRow row = table.Select("Product_id=2").FirstOrDefault();

and update it

并更新它

row["Product_name"] = "cde";

回答by e03050

If your data set is too large first select required rows by Select(). it will stop further looping.

如果您的数据集太大,请首先通过 Select() 选择所需的行。它将停止进一步循环。

DataRow[] selected = table.Select("Product_id = 2")

Then loop through subset and update

然后遍历子集并更新

    foreach (DataRow row in selected)
    {
        row["Product_price"] = "<new price>";
    }

回答by Mohamed Islam Fares

Try the SetFieldmethod:

尝试SetField方法:

By passing column object :

通过传递列对象:

table.Rows[rowIndex].SetField(column, value);

By Passing column index :

通过传递列索引:

table.Rows[rowIndex].SetField(0 /*column index*/, value);

By Passing column name as string :

通过将列名作为字符串传递:

table.Rows[rowIndex].SetField("product_name" /*columnName*/, value);