How to Edit a row in the datatable

前端 未结 6 1059
说谎
说谎 2020-12-05 18:46

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

    Datatable tabl         


        
6条回答
  •  无人及你
    2020-12-05 18:56

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

    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
    }
    

提交回复
热议问题