p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 1.0 and 1.1 Professional (http://p2p.wrox.com/forumdisplay.php?f=57)
-   -   Creating DataGrid In Repeater/DataGrid (http://p2p.wrox.com/showthread.php?t=22817)

liduwan December 19th, 2004 03:40 PM

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.




nashnash December 21st, 2004 05:58 AM

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


liduwan December 21st, 2004 11:15 AM

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.




liduwan December 21st, 2004 06:09 PM

<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



nashnash December 22nd, 2004 07:20 AM

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


liduwan December 28th, 2004 07:50 AM

It works like a charm.
Thanks for the code.


steroche August 15th, 2005 06:40 PM

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


Lofa August 19th, 2005 10:38 AM

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


ceema August 30th, 2005 09:47 AM

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

highpointbleed March 6th, 2007 03:31 PM

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...


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

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.