473,582 Members | 3,083 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Not able to page datagrid within the datalist

Hi,
I have an embedded datagrid within a datalist. I am not able to
perfrom paging in the datagrid. Any ideas? Here is my code:

Here is my Simplegrid.cs file:

using System;
using System.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Sess ionState;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;
using System.Data.Sql Client;
using System.Configur ation;

namespace TestCProject
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.P age
{
protected System.Web.UI.W ebControls.Data List dtl1;
protected System.Web.UI.W ebControls.Labe l Label1;
protected System.Web.UI.W ebControls.Data Grid DataGrid1;
protected System.Web.UI.W ebControls.Data Grid dgr1;

private void Page_Load(objec t sender, System.EventArg s e)
{
if(!Page.IsPost Back)
{
Session["Status"] = "";
dtl1.DataSource = GetCustomers();
dtl1.DataBind() ;

//BindGrid();
}
}

private DataSet GetCustomers()
{
string sSelect;
sSelect = "SELECT CustomerID, CompanyName, City, Country, Phone
FROM Customers WHERE CustomerID LIKE 'c%'";
return GetReader(sSele ct);

}

private DataSet GetReader(strin g sSQL)
{
DataSet ds = new DataSet();
SqlConnection oConnect = new
SqlConnection(C onfigurationSet tings.AppSettin gs["SQLConnectionS tring"]);
oConnect.Open() ;
// SqlCommand oCommand = new SqlCommand(sSQL , oConnect);
//
// return oCommand.Execut eReader(Command Behavior.CloseC onnection);

SqlDataAdapter SQLda = new SqlDataAdapter( sSQL,oConnect);
SQLda.Fill(ds, "SQLNewStar t");
return ds;

}

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

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

}
#endregion

protected void BindGrid()
{
try
{
DataGrid1.Pager Style.Mode = PagerMode.Numer icPages;
DataGrid1.Pager Style.PageButto nCount = 5;
DataGrid1.PageS ize = 4;
DataGrid1.DataS ource=GetNewSta rtData();
DataGrid1.DataB ind();

}
catch (Exception Exp)
{
string errorMessage = Exp.Message;
Response.Write( errorMessage);
}
}
private DataSet GetNewStartData ()
{
DataSet ds = new DataSet();

string strSQL = " Select CategoryId, CategoryName, description from
categories";
SqlConnection myConnection = new
SqlConnection(C onfigurationSet tings.AppSettin gs["SQLConnectionS tring"]);
SqlDataAdapter SQLda = new SqlDataAdapter( strSQL,myConnec tion);
SQLda.Fill(ds, "SQLNewStar t");
return ds;
}
protected void DataGrid1_PageI ndexChanged(obj ect source,
System.Web.UI.W ebControls.Data GridPageChanged EventArgs e)
{
try
{
DataGrid1.Curre ntPageIndex = e.NewPageIndex;
BindGrid();
}
catch (Exception Exp)
{
string Error = Exp.Message;

}
}
protected void BindOrdersGrid( object sender, DataListItemEve ntArgs
e)
{
ListItemType oType = new ListItemType();
oType = (ListItemType)( e.Item.ItemType );
if (oType == ListItemType.Se lectedItem)
{
string sKey = dtl1.DataKeys[e.Item.ItemInde x].ToString();
Session["Status"] = sKey;

DataGrid oGrid = new DataGrid();

oGrid = (DataGrid)(e.It em.FindControl( "dgr1"));
Session["Datagrid"]= oGrid;
oGrid.PagerStyl e.Mode = PagerMode.Numer icPages;
oGrid.PageSize = 2;
oGrid.DataSourc e = GetOrders(sKey) ;
oGrid.DataBind( );

}

}

private void BindNewGrid(str ing currentState, DataGrid oGrid)
{
// DataGrid oGrid = new DataGrid();
//
// oGrid = (DataGrid)(e.It em.FindControl( "dgr1"));

oGrid.PagerStyl e.Mode = PagerMode.Numer icPages;
oGrid.PagerStyl e.PageButtonCou nt = 5;
oGrid.PageSize = 4;
oGrid.DataSourc e = GetOrders(curre ntState);
oGrid.DataBind( );
}

protected void PageIndexChange d(object source,
System.Web.UI.W ebControls.Data GridPageChanged EventArgs e)
{
try
{

DataGrid oGrid = new DataGrid();
// oGrid = (DataGrid)(dtl1 .FindControl("d gr1"));
oGrid = (DataGrid)(Sess ion["Datagrid"]);

oGrid.CurrentPa geIndex = e.NewPageIndex;

oGrid.PagerStyl e.Mode = PagerMode.Numer icPages;
oGrid.PageSize = 2;
oGrid.DataSourc e = GetOrders((stri ng)Session["Status"]);
oGrid.DataBind( );
}
catch (Exception Exp)
{
string Error = Exp.Message;

}
}
private DataSet GetOrders(strin g sKey)
{
string sSelect;
sSelect = "SELECT Orders.OrderID, Orders.OrderDat e, " +
"Orders.Require dDate, Orders.ShippedD ate, Orders.Freight, " +
"Shippers.Compa nyName As ShipperName " +
"FROM Orders JOIN Shippers " +
"ON Orders.ShipVia = Shippers.Shippe rID " +
"WHERE CustomerID='" + sKey + "'";
return GetReader(sSele ct);
}

protected void DoItemSelect(ob ject sender,DataList CommandEventArg s
e)
{
if (e.CommandName == "Select")
{
dtl1.SelectedIn dex = e.Item.ItemInde x;
dtl1.DataSource = GetCustomers();
dtl1.DataBind() ;
}
if (e.CommandName == "UnSelect")
{
dtl1.SelectedIn dex = -1;
dtl1.DataSource = GetCustomers();
dtl1.DataBind() ;
}
}

}
}
Following is the Simplegrid.aspx file:

<%@ Page language="c#" Codebehind="Sim pleGrid.aspx.cs "
AutoEventWireup ="false" Inherits="TestC Project.WebForm 1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1 </title>
<meta name="GENERATOR " Content="Micros oft Visual Studio 7.0">
<meta name="CODE_LANG UAGE" Content="C#">
<meta name="vs_defaul tClientScript" content="JavaSc ript">
<meta name="vs_target Schema"
content="http://schemas.microso ft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING= "GridLayout ">
<form id="Form1" method="post" runat="server">
<TABLE id="Table1" style="Z-INDEX: 104; LEFT: 8px; WIDTH: 448px;
POSITION: absolute; TOP: 8px; HEIGHT: 438px" cellSpacing="1"
cellPadding="1" width="448" border="1">
<TR>
<TD>
<asp:DataGrid id="DataGrid1" AutoGenerateCol umns="false"
OnPageIndexChan ged="DataGrid1_ PageIndexChange d" AllowPaging="Tr ue"
runat="server">
<Columns>
<asp:BoundColum n DataField="Cate goryId"
HeaderText="Cat egoryId" ItemStyle-Wrap="false"></asp:BoundColumn >
<asp:BoundColum n DataField="Cate goryName"
HeaderText="Cat egoryName" ItemStyle-Wrap="false"></asp:BoundColumn >
<asp:BoundColum n DataField="desc ription"
HeaderText="des cription" ItemStyle-Wrap="false"></asp:BoundColumn >
</Columns>
</asp:DataGrid></TD>
</TR>
<TR>
<TD>
<asp:DataList DataKeyField="C ustomerID" id="dtl1" runat="server"
Width="397px" OnItemCommand=" DoItemSelect"
OnItemDataBound ="BindOrdersGri d">
<HeaderStyle Font-Bold="True" ForeColor="#fff fff"
BackColor="#b50 055" />
<FooterStyle Font-Bold="True" ForeColor="#fff fff"
BackColor="#b50 055" />
<ItemStyle BackColor="#FFF 7E7" VerticalAlign=" Top" />
<AlternatingIte mStyle BackColor="#FFF FC0" />
<HeaderTemplate >
<b>Customer List</b>
</HeaderTemplate>
<ItemTemplate >
<asp:ImageButto n CommandName="Se lect"
ImageUrl="~/images/click-down.gif" Width="16" Height="17"
runat="server" AlternateText=" Click to view orders" ID="Imagebutton 1"
/>
<%# DataBinder.Eval (Container.Data Item, "CustomerID ") %>
&nbsp; <b>
<%# DataBinder.Eval (Container.Data Item, "CompanyNam e") %>
</b>&nbsp;
<%# DataBinder.Eval (Container.Data Item, "City") %>
&nbsp;
<%# DataBinder.Eval (Container.Data Item, "Country") %>
&nbsp; &nbsp; Phone:
<%# DataBinder.Eval (Container.Data Item, "Phone") %>
&nbsp;
</ItemTemplate>
<SelectedItemTe mplate>
<asp:ImageButto n CommandName="Un Select"
ImageUrl="~/images/click-up.gif" Width="16" Height="17" runat="server"
AlternateText=" Click to hide orders" ID="Imagebutton 2" />
<%# DataBinder.Eval (Container.Data Item, "CustomerID ") %>
&nbsp; <b>
<%# DataBinder.Eval (Container.Data Item, "CompanyNam e") %>
</b>&nbsp;
<%# DataBinder.Eval (Container.Data Item, "City") %>
&nbsp;
<%# DataBinder.Eval (Container.Data Item, "Country") %>
&nbsp; &nbsp; Phone:
<%# DataBinder.Eval (Container.Data Item, "Phone") %>
&nbsp;
<asp:DataGrid id="dgr1" runat="server" BorderStyle="No ne"
BorderWidth="0" BackColor="#DEB A84" CellPadding="3" CellSpacing="0"
OnPageIndexChan ged="PageIndexC hanged" AllowPaging="Tr ue"
DataKeyField="O rderID" Width="100%" AutoGenerateCol umns="False">
<HeaderStyle BackColor="#c0c 0c0" />
<Columns>
<asp:BoundColum n DataField="Ship perName" HeaderText="Via "
ReadOnly="True" />
</Columns>
</asp:DataGrid>
</SelectedItemTem plate>
</asp:DataList></TD>
</TR>
<TR>
<TD></TD>
</TR>
</TABLE>
<asp:Label id="Label1" style="Z-INDEX: 103; LEFT: 16px; POSITION:
absolute; TOP: 462px" runat="server"> Label</asp:Label>
</form>
</body>
</HTML>

Thanks.
Nov 18 '05 #1
3 2772
Whats the exact Error!!

"CVerma" wrote:
Hi,
I have an embedded datagrid within a datalist. I am not able to
perfrom paging in the datagrid. Any ideas? Here is my code:

Here is my Simplegrid.cs file:

using System;
using System.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Sess ionState;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;
using System.Data.Sql Client;
using System.Configur ation;

namespace TestCProject
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.P age
{
protected System.Web.UI.W ebControls.Data List dtl1;
protected System.Web.UI.W ebControls.Labe l Label1;
protected System.Web.UI.W ebControls.Data Grid DataGrid1;
protected System.Web.UI.W ebControls.Data Grid dgr1;

private void Page_Load(objec t sender, System.EventArg s e)
{
if(!Page.IsPost Back)
{
Session["Status"] = "";
dtl1.DataSource = GetCustomers();
dtl1.DataBind() ;

//BindGrid();
}
}

private DataSet GetCustomers()
{
string sSelect;
sSelect = "SELECT CustomerID, CompanyName, City, Country, Phone
FROM Customers WHERE CustomerID LIKE 'c%'";
return GetReader(sSele ct);

}

private DataSet GetReader(strin g sSQL)
{
DataSet ds = new DataSet();
SqlConnection oConnect = new
SqlConnection(C onfigurationSet tings.AppSettin gs["SQLConnectionS tring"]);
oConnect.Open() ;
// SqlCommand oCommand = new SqlCommand(sSQL , oConnect);
//
// return oCommand.Execut eReader(Command Behavior.CloseC onnection);

SqlDataAdapter SQLda = new SqlDataAdapter( sSQL,oConnect);
SQLda.Fill(ds, "SQLNewStar t");
return ds;

}

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

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

}
#endregion

protected void BindGrid()
{
try
{
DataGrid1.Pager Style.Mode = PagerMode.Numer icPages;
DataGrid1.Pager Style.PageButto nCount = 5;
DataGrid1.PageS ize = 4;
DataGrid1.DataS ource=GetNewSta rtData();
DataGrid1.DataB ind();

}
catch (Exception Exp)
{
string errorMessage = Exp.Message;
Response.Write( errorMessage);
}
}
private DataSet GetNewStartData ()
{
DataSet ds = new DataSet();

string strSQL = " Select CategoryId, CategoryName, description from
categories";
SqlConnection myConnection = new
SqlConnection(C onfigurationSet tings.AppSettin gs["SQLConnectionS tring"]);
SqlDataAdapter SQLda = new SqlDataAdapter( strSQL,myConnec tion);
SQLda.Fill(ds, "SQLNewStar t");
return ds;
}
protected void DataGrid1_PageI ndexChanged(obj ect source,
System.Web.UI.W ebControls.Data GridPageChanged EventArgs e)
{
try
{
DataGrid1.Curre ntPageIndex = e.NewPageIndex;
BindGrid();
}
catch (Exception Exp)
{
string Error = Exp.Message;

}
}
protected void BindOrdersGrid( object sender, DataListItemEve ntArgs
e)
{
ListItemType oType = new ListItemType();
oType = (ListItemType)( e.Item.ItemType );
if (oType == ListItemType.Se lectedItem)
{
string sKey = dtl1.DataKeys[e.Item.ItemInde x].ToString();
Session["Status"] = sKey;

DataGrid oGrid = new DataGrid();

oGrid = (DataGrid)(e.It em.FindControl( "dgr1"));
Session["Datagrid"]= oGrid;
oGrid.PagerStyl e.Mode = PagerMode.Numer icPages;
oGrid.PageSize = 2;
oGrid.DataSourc e = GetOrders(sKey) ;
oGrid.DataBind( );

}

}

private void BindNewGrid(str ing currentState, DataGrid oGrid)
{
// DataGrid oGrid = new DataGrid();
//
// oGrid = (DataGrid)(e.It em.FindControl( "dgr1"));

oGrid.PagerStyl e.Mode = PagerMode.Numer icPages;
oGrid.PagerStyl e.PageButtonCou nt = 5;
oGrid.PageSize = 4;
oGrid.DataSourc e = GetOrders(curre ntState);
oGrid.DataBind( );
}

protected void PageIndexChange d(object source,
System.Web.UI.W ebControls.Data GridPageChanged EventArgs e)
{
try
{

DataGrid oGrid = new DataGrid();
// oGrid = (DataGrid)(dtl1 .FindControl("d gr1"));
oGrid = (DataGrid)(Sess ion["Datagrid"]);

oGrid.CurrentPa geIndex = e.NewPageIndex;

oGrid.PagerStyl e.Mode = PagerMode.Numer icPages;
oGrid.PageSize = 2;
oGrid.DataSourc e = GetOrders((stri ng)Session["Status"]);
oGrid.DataBind( );
}
catch (Exception Exp)
{
string Error = Exp.Message;

}
}
private DataSet GetOrders(strin g sKey)
{
string sSelect;
sSelect = "SELECT Orders.OrderID, Orders.OrderDat e, " +
"Orders.Require dDate, Orders.ShippedD ate, Orders.Freight, " +
"Shippers.Compa nyName As ShipperName " +
"FROM Orders JOIN Shippers " +
"ON Orders.ShipVia = Shippers.Shippe rID " +
"WHERE CustomerID='" + sKey + "'";
return GetReader(sSele ct);
}

protected void DoItemSelect(ob ject sender,DataList CommandEventArg s
e)
{
if (e.CommandName == "Select")
{
dtl1.SelectedIn dex = e.Item.ItemInde x;
dtl1.DataSource = GetCustomers();
dtl1.DataBind() ;
}
if (e.CommandName == "UnSelect")
{
dtl1.SelectedIn dex = -1;
dtl1.DataSource = GetCustomers();
dtl1.DataBind() ;
}
}

}
}
Following is the Simplegrid.aspx file:

<%@ Page language="c#" Codebehind="Sim pleGrid.aspx.cs "
AutoEventWireup ="false" Inherits="TestC Project.WebForm 1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1 </title>
<meta name="GENERATOR " Content="Micros oft Visual Studio 7.0">
<meta name="CODE_LANG UAGE" Content="C#">
<meta name="vs_defaul tClientScript" content="JavaSc ript">
<meta name="vs_target Schema"
content="http://schemas.microso ft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING= "GridLayout ">
<form id="Form1" method="post" runat="server">
<TABLE id="Table1" style="Z-INDEX: 104; LEFT: 8px; WIDTH: 448px;
POSITION: absolute; TOP: 8px; HEIGHT: 438px" cellSpacing="1"
cellPadding="1" width="448" border="1">
<TR>
<TD>
<asp:DataGrid id="DataGrid1" AutoGenerateCol umns="false"
OnPageIndexChan ged="DataGrid1_ PageIndexChange d" AllowPaging="Tr ue"
runat="server">
<Columns>
<asp:BoundColum n DataField="Cate goryId"
HeaderText="Cat egoryId" ItemStyle-Wrap="false"></asp:BoundColumn >
<asp:BoundColum n DataField="Cate goryName"
HeaderText="Cat egoryName" ItemStyle-Wrap="false"></asp:BoundColumn >
<asp:BoundColum n DataField="desc ription"
HeaderText="des cription" ItemStyle-Wrap="false"></asp:BoundColumn >
</Columns>
</asp:DataGrid></TD>
</TR>
<TR>
<TD>
<asp:DataList DataKeyField="C ustomerID" id="dtl1" runat="server"
Width="397px" OnItemCommand=" DoItemSelect"
OnItemDataBound ="BindOrdersGri d">
<HeaderStyle Font-Bold="True" ForeColor="#fff fff"
BackColor="#b50 055" />
<FooterStyle Font-Bold="True" ForeColor="#fff fff"
BackColor="#b50 055" />
<ItemStyle BackColor="#FFF 7E7" VerticalAlign=" Top" />
<AlternatingIte mStyle BackColor="#FFF FC0" />
<HeaderTemplate >
<b>Customer List</b>
</HeaderTemplate>
<ItemTemplate >
<asp:ImageButto n CommandName="Se lect"
ImageUrl="~/images/click-down.gif" Width="16" Height="17"
runat="server" AlternateText=" Click to view orders" ID="Imagebutton 1"
/>
<%# DataBinder.Eval (Container.Data Item, "CustomerID ") %>
<b>
<%# DataBinder.Eval (Container.Data Item, "CompanyNam e") %>
</b>
<%# DataBinder.Eval (Container.Data Item, "City") %>

<%# DataBinder.Eval (Container.Data Item, "Country") %>
Phone:
<%# DataBinder.Eval (Container.Data Item, "Phone") %>

</ItemTemplate>
<SelectedItemTe mplate>
<asp:ImageButto n CommandName="Un Select"
ImageUrl="~/images/click-up.gif" Width="16" Height="17" runat="server"
AlternateText=" Click to hide orders" ID="Imagebutton 2" />
<%# DataBinder.Eval (Container.Data Item, "CustomerID ") %>
<b>
<%# DataBinder.Eval (Container.Data Item, "CompanyNam e") %>
</b>
<%# DataBinder.Eval (Container.Data Item, "City") %>

<%# DataBinder.Eval (Container.Data Item, "Country") %>
Phone:
<%# DataBinder.Eval (Container.Data Item, "Phone") %>

<asp:DataGrid id="dgr1" runat="server" BorderStyle="No ne"
BorderWidth="0" BackColor="#DEB A84" CellPadding="3" CellSpacing="0"
OnPageIndexChan ged="PageIndexC hanged" AllowPaging="Tr ue"
DataKeyField="O rderID" Width="100%" AutoGenerateCol umns="False">
<HeaderStyle BackColor="#c0c 0c0" />
<Columns>
<asp:BoundColum n DataField="Ship perName" HeaderText="Via "
ReadOnly="True" />
</Columns>
</asp:DataGrid>
</SelectedItemTem plate>
</asp:DataList></TD>
</TR>
<TR>
<TD></TD>
</TR>
</TABLE>
<asp:Label id="Label1" style="Z-INDEX: 103; LEFT: 16px; POSITION:
absolute; TOP: 462px" runat="server"> Label</asp:Label>
</form>
</body>
</HTML>

Thanks.

Nov 18 '05 #2
There is NO Error message..it just doesn't do paging.
I see page nos 1 2 3. When I click 2, the data still displays the
results of page no 1. Does that make sense?

c.*****@gmail.c om (CVerma) wrote in message news:<e2******* *************** ****@posting.go ogle.com>...
Hi,
I have an embedded datagrid within a datalist. I am not able to
perfrom paging in the datagrid. Any ideas? Here is my code:

Here is my Simplegrid.cs file:

using System;
using System.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Sess ionState;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;
using System.Data.Sql Client;
using System.Configur ation;

namespace TestCProject
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.P age
{
protected System.Web.UI.W ebControls.Data List dtl1;
protected System.Web.UI.W ebControls.Labe l Label1;
protected System.Web.UI.W ebControls.Data Grid DataGrid1;
protected System.Web.UI.W ebControls.Data Grid dgr1;

private void Page_Load(objec t sender, System.EventArg s e)
{
if(!Page.IsPost Back)
{
Session["Status"] = "";
dtl1.DataSource = GetCustomers();
dtl1.DataBind() ;

//BindGrid();
}
}

private DataSet GetCustomers()
{
string sSelect;
sSelect = "SELECT CustomerID, CompanyName, City, Country, Phone
FROM Customers WHERE CustomerID LIKE 'c%'";
return GetReader(sSele ct);

}

private DataSet GetReader(strin g sSQL)
{
DataSet ds = new DataSet();
SqlConnection oConnect = new
SqlConnection(C onfigurationSet tings.AppSettin gs["SQLConnectionS tring"]);
oConnect.Open() ;
// SqlCommand oCommand = new SqlCommand(sSQL , oConnect);
//
// return oCommand.Execut eReader(Command Behavior.CloseC onnection);

SqlDataAdapter SQLda = new SqlDataAdapter( sSQL,oConnect);
SQLda.Fill(ds, "SQLNewStar t");
return ds;

}

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

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

}
#endregion

protected void BindGrid()
{
try
{
DataGrid1.Pager Style.Mode = PagerMode.Numer icPages;
DataGrid1.Pager Style.PageButto nCount = 5;
DataGrid1.PageS ize = 4;
DataGrid1.DataS ource=GetNewSta rtData();
DataGrid1.DataB ind();

}
catch (Exception Exp)
{
string errorMessage = Exp.Message;
Response.Write( errorMessage);
}
}
private DataSet GetNewStartData ()
{
DataSet ds = new DataSet();

string strSQL = " Select CategoryId, CategoryName, description from
categories";
SqlConnection myConnection = new
SqlConnection(C onfigurationSet tings.AppSettin gs["SQLConnectionS tring"]);
SqlDataAdapter SQLda = new SqlDataAdapter( strSQL,myConnec tion);
SQLda.Fill(ds, "SQLNewStar t");
return ds;
}
protected void DataGrid1_PageI ndexChanged(obj ect source,
System.Web.UI.W ebControls.Data GridPageChanged EventArgs e)
{
try
{
DataGrid1.Curre ntPageIndex = e.NewPageIndex;
BindGrid();
}
catch (Exception Exp)
{
string Error = Exp.Message;

}
}
protected void BindOrdersGrid( object sender, DataListItemEve ntArgs
e)
{
ListItemType oType = new ListItemType();
oType = (ListItemType)( e.Item.ItemType );
if (oType == ListItemType.Se lectedItem)
{
string sKey = dtl1.DataKeys[e.Item.ItemInde x].ToString();
Session["Status"] = sKey;

DataGrid oGrid = new DataGrid();

oGrid = (DataGrid)(e.It em.FindControl( "dgr1"));
Session["Datagrid"]= oGrid;
oGrid.PagerStyl e.Mode = PagerMode.Numer icPages;
oGrid.PageSize = 2;
oGrid.DataSourc e = GetOrders(sKey) ;
oGrid.DataBind( );

}

}

private void BindNewGrid(str ing currentState, DataGrid oGrid)
{
// DataGrid oGrid = new DataGrid();
//
// oGrid = (DataGrid)(e.It em.FindControl( "dgr1"));

oGrid.PagerStyl e.Mode = PagerMode.Numer icPages;
oGrid.PagerStyl e.PageButtonCou nt = 5;
oGrid.PageSize = 4;
oGrid.DataSourc e = GetOrders(curre ntState);
oGrid.DataBind( );
}

protected void PageIndexChange d(object source,
System.Web.UI.W ebControls.Data GridPageChanged EventArgs e)
{
try
{

DataGrid oGrid = new DataGrid();
// oGrid = (DataGrid)(dtl1 .FindControl("d gr1"));
oGrid = (DataGrid)(Sess ion["Datagrid"]);

oGrid.CurrentPa geIndex = e.NewPageIndex;

oGrid.PagerStyl e.Mode = PagerMode.Numer icPages;
oGrid.PageSize = 2;
oGrid.DataSourc e = GetOrders((stri ng)Session["Status"]);
oGrid.DataBind( );
}
catch (Exception Exp)
{
string Error = Exp.Message;

}
}
private DataSet GetOrders(strin g sKey)
{
string sSelect;
sSelect = "SELECT Orders.OrderID, Orders.OrderDat e, " +
"Orders.Require dDate, Orders.ShippedD ate, Orders.Freight, " +
"Shippers.Compa nyName As ShipperName " +
"FROM Orders JOIN Shippers " +
"ON Orders.ShipVia = Shippers.Shippe rID " +
"WHERE CustomerID='" + sKey + "'";
return GetReader(sSele ct);
}

protected void DoItemSelect(ob ject sender,DataList CommandEventArg s
e)
{
if (e.CommandName == "Select")
{
dtl1.SelectedIn dex = e.Item.ItemInde x;
dtl1.DataSource = GetCustomers();
dtl1.DataBind() ;
}
if (e.CommandName == "UnSelect")
{
dtl1.SelectedIn dex = -1;
dtl1.DataSource = GetCustomers();
dtl1.DataBind() ;
}
}

}
}
Following is the Simplegrid.aspx file:

<%@ Page language="c#" Codebehind="Sim pleGrid.aspx.cs "
AutoEventWireup ="false" Inherits="TestC Project.WebForm 1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1 </title>
<meta name="GENERATOR " Content="Micros oft Visual Studio 7.0">
<meta name="CODE_LANG UAGE" Content="C#">
<meta name="vs_defaul tClientScript" content="JavaSc ript">
<meta name="vs_target Schema"
content="http://schemas.microso ft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING= "GridLayout ">
<form id="Form1" method="post" runat="server">
<TABLE id="Table1" style="Z-INDEX: 104; LEFT: 8px; WIDTH: 448px;
POSITION: absolute; TOP: 8px; HEIGHT: 438px" cellSpacing="1"
cellPadding="1" width="448" border="1">
<TR>
<TD>
<asp:DataGrid id="DataGrid1" AutoGenerateCol umns="false"
OnPageIndexChan ged="DataGrid1_ PageIndexChange d" AllowPaging="Tr ue"
runat="server">
<Columns>
<asp:BoundColum n DataField="Cate goryId"
HeaderText="Cat egoryId" ItemStyle-Wrap="false"></asp:BoundColumn >
<asp:BoundColum n DataField="Cate goryName"
HeaderText="Cat egoryName" ItemStyle-Wrap="false"></asp:BoundColumn >
<asp:BoundColum n DataField="desc ription"
HeaderText="des cription" ItemStyle-Wrap="false"></asp:BoundColumn >
</Columns>
</asp:DataGrid></TD>
</TR>
<TR>
<TD>
<asp:DataList DataKeyField="C ustomerID" id="dtl1" runat="server"
Width="397px" OnItemCommand=" DoItemSelect"
OnItemDataBound ="BindOrdersGri d">
<HeaderStyle Font-Bold="True" ForeColor="#fff fff"
BackColor="#b50 055" />
<FooterStyle Font-Bold="True" ForeColor="#fff fff"
BackColor="#b50 055" />
<ItemStyle BackColor="#FFF 7E7" VerticalAlign=" Top" />
<AlternatingIte mStyle BackColor="#FFF FC0" />
<HeaderTemplate >
<b>Customer List</b>
</HeaderTemplate>
<ItemTemplate >
<asp:ImageButto n CommandName="Se lect"
ImageUrl="~/images/click-down.gif" Width="16" Height="17"
runat="server" AlternateText=" Click to view orders" ID="Imagebutton 1"
/>
<%# DataBinder.Eval (Container.Data Item, "CustomerID ") %>
&nbsp; <b>
<%# DataBinder.Eval (Container.Data Item, "CompanyNam e") %>
</b>&nbsp;
<%# DataBinder.Eval (Container.Data Item, "City") %>
&nbsp;
<%# DataBinder.Eval (Container.Data Item, "Country") %>
&nbsp; &nbsp; Phone:
<%# DataBinder.Eval (Container.Data Item, "Phone") %>
&nbsp;
</ItemTemplate>
<SelectedItemTe mplate>
<asp:ImageButto n CommandName="Un Select"
ImageUrl="~/images/click-up.gif" Width="16" Height="17" runat="server"
AlternateText=" Click to hide orders" ID="Imagebutton 2" />
<%# DataBinder.Eval (Container.Data Item, "CustomerID ") %>
&nbsp; <b>
<%# DataBinder.Eval (Container.Data Item, "CompanyNam e") %>
</b>&nbsp;
<%# DataBinder.Eval (Container.Data Item, "City") %>
&nbsp;
<%# DataBinder.Eval (Container.Data Item, "Country") %>
&nbsp; &nbsp; Phone:
<%# DataBinder.Eval (Container.Data Item, "Phone") %>
&nbsp;
<asp:DataGrid id="dgr1" runat="server" BorderStyle="No ne"
BorderWidth="0" BackColor="#DEB A84" CellPadding="3" CellSpacing="0"
OnPageIndexChan ged="PageIndexC hanged" AllowPaging="Tr ue"
DataKeyField="O rderID" Width="100%" AutoGenerateCol umns="False">
<HeaderStyle BackColor="#c0c 0c0" />
<Columns>
<asp:BoundColum n DataField="Ship perName" HeaderText="Via "
ReadOnly="True" />
</Columns>
</asp:DataGrid>
</SelectedItemTem plate>
</asp:DataList></TD>
</TR>
<TR>
<TD></TD>
</TR>
</TABLE>
<asp:Label id="Label1" style="Z-INDEX: 103; LEFT: 16px; POSITION:
absolute; TOP: 462px" runat="server"> Label</asp:Label>
</form>
</body>
</HTML>

Thanks.

Nov 18 '05 #3
I was able to do paging in the embedded datagrid..had to change the
PageIndecChange d function a little..

The whole code remains the same except for the first few lines:

DataGrid oGrid = new DataGrid()
oGrid = (DataGrid)sourc e; // this was able to point to the current
instance of datagrid.
--
Rest of the code as it is.

Thanks.
Nov 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1554
by: Bob Rosen | last post by:
The description given in "Customizing Items Dynamically in the DataList or DataGrid Web Server Control" (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vbCode/ html/vbtskCodeCustomizingItemsDynamicallyInDataListWebControlVisualBasic.asp ) says "The examples use the DataList control, but apply to the DataGrid control as...
1
5286
by: Craig Banks | last post by:
If a row of data in a dataset has a lot of columns the row displaying the data in a datagrid will run way off the screen. What I'd like to do is display a row of data over several datagrid rows so the user doesn't have to scroll horizontally. Essentially, I want to wrap a datagrid row (not text in individual columns) with as much control as...
2
3280
by: Matthew | last post by:
I have a DataGrid nested within a DataList. The DataGrid generates LinkButtons with specific CommandName values. I cannot figure out how to trap this event. I need to know which DataGrid LinkButton was selected. Thanks in advance.
2
1067
by: tshad | last post by:
I am trying to find out how to put a datagrid in another datagrid. I want to be able to display the summary data that I get from my tables to show in the first grid. The when I press a "+" sign on one of the rows, I wanted it to expand to show another datagrid below it, with the detail. I can get this to work as a datagrid within a...
0
1689
by: Chris | last post by:
I've been searching all over and think I am close, but keep getting the error "Index out of range" when trying to reference a nested datagrid when an OnEditCommand event is raised. When the OnEditCommand event is raised, I try to do the following: 1. Find the selected datalist item in which the nested datagrid raised the event. (by...
0
958
by: Vitor Mata | last post by:
Please help me on this one... I have one page with one datalist ...and on the item template i have one datagrid ... by other words one datalist populating one datagrid... everything is working perfect...except when i print the page. The problem is when i print the page, the gridlines of the datagrid are well printed only in the first page...
3
411
by: Arpan | last post by:
Consider the following code: <form runat="server"> <asp:DataList ID="dlUsers" OnCancelCommand="CancelList" OnDeleteCommand="DeleteList" OnEditCommand="EditList" OnItemCommand="ItemList" OnUpdateCommand="UpdateList" RepeatDirection="horizontal" RepeatLayout="table" runat="server"> <ItemTemplate> <asp:LinkButton ID="lnkButton"...
7
2525
by: rn5a | last post by:
How can I place a Label control in the FooterTemplate of a DataList control? I tried this <script runat="server"> Sub Page_Load(obj As Object, ea As EventArgs) Dim dblTotal As Double Dim sqlReader As SqlDataReader
1
1876
by: Brock | last post by:
First note that I am using Framework 1.1. I have an .aspx page that is displaying a list of employees, but only the Employee Number, First Name, Last Name, and Title. It is working great. I recessed it in a <Div></Divto allow scrolling of just the data, not the page. What I need to do is place a DataList (also in a <Div></Divto allow...
0
7886
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7809
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8159
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8312
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6569
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5366
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2312
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1413
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1147
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.