Link to home
Start Free TrialLog in
Avatar of rondre
rondreFlag for United States of America

asked on

Gridview within Gridview c#

I have 2 tables (master and detail) that look as follows:

TRADE_MASTER:
_______________
TM_ID  - trade master id
TM_O  - trade master offering member
TM_R  - trade master requested member
TM_D  - trade master date
TM_S  - trade master status


TRADE_DETAIL
________________
TD_ID  - trade detail id
TM_ID  - trade master id (foreign key)
TD_PID  - player id
TD_T  - trade detail type

These are 2 tables for a fantasy football website that deal with trades between members.  I have pages that write values to these tables and the page i'm trying to create is a view of the trade itself.  The main gridview will select on the master table and have columns of the 2 members involved, the date, status, and 2 nested gridviews based on the trade detail table.

The nested gridviews will be need to have a query selecting all records based on the TM_ID field and will be separated by the TD_T field (this field will say either OFFER or REQUEST depending on if the player was linked to the offerer or the requestee).

I'm not sure how to populate the child gridviews in this case.  I've seen several examples online but none of them make sense to me and am looking for a better example or simply an explanation of how this should work.  Examples are preferred so I can try to follow the logic but almost every example I've seen just throws the code out there with no explanation as to the parts that don't seem to make sense.  Hoping someone can help me with this!
Avatar of BurnieP
BurnieP
Flag of Canada image

Hi,

I have created a sample project with a nested repeater example, and a gridview populated by another gridview example.  Maybe it can help you advance in your project.  I am using Linq to objects to populate the repeaters and gridviews so I hope you have a little bit of knowledge with Linq.  But overall, they are pretty straight-forward and basic examples.

Hope it helps,

Burnie
NestedExamples.zip
ASKER CERTIFIED SOLUTION
Avatar of jagssidurala
jagssidurala
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rondre

ASKER

Thanks - I tried those but couldn't quite get it to work - I was able to figure this out through more searching though and will post my code in case it helps - perhaps i wasn't clear with my goals.

thanks for all replies.
protected void Page_Load(object sender, EventArgs e)
    {
        HyperLink hl;

        try
        {
            hl = Master.FindControl("_members") as HyperLink;
            hl.CssClass = "current_page_item";
        }
        catch (Exception)
        {
        }

        if (!IsPostBack)
        {

            // Declare the query string.
            String queryString =
              "Select (Select member_sname from member where member_id = t.trade_offerer) as PROPOSER, " +
                     "(SELECT MEMBER_SNAME FROM MEMBER WHERE MEMBER_ID = T.TRADE_REQUESTOR) AS REQUESTEE, " +
                     " T.TRADE_DATE, T.TRADE_STATUS, T.TM_ID FROM TRADE_MASTER T ORDER BY T.TM_ID DESC";

            // Run the query and bind the resulting DataSet
            // to the GridView control.
            DataSet ds = GetData(queryString);
            if (ds.Tables.Count > 0)
            {
                gvTrades.DataSource = ds;
                gvTrades.DataBind();
            }
        }

    }

    DataSet GetData(String queryString)
    {

        // Retrieve the connection string stored in the Web.config file.
        string connectionString = WebConfigurationManager.ConnectionStrings["DBSFL"].ConnectionString;

        DataSet ds = new DataSet();
        // Connect to the database and run the query.
        SqlConnection connection = new SqlConnection(connectionString);
        SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection);

        // Fill the DataSet.
        adapter.Fill(ds);
        return ds;
    }

    protected void BindSubs(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            object objTemp = gvTrades.DataKeys[e.Row.RowIndex].Value as object;
            if (objTemp != null)
            {
                string tmid = objTemp.ToString();
                GridView gvo = (GridView)e.Row.FindControl("gvODetail");
                GridView gvr = (GridView)e.Row.FindControl("gvRDetail");

                
                string queryOffer = "SELECT P.PLAYER_NAME AS PLAYER FROM TRADE_DETAIL T LEFT OUTER JOIN PLAYER P ON T.PLAYER_ID = P.PLAYER_ID WHERE T.TM_ID = " + tmid + " AND T.TRADE_TYPE = 'OFFER'";
                string queryRequest = "SELECT P.PLAYER_NAME AS PLAYER FROM TRADE_DETAIL T LEFT OUTER JOIN PLAYER P ON T.PLAYER_ID = P.PLAYER_ID WHERE T.TM_ID = " + tmid + " AND T.TRADE_TYPE = 'REQUEST'";

                DataSet dsOff = GetData(queryOffer);
                gvo.DataSource = dsOff;
                gvo.DataBind();

                DataSet dsReq = GetData(queryRequest);
                gvr.DataSource = dsReq;
                gvr.DataBind();
            }
        }
    }

Open in new window

My solution is same as authors solution. Why Mr rondre is rejected my solution.
Avatar of rondre

ASKER

I agree it's similar but for some reason it wasn't working for me - I did some copy/pasting from both and am now thinking there might have been some spelling errors that I didn't catch.  long as someone else can get help from any of these i don't mind accepting your solution.
Avatar of rondre

ASKER

Didn't work for me but might have been user error or capitalization problems when using some copy/paste.