How to update a datatable column value depending on another DataTable column values?

Go To StackoverFlow.com

-1

I have two data tables

DataTable dt1=new DataTable();
DataTable dt2=new DataTable();

I want to update one of the column's values depending on other datatable column value.

Eg:

dt1 contains columns [setFamilyno] ["HouseNo"] ["Surname"]

dt2 contains the same columns

I want to update [setFamilyno] column of dt1 DataTable.

  • if dt1["HouseNo"] ["Surname"] is equal to dt2["HouseNo"] ["Surname"] then set the value 1 of dt1 DataTable for all such matches in dt1 table. And for next row from dt2["HouseNo"] ["Surname"] is equal to dt1["HouseNo"] ["Surname"] then set value for [setfamilyno]=2 for all such matches..
2012-04-04 06:05
by Ashutosh


0

Try to create a DataRelation with a ForeignKeyConstraint. The help will be enough to know how to do it.

2012-04-04 06:38
by Dummy01


0

DataView view = new DataView(Datatble);
int viewcount = view.Count;


DataTable distinctValues = view.ToTable(true, "SurNameEnglish", "HouseNumber");
int distinctcount = distinctValues.Rows.Count;

int cnt = 1;
 for (int j = 0; j < distinctcount; j++)
                {

                    string surname = distinctValues.Rows[j]["SurNameEnglish"].ToString();
                    string Housenumber = distinctValues.Rows[j]["HouseNumber"].ToString();
                    for (int i = 0; i < viewcount; i++)
                    {
                        if (Datatble.Rows[i]["SurNameEnglish"].Equals(surname) && Datatble.Rows[i]["HouseNumber"].Equals(Housenumber))
                        {



                            Datatble.Rows[i]["Family"] = cnt;
                            Datatble.AcceptChanges();

                        }
                    }
                    cnt++;

                }

}
2012-04-04 10:54
by SHEKHAR SHETE
Ads