How to combine different Data Controls

Last post 03-04-2008 3:51 AM by Samu Zhang - MSFT. 4 replies.

Sort Posts:

  • How to combine different Data Controls

    02-27-2008, 9:09 PM

     I have a question that I needed to ask.

     Recently, I encountered a new challenge at work on a project.

     Does anyone have any experience with combining a Grid View control with Repeater?

    it's like repeatedly displaying tables of information.
     

    Table 1
    Table 2
    Table 3
    Table 4

     

    For each table 1,2,3 & 4 in this case are sets of data that points to the same Foreign Key in the database table. However, each table(set) has a different name. The only concept I have with my limited knowledge is to replace the tables(or sets) with GridView, then repeat it using a Repeater Control.

     I have totally no clue how that is going to work. But if there is anyone with a practical suggestion, can you please contribute to this thread? Need urgent help on this matter.
     

    What happens tomorrow is dependent upon what you choose to do today!
  • Re: How to combine different Data Controls

    02-27-2008, 10:17 PM

    This article talks of nested repeater. You can easily change either inner repeater or outer repeater to bea gridview

    http://www.codeproject.com/KB/aspnet/AspNetNestedRepeaters.aspx

    Vikram
    www.vikramlakhotia.com
    justlikethat.vikramlakhotia

    Please mark the answer if it helped you
  • Re: How to combine different Data Controls

    02-27-2008, 11:41 PM
    Answer
    • Loading...
    • rojay12
    • Joined on 04-03-2006, 10:43 PM
    • Sacramento, CA
    • Posts 612

    you are correct try this... This is using the AdventureWorks DB.

     

    using System;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
    
        }
    
    
    
        protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem | e.Item.ItemType == ListItemType.Item)
            {
                String strSQL = "SELECT TOP 10 Production.Product.Name, Production.ProductSubcategory.ProductCategoryID FROM Production.Product INNER JOIN Production.ProductSubcategory ON Production.Product.ProductSubcategoryID = Production.ProductSubcategory.ProductSubcategoryID Where Production.ProductSubcategory.ProductCategoryID=@ProductCategoryID";
                SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SiteCS"].ConnectionString);
                SqlCommand cmd = new SqlCommand(strSQL, conn);
                cmd.Parameters.Add("@ProductCategoryID", SqlDbType.Int).Value = ((System.Data.DataRowView)e.Item.DataItem)["ProductCategoryID"].ToString();
                cmd.CommandType = CommandType.Text;
                GridView gv = (GridView)e.Item.FindControl("GridView1");
                if (conn.State== ConnectionState.Closed) {conn.Open();}
                gv.DataSource = cmd.ExecuteReader();
                gv.DataBind();
                if (conn.State == ConnectionState.Open) { conn.Close(); }
            }
        }
    }
    

     

     

    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1" 
            onitemdatabound="Repeater1_ItemDataBound">
            <ItemTemplate>
            <table>
                <tr>
                    <td><asp:Label runat="server" ID="Name" Text='<%# Bind("Name") %>' />
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:GridView runat="server" ID="GridView1">
                        </asp:GridView>
                    </td>
                </tr>
            </table>
            </ItemTemplate>
        </asp:Repeater>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:SiteCS %>" 
            SelectCommand="SELECT ProductCategoryID, Name FROM Production.ProductCategory"></asp:SqlDataSource>
        </form>
    </body>
    </html>
    
     
    Jared Roberts
    Lead Application Developer
  • Re: How to combine different Data Controls

    02-29-2008, 3:48 AM

    Hi rojay12,

    Thanks for helping me with this. Now, this is only 1/3 to the solution of the problem.

     The other 2/3 is that, I need to add a hyperlink/linkbutton control/normal html link to each record of the GridView.

     For simplicity purposes, let's just stick to normal html link.

     Anyone has any idea how to do that?

    I've already used the concept from rojay12's reply.

    Thanks again!

     

    What happens tomorrow is dependent upon what you choose to do today!
  • Re: How to combine different Data Controls

    03-04-2008, 3:51 AM
    Answer

    Hi Intellisense1001 ,

    Intellisense1001:
    I need to add a hyperlink/linkbutton control/normal html link to each record of the GridView.

    You can add controls in Itemtemplate of gridview control.

    See this useful link.

    http://samples.gotdotnet.com/quickstart/aspplus/doc/webdatalist.aspx

     

    Sincerely,
    Samu Zhang
    Microsoft Online Community Support

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
Page 1 of 1 (5 items)
Microsoft Communities
Page view counter