Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Professional
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Professional section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Display Modes
  #1 (permalink)  
Old December 19th, 2004, 03:40 PM
Registered User
 
Join Date: Dec 2004
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Creating DataGrid In Repeater/DataGrid

Hi

I want to create a table that looks like this:

http://203.130.203.133/jean.gif

Can anyone help me?
Please notice that jean code is a 2 column cell.
Also for stock, I need to prerender the text before displaying.
So if stock is zero, display "No Stock" instead of 0 (zero)

I tried to use a Repeater that creates a DataGrid in its Item template but I problem in pre-rendering the text.

Many thanks in advance.



  #2 (permalink)  
Old December 21st, 2004, 05:58 AM
Authorized User
 
Join Date: Sep 2004
Location: , , .
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to nashnash
Default

Hi,
Please copy and pase this code and this will give you the result as you need
-----------------------------save this as 'nested.aspx'--------

<%@ Import Namespace="System.Data" %>
<%@ Page language="c#" Codebehind="nested.aspx.cs" AutoEventWireup="false" Inherits="Insight.nested" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    <HEAD>
        <title>nested</title>
        <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
        <meta name="CODE_LANGUAGE" Content="C#">
        <meta name="vs_defaultClientScript" content="JavaScript">
        <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
        <form id="Form1" method="post" runat="server">
            <asp:repeater id="parentRepeater" runat="server">


                <HeaderTemplate>
                    <table border="1" width="300" >
                        <tr>
                            <td width="100"><b>Size</b></td>
                            <td width="100"><b>Stock</b></td>
                            <td width="100"><b>Price</b></td>
                        </tr>
                    </table>
                </HeaderTemplate>
                <ItemTemplate>
                    <table border=1 width="300">
                        <tr>
                            <td width="100"><b><%# DataBinder.Eval(Container.DataItem,"Brand") %></b></td>
                            <td width="200" align=center ><b><%# DataBinder.Eval(Container.DataItem,"Code") %></b></td>
                        </tr>
                        </table>
                                <asp:repeater id="childRepeater" datasource='<%# ((DataRowView)Container.DataItem).Row.GetChildRows ("myrelation") %>' runat="server">
                                <ItemTemplate>
                                    <table border=1 width="300">
                                    <tr>
                                        <td width="100"><%# DataBinder.Eval(Container.DataItem, "[size]")%></td>

                                        <td width="100"><%#CheckStock(Convert.ToInt32(DataBind er.Eval(Container.DataItem, "[stock]")))%></td>

                                        <td width="100"><%# DataBinder.Eval(Container.DataItem, "[price]")%></td>
                                    </tr>
                                    </table>
                                </ItemTemplate>
                            </asp:repeater>
                        </td>
                    </tr>

            </ItemTemplate>
            </asp:repeater>
        </form>
    </body>
</HTML>

----------------------
------------Save this as 'nested.aspx.cs'----------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace Insight
{
    /// <summary>
    /// NASIM aHMAD
    /// 21/12/2004
    /// </summary>
    public class nested : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.Repeater parentRepeater;

        private void Page_Load(object sender, System.EventArgs e)
        {
            SqlConnection cnn = new SqlConnection("server=INSIGHT;UID=sa;PWD=Insight;d atabase=Pubs;"); //cHANGE YOUR CONNECTION STRING HERE
            SqlDataAdapter cmd1 = new SqlDataAdapter("select * from product",cnn);

            //Create and fill the DataSet.
            DataSet ds = new DataSet();
            cmd1.Fill(ds,"product");

            //Create a second DataAdapter for the Stock table.
            SqlDataAdapter cmd2 = new SqlDataAdapter("select * from stock",cnn);
            cmd2.Fill(ds,"stock");

            //Create the relation bewtween the product and stock tables.
            ds.Relations.Add("myrelation",ds.Tables["product"].Columns["prod_id"],ds.Tables["stock"].Columns["prod_id"]);

            //Bind the product table to the parent Repeater control, and call DataBind.
            parentRepeater.DataSource = ds.Tables["product"];
            Page.DataBind();

            //Close the connection.
            cnn.Close();
        }

        // This function will take the stock and
        // retrun you the label if there is Stock
        // it will print Stock other wise it will
        // print 'No Stock'
        protected string CheckStock(int Stock)
        {
            if(Stock>0)
            {
                return Stock.ToString();
            }

            else
            {
                return "No Stock";
            }
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion
    }
}
-------------------

Please change your connection string here
SqlConnection cnn = new SqlConnection("server=INSIGHT;UID=sa;PWD=Insight;d atabase=Pubs;"); //cHANGE YOUR CONNECTION STRING HERE

in "nested.aspx.cs"

i have kept the table name as 'product' and 'stock' and same column name as you sepcified...

Please let me know if it doesnt work for you.


Thanks
Nash

  #3 (permalink)  
Old December 21st, 2004, 11:15 AM
Registered User
 
Join Date: Dec 2004
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi

It works but it was done in C#, which is easier when you do type casting, however i am not familiar with C#, it will take more time for me to edit the page. Is it posible for you to convert this into VB.NET.

Secondly, I see that you created a repeater inside a repeater, which not a bad idea. If I create controls(eg: buttons) inside the second repeater, how do I access the controls property and events?

Many thanks in advance.



  #4 (permalink)  
Old December 21st, 2004, 06:09 PM
Registered User
 
Join Date: Dec 2004
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

<asp:repeater id="childRepeater" datasource='<%# ((DataRowView)Container.DataItem).Row.GetChildRows ("myrelation") %>' runat="server">

How do I do this in VB.NET?

Many Thanks In Advance


  #5 (permalink)  
Old December 22nd, 2004, 07:20 AM
Authorized User
 
Join Date: Sep 2004
Location: , , .
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to nashnash
Default

copy and paste the code is in vb.net ...

--Save this as 'WebForm1.aspx'---------------
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="VB.WebForm1"%>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    <HEAD>
        <title>nested</title>
        <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
        <meta name="CODE_LANGUAGE" Content="C#">
        <meta name="vs_defaultClientScript" content="JavaScript">
        <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
        <form id="Form2" method="post" runat="server">
            <asp:repeater id="parentRepeater" runat="server">
                <HeaderTemplate>
                    <table border="1" width="300">
                        <tr>
                            <td width="100"><b>Size</b></td>
                            <td width="100"><b>Stock</b></td>
                            <td width="100"><b>Price</b></td>
                        </tr>
                    </table>
                </HeaderTemplate>
                <ItemTemplate>
                    <table border="1" width="300">
                        <tr>
                            <td width="100"><b><%# DataBinder.Eval(Container.DataItem,"Brand") %></b></td>
                            <td width="200" align="center"><b><%# DataBinder.Eval(Container.DataItem,"Code") %></b></td>
                        </tr>
                    </table>
                    <asp:repeater id="childRepeater" datasource='<%# CType(Container.DataItem,DataRowView).CreateChildV iew("OrderRelation") %>' runat="server">
                        <ItemTemplate>
                            <table border="1" width="300">
                                <tr>
                                    <td width="100"><%# DataBinder.Eval(Container.DataItem, "[size]")%></td>
                                    <td width="100"><%#CheckStock(Convert.ToInt32(DataBind er.Eval(Container.DataItem, "[stock]")))%></td>
                                    <td width="100"><%# DataBinder.Eval(Container.DataItem, "[price]")%></td>
                                </tr>
                            </table>
                        </ItemTemplate>
                    </asp:repeater>
                    </td> </tr>
                </ItemTemplate>
            </asp:repeater>
        </form>
    </body>
</HTML>

--save this as 'WebForm1.aspx.vb' --------
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Public Class WebForm1
    Inherits System.Web.UI.Page



#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents parentRepeater As System.Web.UI.WebControls.Repeater
    Protected WithEvents Repeater1 As System.Web.UI.WebControls.Repeater

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here



        Dim cnn As New SqlConnection("server=INSIGHT;UID=sa;PWD=Insight;d atabase=Pubs;")
        Dim ds As New DataSet
        cnn.Open()
        Dim cmd1 As New SqlDataAdapter("select * from product", cnn)
        Dim cmd2 As New SqlDataAdapter("select * from stock", cnn)

        cmd1.Fill(ds, "product")
        cmd2.Fill(ds, "stock")
        ds.Tables(0).TableName = "product"
        ds.Tables(1).TableName = "stock"

        Dim Parent As DataColumn = ds.Tables("product").Columns("prod_id")
        Dim Child As DataColumn = ds.Tables("stock").Columns("prod_id")

        Dim ORelation As DataRelation = New DataRelation("OrderRelation", Parent, Child, False)
        ds.Relations.Add(ORelation)

        parentRepeater.DataSource = ds.Tables("product")
        DataBind()
        cnn.Close()

    End Sub

    ' This function will take the stock and
    ' retrun you the label if there is Stock
    ' it will print Stock other wise it will
    ' print 'No Stock'

    Protected Function CheckStock(ByVal Stock) As String
        If (Stock > 0) Then
            Return Stock.ToString()

        Else
            Return "No Stock"
        End If

    End Function

End Class

------

Plz let me know if it works for you...

Thanks
Nash

  #6 (permalink)  
Old December 28th, 2004, 07:50 AM
Registered User
 
Join Date: Dec 2004
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It works like a charm.
Thanks for the code.

  #7 (permalink)  
Old August 15th, 2005, 07:40 PM
Registered User
 
Join Date: Aug 2005
Location: Dublin, , Ireland.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by liduwan
 It works like a charm.
Thanks for the code.

Hi,
Just came across your post - i was wondering if you know or have come across anyway to edit a nested datagrid?
Thanks in advance,
Stephen

  #8 (permalink)  
Old August 19th, 2005, 11:38 AM
Authorized User
 
Join Date: Aug 2005
Location: , , Finland.
Posts: 68
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This script is almost same as script on Microsoft pages. I have copied the script and tried to get it work but nothing hapends.

What should i Do?
When i use DWMX2004 select command have before the table name dbo.
ex. "dbo.user" how should i insert in this one.

when I tried the microsoft script I did get only a blank screen

  #9 (permalink)  
Old August 30th, 2005, 10:47 AM
Authorized User
 
Join Date: Aug 2005
Location: , , .
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by nashnash
 copy and paste the code is in vb.net ...

--Save this as 'WebForm1.aspx'---------------
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="VB.WebForm1"%>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    <HEAD>
        <title>nested</title>
        <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
        <meta name="CODE_LANGUAGE" Content="C#">
        <meta name="vs_defaultClientScript" content="JavaScript">
        <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
        <form id="Form2" method="post" runat="server">
            <asp:repeater id="parentRepeater" runat="server">
                <HeaderTemplate>
                    <table border="1" width="300">
                        <tr>
                            <td width="100"><b>Size</b></td>
                            <td width="100"><b>Stock</b></td>
                            <td width="100"><b>Price</b></td>
                        </tr>
                    </table>
                </HeaderTemplate>
                <ItemTemplate>
                    <table border="1" width="300">
                        <tr>
                            <td width="100"><b><%# DataBinder.Eval(Container.DataItem,"Brand") %></b></td>
                            <td width="200" align="center"><b><%# DataBinder.Eval(Container.DataItem,"Code") %></b></td>
                        </tr>
                    </table>
                    <asp:repeater id="childRepeater" datasource='<%# CType(Container.DataItem,DataRowView).CreateChildV iew("OrderRelation")††%>' runat="server">
                        <ItemTemplate>
                            <table border="1" width="300">
                                <tr>
                                    <td width="100"><%# DataBinder.Eval(Container.DataItem, "[size]")%></td>
                                    <td width="100"><%#CheckStock(Convert.ToInt32(DataBind er.Eval(Container.DataItem, "[stock]")))%></td>
                                    <td width="100"><%# DataBinder.Eval(Container.DataItem, "[price]")%></td>
                                </tr>
                            </table>
                        </ItemTemplate>
                    </asp:repeater>
                    </td> </tr>
                </ItemTemplate>
            </asp:repeater>
        </form>
    </body>
</HTML>

--save this as 'WebForm1.aspx.vb' --------
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Public Class WebForm1
    Inherits System.Web.UI.Page



#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents parentRepeater As System.Web.UI.WebControls.Repeater
    Protected WithEvents Repeater1 As System.Web.UI.WebControls.Repeater

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here



        Dim cnn As New SqlConnection("server=INSIGHT;UID=sa;PWD=Insight;d atabase=Pubs;")
        Dim ds As New DataSet
        cnn.Open()
        Dim cmd1 As New SqlDataAdapter("select * from product", cnn)
        Dim cmd2 As New SqlDataAdapter("select * from stock", cnn)

        cmd1.Fill(ds, "product")
        cmd2.Fill(ds, "stock")
        ds.Tables(0).TableName = "product"
        ds.Tables(1).TableName = "stock"

        Dim Parent As DataColumn = ds.Tables("product").Columns("prod_id")
        Dim Child As DataColumn = ds.Tables("stock").Columns("prod_id")

        Dim ORelation As DataRelation = New DataRelation("OrderRelation", Parent, Child, False)
        ds.Relations.Add(ORelation)

        parentRepeater.DataSource = ds.Tables("product")
        DataBind()
        cnn.Close()

    End Sub

    ' This function will take the stock and
    ' retrun you the label if there is Stock
    ' it will print Stock other wise it will
    ' print 'No Stock'
†††
    Protected Function CheckStock(ByVal Stock) As String
        If (Stock > 0) Then
            Return Stock.ToString()

        Else
            Return "No Stock"
        End If

    End Function

End Class

------

Plz let me know if it works for you...

Thanks
Nash

Hello Nash,

   I have tried the code but just like for Lofa, me too gets a blank page, could you please tell me what's wrong with the coding?

Ceema
  #10 (permalink)  
Old March 6th, 2007, 03:31 PM
Registered User
 
Join Date: Jan 2007
Location: Garland, Texas, USA.
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to highpointbleed
Default

I have this wonderfulness...

front aspx page....

                                <asp:Repeater id="dr_results" Runat="server">
<ItemTemplate>
    <table width="500">
        <tr>
            <td><b>Employee:</b>
                <%#Container.DataItem("DisplayName")%>
            </td>
            <td><b>In Service:</b>
                <%#Container.DataItem("In service")%>
            </td>
        </tr>
        <tr>
            <td>
                <asp:DataGrid id="dg_WO" runat="server"
                    CellPadding="3"
                    BorderColor="#999999"
                    BackColor="White"
                    Font-Size="X-Small"
                    Width="874px"
                    GridLines="Vertical"
                    BorderWidth="1px"
                    BorderStyle="None"
                    AutoGenerateColumns="true" visible="True"
                                                        DataSource='<%# GetDataSource(Container.DataItem("In service"),Container.DataItem("Out of Service"),Container.DataItem("DisplayName"))%>'>
                </asp:DataGrid>
            </TD>
        </TR>
        <tr>
            <td><b>Out Service:</b>
                <%#Container.DataItem("Out of Service")%>
        </td>
                                            </tr>
                                        </TABLE>
</ItemTemplate>
</asp:Repeater>


code behind page.....

  Private Sub btn_date_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_date.Click
        Try
            With _ADO
                _ADO.SQLStr = "usp_GetStandByTime '" & txt_from.Text & " 3:30:00 PM', '" & txt_to.Text & " 7:00:00 AM'"
                _DataSet = .ReturnDataSet

                dr_results.DataSource = _DataSet.Tables(1)
                dr_results.DataBind()

                pnl_date.Visible = False
                pnl_Report.Visible = True
            End With
        Catch ex As Exception
            lbl_error.Text = "- No Data to Return - " & ex.Message
            pnl_date.Visible = True
            pnl_Report.Visible = False
        End Try
    End Sub

    Public Function GetDataSource(ByVal dataItem1 As String, ByVal dataItem2 As String, ByVal dataItem3 As String) As DataView
        Dim i As Integer = 0
        Dim Inserv As String = dataItem1.ToString
        Dim Outserv As String = dataItem2.ToString
        Dim Employee As String = dataItem3.ToString
        Dim _DataSet As New DataSet
        Try
            With _ADO
                _ADO.SQLStr = "usp_GetStandByTimeByEmp '" & Inserv & "', '" & Outserv & "', '" & Employee & "'"
                _DataSet = .ReturnDataSet
                dg_WO.DataSource = _DataSet.Tables(0)
                dg_WO.DataBind()
            End With
        Catch ex As Exception
            lbl_error.Text = "--- No Data to Return --- " & ex.Message
            pnl_date.Visible = False
            pnl_Report.Visible = True
        End Try
    End Function


--------------end code---------------

I get "--- No Data to Return --- Object not set to a reference of an object"

Please help!!!!!!!!




smell that - that's tension...
 


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
creating columns in datagrid pady123 ASP.NET 1.x and 2.0 Application Design 0 September 8th, 2007 02:08 AM
Creating a Datagrid ja8261 VB.NET 2002/2003 Basics 2 November 12th, 2004 05:35 PM
Creating on-demand validation in DataGrid sankar General .NET 0 June 22nd, 2004 06:02 AM
Datalist , Datagrid, or Repeater question olambe BOOK: ASP.NET Website Programming Problem-Design-Solution 7 June 1st, 2004 02:00 PM
Creating a Hyperlink in a datagrid aadz5 ASP.NET 1.0 and 1.1 Basics 0 October 25th, 2003 08:36 AM



All times are GMT -4. The time now is 02:35 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.