简体   繁体   English

如何更新数据表的特定行

[英]How to update a specific row of a DataTable

I need to update a datatable in VB.net based on the other column value 我需要根据其他列值更新VB.net中的数据表

| Column_1 |   Column_2 | Column_3 |
___________ ____________ ____________
|    2     |    1       |           |
___________ ____________ ____________
|    5     |    3       |           |
___________ ____________ ____________
|    4     |    2       |           |
___________ ____________ ____________
|    1     |    7       |           |
___________ ____________ ____________
|    8     |    9       |           |
___________ ____________ ____________

What i need is for column1 value 5 , i need column 3 to be 1 and for the remaining row it should be zero. 我需要的是column1 value 5 ,我需要将column 3设置为1,而对于其余的行,它应该设置为零。

What i have tried is 我试过的是

Dim CandidateRow As DataRow
CandidateRow = datatable.select("Column_1" = & rbl.selectedvalue).FirstOrDefault  ' datatable value here rbl.selected value would return 5
CandidateRow("Column_3") = 1

It is not updating the Datatable. 它不更新数据表。

So what can be done to perform it 那么可以做什么来执行它

try below c# code and convert it to vb.net 尝试下面的C#代码并将其转换为vb.net

foreach(DataRow row in table)
    {
       if(row["Column_1"]!=null and row["Column_1"].ToString()=="5")
         {
            row["Column_3"]=1;
         }
       else
         {
           row["Column_3"]=0;
         }
    }
//call AcceptChanges() to persist changes made.
table. AcceptChanges();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM