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
.
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..Try to create a DataRelation with a ForeignKeyConstraint. The help will be enough to know how to do it.
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++;
}
}