Solved

Gridview within Gridview c#

Posted on 2010-11-26
7
640 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 500 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 500 total points
ID: 34222373
Refer this link

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

which is same as i explained above.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

832 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