Click here to Skip to main content
15,902,002 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to compare lowest column values in two datatable and add to other datatable ..
how to do this..pls provide proper example
Posted

1 solution

Hi,
let say you have three datatables dt1,dt2,dt3.
C#
//suppose you want to get minimum value of ID from dt1,dt2
DataTable dt1; // you already have values in this datatable
DataTable dt2; // you already have values in this datatable
DataTable dt3;
int minvalue1=0,minvalue2=0;
DataRow[] dr = dt1.Select("ID = Min(ID)");
if(dr != null && dr.Count() > 0)
{
      minvalue1 = Convert.ToInt32(dr.CopyToDataTable().Rows[0]["ID"]);
}
DataRow[] dr1 = dt2.Select("ID = Min(ID)");
if(dr1 != null && dr1.Count() > 0)
{
      minvalue2 = Convert.ToInt32(dr1.CopyToDataTable().Rows[0]["ID"]);
}
DataRow drNew = dt3.NewRow();
if(minvalue1 <= minvalue2)
{
      drNew["ID"] = minvalue1;
} 
else
{
      drNew["ID"] = minvalue2;
} 
dt3.Rows.Add(drNew);

Hope it helps you.
Thanks.
 
Share this answer
 
v3
Comments
maulikshah1990 30-Oct-13 3:06am    
Hi ...thanks..but lets say i have mutiple values in both datatable..(one datatable has 5 rows , second datatable has 8 rows) ..then what ????
Harshil_Raval 30-Oct-13 3:11am    
There is no problem with any number of row. What i have done is first get minimum value from particular column from datatable1. then same for datatable2. and then finally insert minimum value in datatable3. Number of rows does not matter here. But you should know from two table, on which column you want to get minimum value.
maulikshah1990 30-Oct-13 3:16am    
My problem is , i have two datatable with column price , and i want to compare minimum value in 2 datatables price column and add that in third datatable.. ...

But before this ,i have a column called name , and i want to check if this column value exist in second datatable...
i hope u understood .or pls give your email id ...i will mail you properly.
Harshil_Raval 30-Oct-13 3:20am    
Suppose in datatable1 in name column value is "Test" and same is exist in datatable2. Then what you want to do?
Tom Marvolo Riddle 30-Oct-13 3:22am    
5!

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