操纵数据表值 [英] Manipulating datatable values

查看:66
本文介绍了操纵数据表值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据集中有两个表,基于一个表值我必须修改另一个表值并在视图中显示它。



I have two tables in a dataset and based on one table values i have to modify another table value and display it in the view.

DataTable dt = _dsexportData.Tables["DiagnosisFaultDescription1"];
                      DataTable dt1 = _dsexportData.Tables["RecommendationDetails"];

                      foreach (DataRow item in dt.Rows)
                      {
                          if (item[0].ToString() == "Non-standard Fault Detected")
                          {
                              item["Recommendations"] = "Contact vibration Specialist";
                          }
                      }





但是问题列表在DiagnosisFaultDescription1中不可用它是在RecommendationDetails中如何更改该行值?



but the thing is Recommendations column is not available in DiagnosisFaultDescription1 it is in RecommendationDetails how can i change that row value??

推荐答案

您好Chandra,



如果我理解正确地说,您正在尝试根据第一个表中的值更新第二个表中的值(DiagnosisFaultDescription1和RecommendationDetails) - 但是第二个表没有列名(我假设表模式)在两个表中是相同的,或者有重叠的列。如果不是这种情况,那么我看不出你正在做什么的目的。





Hi Chandra,

If I understand correctly, you're trying to update values in the second table, based on values in the first table("DiagnosisFaultDescription1" and "RecommendationDetails") -- however the second table doesn't have column names (I'm assuming the table schema is the same in both tables or has overlapping columns. If neither is the case then I don't see the purpose in what you're doing).


DataTable dt = _dsexportData.Tables["DiagnosisFaultDescription1"];
DataTable dt1 = _dsexportData.Tables["RecommendationDetails"];

var hasRecommendations = dt1.Columns.Contains("Recommendations");
var index = dt1.Columns.IndexOf("Recommendations");

// Loop through the first table, update the second table at the same row position
for (int i = 0; i < dt.Rows.Count; i++)
{
    var item = dt.Rows[i];
    // If the first column in the current row of DiagnosisFaultDescription1 is "Non-standard Fault Detected"
    if (item[0].ToString() == "Non-standard Fault Detected")
    {
        // You may not need this check
        if (hasRecommendations)
        {
            // Set the first column in the same row index in the second table
            // By Name
            dt1.Rows[i]["Recommendations"] = "Contact vibration Specialist";

            // OR By Index of the column
            dt1.Rows[i][index] = "Contact vibration Specialist";
        }
    }
}





如果你能提供更好的答案可以澄清您的问题,如果可能,提供更多的架构细节。否则,尝试解释你想要发生什么作为最终结果。



希望这会有所帮助,



Code.Combustion



I can provide a better answer if you can clarify your question and if possible, provide more schema detail. Otherwise, try to explain what you'd like to occur as the final result.

Hope this helps,

Code.Combustion


这篇关于操纵数据表值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆