?
Solved

Compare rows from gridview1 to gridview2

Posted on 2009-07-04
8
Medium Priority
?
572 Views
Last Modified: 2013-11-25
I have two gridview in a webpage, each get data from different source but they have two rows are are the same
Gridview1
Code    Boxes      
x1           100

GridView2
Code     Boxes
x1          120

Gridview updates Boxes automatically
so I want to compare this two numbers an get the results into another column on GridView2 called Difference  based on the  Code  listed on both grids. Any ideas
0
Comment
Question by:teogos
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 24813698
How do you retrieve the data ? Is it in a dataset, ... ?
0
 

Author Comment

by:teogos
ID: 24813771
using the wizard to a SLQ connection   sqldatasource
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 24813831
Compare 2 Gridview row values and write those who doesn't match
http://forums.asp.net/p/1236832/2247869.aspx
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

Author Comment

by:teogos
ID: 24814003
Looks a good sample but I can see to get where is writing the difference
0
 

Author Comment

by:teogos
ID: 24815322
Perhaps the question was not understood,  
Grid1
code     boxes
x1          200
x2          100

Grid2
code      boxes
x1         300
x2          150

compare grid1  on code  to grid2 and   add a column on grid2
where it will show the difference on BOXES  from grid1  to grid2  base on CODE
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 24820721
Can you show how you bind the data, because I think you'll need some comparision on level of your datainput (sqldatasource, objectdatasource, ...)
0
 
LVL 53

Accepted Solution

by:
Dhaest earned 2000 total points
ID: 24820803
I've tested it with code below
        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dtGrid1 = new DataTable();
            dtGrid1.Columns.Add("Code", typeof(string));
            dtGrid1.Columns.Add("Boxes", typeof(int));
            DataTable dtGrid2 = new DataTable();
            dtGrid2.Columns.Add("Code", typeof(string));
            dtGrid2.Columns.Add("Boxes", typeof(int));
 
            // fill first datatable
            DataRow dr = dtGrid1.NewRow();
            dr["Code"] = "x1";
            dr["Boxes"] = 100;
            dtGrid1.Rows.Add(dr);
            dr = dtGrid1.NewRow();
            dr["Code"] = "x2";
            dr["Boxes"] = 200;
            dtGrid1.Rows.Add(dr);
 
            // fill second datatable
            dr = dtGrid2.NewRow();
            dr["Code"] = "x1";
            dr["Boxes"] = 100;
            dtGrid2.Rows.Add(dr);
            dr = dtGrid2.NewRow();
            dr["Code"] = "x2";
            dr["Boxes"] = 220;
            dtGrid2.Rows.Add(dr);
 
            GridView1.DataSource = dtGrid1;
            GridView1.DataBind();
 
            GridView2.DataSource = dtGrid2;
            GridView2.DataBind();
 
        }
 
        protected void Button1_Click(object sender, EventArgs e)
        {
            DataTable alfa = (DataTable)GridView1.DataSource;
            alfa.Columns.Add("Equal", typeof(bool));
            DataTable beta = (DataTable)GridView2.DataSource;
            foreach (DataRow x in alfa.Rows)
            {
                bool bEqual = false;
                string strFilter = "Code = '" + x["code"].ToString() + "'";
                DataRow[] filtered = beta.Select(strFilter);
                if (filtered.GetUpperBound(0) >= 0)
                {
                    DataRow dr = filtered[0];
                    if (dr["Boxes"].ToString().Trim() == x["Boxes"].ToString().Trim())
                        bEqual = true;
                }
                x["Equal"] = bEqual;
            }
            GridView1.DataSource = alfa;
            GridView1.DataBind();
        }

Open in new window

0
 

Author Closing Comment

by:teogos
ID: 31599783
Sorry, took so long to get back into, but this works I just have to rearrenge the data. Thanks
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question