Binding gridview to two datasources

Posted on 2007-10-01
Last Modified: 2013-11-26
I think this is easy but my brain hurts :( i want to bind a gridview to two different datasources, i am thinking that the best way to do this is to create a new datatable and add both datatables  to the one and bind it to the grid. but i am having problems getting this to work. any ideas  on how to do this and if maybe there is a better solution than doing it this way?
Question by:Steege1
    LVL 13

    Expert Comment

    Why do you need to bind two datasources to the gridview?

    Author Comment

    i have two different datatables that i want to merge for one view, i could put them into seperate views but that doesnt make sense  since the data is very similar. its coming from the same database but different parameters and i really dont want to create a sproc just for this one special occasion. so i figure my best bet would be two make my sproc call that returns the datatables. put them into one and then bind. like i said im not sure if this is the best solution, i.e. putting the two datatables together or if there is a way you can databind of gridview to two differenct sourcs
    LVL 7

    Expert Comment

    by:Abu Hamdan

    You can combine the 2 results you are getting from DB using queries, you said you have 2 quesries each one on same table but with different paramaters,, so you can do the following:

    select * from myTable where column1 = par1 and column2= par1
    select * from myTable where column1 = par3 and column2= par4

    This will return 2 results combined with one DataTable

    Author Comment

    i am using sp calls so i would prefer not to make a wrapper sp for this
    LVL 7

    Expert Comment

    by:Abu Hamdan
    Okay another way ,, is to make you own datatable,, make new datatable add whatever columns to it ,, then bind it by looping through the results from 2 sp,,

    Author Comment

    do you have any code examples? i understand that this is what i want to do but need a start on how
    LVL 7

    Accepted Solution

    DataSet ds = new DataSet();



    // Loop through result1
    while (drPort.Read())
    DataRow oRow = ds.Tables[0].NewRow();  
    oRow["column1"]= drPort["resul1"];
    oRow["column2"]= drPort["resul2"];
    and repeat same for second result ,, looping is regular datareader
    LVL 1

    Expert Comment

    Forced accept.

    EE Admin

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    Suggested Solutions

    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    746 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now