Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
How to compare two rows in a single table and return change field and its value .

I am using Asp.net with linq.

Please give me exact idea how can i do it?
Posted
Comments
Sergey Alexandrovich Kryukov 15-Mar-12 2:16am    
How do you define "change field"?
--SA
ZurdoDev 15-Mar-12 8:04am    
I believe OP means which fields have different values between the 2 rows.

1 solution

Please try the Following Sample code and change the Code according to your requirement




protected void Page_Load(object sender, EventArgs e)
        {
            //GetFiles(@"C:\Working");
            //BindGrid();
            getChangedRecords(CreateTable());
        }


public void getChangedRecords(DataTable DtInput)
        {

            DataTable dtTest = new DataTable("Change");
            dtTest.Columns.Add("Fieldname", typeof(String));
            dtTest.Columns.Add("RowNo", typeof(String));
            dtTest.Columns.Add("FieldValue", typeof(String));


            foreach (DataRow item in DtInput.Rows)
            {
                int index = DtInput.Rows.IndexOf(item);
                int iCount = item.ItemArray.Count();

                if (index != DtInput.Rows.Count-1)
                {
                    for (int i = 0; i < iCount; i++)
                    {
                        if (item[i].ToString() != DtInput.Rows[index + 1][i].ToString())
                        {
                            DataRow drTest = dtTest.NewRow();
                            drTest["Fieldname"] = DtInput.Columns[i].Caption;
                            drTest["RowNo"] = index;
                            drTest["FieldValue"] = item[i].ToString();

                            dtTest.Rows.Add(drTest);
                        }
                    }
                }
            }

        }






public DataTable CreateTable()
        {
            DataTable dtTest = new DataTable("Test");
            dtTest.Columns.Add("Col1", typeof(int));
            dtTest.Columns.Add("Col2", typeof(String));
            dtTest.Columns.Add("Col3", typeof(String));
            int iCount = 2;

            for (int i = 0; i < iCount; i++)
            {
                DataRow drTest = dtTest.NewRow();
                drTest["Col1"] = i;
                drTest["Col2"] = "Name";
                drTest["Col3"] = "Vipin" + i;

                dtTest.Rows.Add(drTest);
            }

            return dtTest;

        }
 
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