Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.

C#
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";
                          }
                      }


but the thing is Recommendations column is not available in DiagnosisFaultDescription1 it is in RecommendationDetails how can i change that row value??
Posted
Updated 12-Aug-14 3:29am
v3
Comments
ChauhanAjay 12-Aug-14 11:07am    
Is there any common column in both the data tables so that we can create a relationship between both of them.

1 solution

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


C#
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";
        }
    }
}


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
 
Share this answer
 
Comments
chandra sekhar 18-Aug-14 0:32am    
Your solution is for fixed column,but what if non standard fault is another column?? or more than 1 column?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900