Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Gridview within Gridview c#

Posted on 2010-11-26
7
Medium Priority
?
643 Views
Last Modified: 2012-05-10
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!
0
Comment
Question by:rondre
  • 3
  • 3
7 Comments
 
LVL 16

Expert Comment

by:BurnieP
ID: 34222298
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
0
 
LVL 12

Accepted Solution

by:
jagssidurala earned 1500 total points
ID: 34222332
i think this is your output

we need only one child GridView as per requirement.

TM_ID1                 Date                Status
            
       TD_ID1    OFFER
       TD_ID2    OFFER
       TD_ID3    OFFER      
       TD_ID4    Request
       TD_ID5    Request
       TD_ID6    Request

TM_ID2                 Date                Status
     
       TD_ID7    OFFER
       TD_ID8    OFFER
       TD_ID9    Request


.aspx
------------

 <asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" OnRowDatabound="GridView1_OnRowDataBound">
            <Columns>
                <asp:BoundField DataField="TM_ID1"
HeaderText="TM_ID1" />
<asp:BoundField DataField="Date"
HeaderText="Date" />
<asp:BoundField DataField="Status"
HeaderText="Status" />
 <asp:TemplateField>
                    <ItemTemplate>
<asp:HiddenField id="hdfTmId" runat="server" value='<%#Eval("TM_ID1")%>'
   </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:GridView ID="GridView2"
                            runat="server" AutoGenerateColumns="False">
                            <Columns>
                                <asp:BoundField DataField="TD_ID1"
HeaderText="TD_ID1" />
                                <asp:BoundField DataField="TD_T"
HeaderText="TD_T" />
                            </Columns>
                        </asp:GridView>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

.aspx.cs
------------------
in Page load

page_Load...........
{
     DataTable dtTrademaster = //Get Trademaster Data from DB and assign to grideview1;
GridView1.DataSource = dtTrademaster;
GridView1.DataBind();
}

in Rowdatabound event
{
HiddenField hdfTmId = (HiddenField)e.Row.FindControl("hdfTmId ");
GridView GridView2 = (GridView)e.Row.FindControl("GridView2");

if(hdfTmId != null && GridView2 != null)
{
    DataTable dtTradeDetail = GetTradeDetailByTmId(Convert.ToInt32(hdfTmId.value))// get trade detail by sending Tmid which is in hdfTmId
GridView2.DataSource = dtTradeDetail ;
GridView2.DataBind();

}

}



      
0
 
LVL 12

Assisted Solution

by:jagssidurala
jagssidurala earned 1500 total points
ID: 34222373
Refer this link

http://www.codeproject.com/KB/viewstate/GridViewWithinGridView.aspx

which is same as i explained above.
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 

Author Comment

by:rondre
ID: 34223081
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

0
 
LVL 12

Expert Comment

by:jagssidurala
ID: 34225917
My solution is same as authors solution. Why Mr rondre is rejected my solution.
0
 

Author Comment

by:rondre
ID: 34232059
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.
0
 

Author Closing Comment

by:rondre
ID: 34232068
Didn't work for me but might have been user error or capitalization problems when using some copy/paste.
0

Featured Post

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

972 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