Click here to Skip to main content
15,908,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my 'DataTable' Data is


Pk Col1 col2 col3


100 1 1 Result
101 2 2
103 3 3
104 2 4
105 5 5

this is my 'DataTable' ,now i want to compare 'col1,col2' records.If it missmatch
then Check the condition.
for ex :


104 2 4 , this row columns are missmatched, then


check the condition

If(2<4)
{

Then, in col2 2 is come then its increment 1 means the vaslue of 2 is replaced by 3,... upto its reaches 4.
Once it Reaches the 4, then col2 value(4) is replaced with col1 value(2) then store the result in anothe column.

}

My final Result is like this 1,3,4,2,5
This is my task, plz give suggestion to do this
Posted
Updated 1-Aug-12 19:24pm
v2
Comments
pradiprenushe 2-Aug-12 1:28am    
You want to interchange column? Still your logic is not clear.
BobJanova 2-Aug-12 10:52am    
Seems like homework, this is a really arbitrary looking requirement.

Hi,
Try this:
C#
for(int i=0; i<dt.rows.count;>{
    if(dt.Rows[i][1] != dt.Rows[i][2])
    {
        if(dt.Rows[i][1] < dt.Rows[i][2])
        {
            int val= Convert.ToInt32(dt.Rows[i][1]);
            do
            {
                val++;
            }while(val <= Convert.ToInt32(dt.Rows[i][1]);
            dt.Rows[i][1] = val;
        }
    }
}
dt.AcceptChanges();



--Amit
 
Share this answer
 
try this:

VB
For Each dr as DataRow in dt.Rows

If dr("col1")<>dr("col2") Then
--your condition
End If

Next
 
Share this answer
 
v2
Try the below one. Here replace age with col1 and age2 with col2.

C#
foreach (DataRow dr in dt.Rows)
            {
                if (Convert.ToInt32(dr["Age"]) < Convert.ToInt32(dr["Age2"]))
                {

                    foreach (DataRow dr2 in dt.Rows)
                    {
                        if (Convert.ToInt32(dr2["Age2"]) == Convert.ToInt32(dr["Age"]))
                        {

                            foreach (DataRow dr3 in dt.Rows)
                            {
                                if (Convert.ToInt32(dr3["Age2"]) < Convert.ToInt32(dr["Age2"]))
                                    dr3["Age2"] = Convert.ToInt32(dr["Age"]) + 1;
                                if (Convert.ToInt32(dr3["Age2"]) == Convert.ToInt32(dr["Age2"]))
                                    dr3["Age2"] = Convert.ToInt32(dr["Age"]);
                            }
                        }

                    }
                }



i tried with similar scenario of table. Below is the my table contents. It worked perfectly.

C#
ew Student() { Name = "Jack", Age = 1, Age2 = 15,StudentId = 100 },
         new Student() { Name = "Smith", Age = 2, Age2 = 2,StudentId = 101 },
         new Student() { Name = "Smit", Age = 3,Age2 = 3, StudentId = 102 },
          new Student() { Name = "Smit", Age = 2,Age2 = 4, StudentId = 102 }
 
Share this answer
 

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