C
CVerma
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.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;
using System.Configuration;
namespace TestCProject
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataList dtl1;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.DataGrid dgr1;
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
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(sSelect);
}
private DataSet GetReader(string sSQL)
{
DataSet ds = new DataSet();
SqlConnection oConnect = new
SqlConnection(ConfigurationSettings.AppSettings["SQLConnectionString"]);
oConnect.Open();
// SqlCommand oCommand = new SqlCommand(sSQL, oConnect);
//
// return oCommand.ExecuteReader(CommandBehavior.CloseConnection);
SqlDataAdapter SQLda = new SqlDataAdapter(sSQL,oConnect);
SQLda.Fill(ds, "SQLNewStart");
return ds;
}
#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
protected void BindGrid()
{
try
{
DataGrid1.PagerStyle.Mode = PagerMode.NumericPages;
DataGrid1.PagerStyle.PageButtonCount = 5;
DataGrid1.PageSize = 4;
DataGrid1.DataSource=GetNewStartData();
DataGrid1.DataBind();
}
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(ConfigurationSettings.AppSettings["SQLConnectionString"]);
SqlDataAdapter SQLda = new SqlDataAdapter(strSQL,myConnection);
SQLda.Fill(ds, "SQLNewStart");
return ds;
}
protected void DataGrid1_PageIndexChanged(object source,
System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
try
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
BindGrid();
}
catch (Exception Exp)
{
string Error = Exp.Message;
}
}
protected void BindOrdersGrid(object sender, DataListItemEventArgs
e)
{
ListItemType oType = new ListItemType();
oType = (ListItemType)(e.Item.ItemType);
if (oType == ListItemType.SelectedItem)
{
string sKey = dtl1.DataKeys[e.Item.ItemIndex].ToString();
Session["Status"] = sKey;
DataGrid oGrid = new DataGrid();
oGrid = (DataGrid)(e.Item.FindControl("dgr1"));
Session["Datagrid"]= oGrid;
oGrid.PagerStyle.Mode = PagerMode.NumericPages;
oGrid.PageSize = 2;
oGrid.DataSource = GetOrders(sKey);
oGrid.DataBind();
}
}
private void BindNewGrid(string currentState, DataGrid oGrid)
{
// DataGrid oGrid = new DataGrid();
//
// oGrid = (DataGrid)(e.Item.FindControl("dgr1"));
oGrid.PagerStyle.Mode = PagerMode.NumericPages;
oGrid.PagerStyle.PageButtonCount = 5;
oGrid.PageSize = 4;
oGrid.DataSource = GetOrders(currentState);
oGrid.DataBind();
}
protected void PageIndexChanged(object source,
System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
try
{
DataGrid oGrid = new DataGrid();
// oGrid = (DataGrid)(dtl1.FindControl("dgr1"));
oGrid = (DataGrid)(Session["Datagrid"]);
oGrid.CurrentPageIndex = e.NewPageIndex;
oGrid.PagerStyle.Mode = PagerMode.NumericPages;
oGrid.PageSize = 2;
oGrid.DataSource = GetOrders((string)Session["Status"]);
oGrid.DataBind();
}
catch (Exception Exp)
{
string Error = Exp.Message;
}
}
private DataSet GetOrders(string sKey)
{
string sSelect;
sSelect = "SELECT Orders.OrderID, Orders.OrderDate, " +
"Orders.RequiredDate, Orders.ShippedDate, Orders.Freight, " +
"Shippers.CompanyName As ShipperName " +
"FROM Orders JOIN Shippers " +
"ON Orders.ShipVia = Shippers.ShipperID " +
"WHERE CustomerID='" + sKey + "'";
return GetReader(sSelect);
}
protected void DoItemSelect(object sender,DataListCommandEventArgs
e)
{
if (e.CommandName == "Select")
{
dtl1.SelectedIndex = e.Item.ItemIndex;
dtl1.DataSource = GetCustomers();
dtl1.DataBind();
}
if (e.CommandName == "UnSelect")
{
dtl1.SelectedIndex = -1;
dtl1.DataSource = GetCustomers();
dtl1.DataBind();
}
}
}
}
Following is the Simplegrid.aspx file:
<%@ Page language="c#" Codebehind="SimpleGrid.aspx.cs"
AutoEventWireup="false" Inherits="TestCProject.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<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">
<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>
<aspataGrid id="DataGrid1" AutoGenerateColumns="false"
OnPageIndexChanged="DataGrid1_PageIndexChanged" AllowPaging="True"
runat="server">
<Columns>
<asp:BoundColumn DataField="CategoryId"
HeaderText="CategoryId" ItemStyle-Wrap="false"></asp:BoundColumn>
<asp:BoundColumn DataField="CategoryName"
HeaderText="CategoryName" ItemStyle-Wrap="false"></asp:BoundColumn>
<asp:BoundColumn DataField="description"
HeaderText="description" ItemStyle-Wrap="false"></asp:BoundColumn>
</Columns>
</aspataGrid></TD>
</TR>
<TR>
<TD>
<aspataList DataKeyField="CustomerID" id="dtl1" runat="server"
Width="397px" OnItemCommand="DoItemSelect"
OnItemDataBound="BindOrdersGrid">
<HeaderStyle Font-Bold="True" ForeColor="#ffffff"
BackColor="#b50055" />
<FooterStyle Font-Bold="True" ForeColor="#ffffff"
BackColor="#b50055" />
<ItemStyle BackColor="#FFF7E7" VerticalAlign="Top" />
<AlternatingItemStyle BackColor="#FFFFC0" />
<HeaderTemplate>
<b>Customer List</b>
</HeaderTemplate>
<ItemTemplate>
<asp:ImageButton CommandName="Select"
ImageUrl="~/images/click-down.gif" Width="16" Height="17"
runat="server" AlternateText="Click to view orders" ID="Imagebutton1"
/>
<%# DataBinder.Eval(Container.DataItem, "CustomerID") %>
<b>
<%# DataBinder.Eval(Container.DataItem, "CompanyName") %>
</b>
<%# DataBinder.Eval(Container.DataItem, "City") %>
<%# DataBinder.Eval(Container.DataItem, "Country") %>
Phone:
<%# DataBinder.Eval(Container.DataItem, "Phone") %>
</ItemTemplate>
<SelectedItemTemplate>
<asp:ImageButton CommandName="UnSelect"
ImageUrl="~/images/click-up.gif" Width="16" Height="17" runat="server"
AlternateText="Click to hide orders" ID="Imagebutton2" />
<%# DataBinder.Eval(Container.DataItem, "CustomerID") %>
<b>
<%# DataBinder.Eval(Container.DataItem, "CompanyName") %>
</b>
<%# DataBinder.Eval(Container.DataItem, "City") %>
<%# DataBinder.Eval(Container.DataItem, "Country") %>
Phone:
<%# DataBinder.Eval(Container.DataItem, "Phone") %>
<aspataGrid id="dgr1" runat="server" BorderStyle="None"
BorderWidth="0" BackColor="#DEBA84" CellPadding="3" CellSpacing="0"
OnPageIndexChanged="PageIndexChanged" AllowPaging="True"
DataKeyField="OrderID" Width="100%" AutoGenerateColumns="False">
<HeaderStyle BackColor="#c0c0c0" />
<Columns>
<asp:BoundColumn DataField="ShipperName" HeaderText="Via"
ReadOnly="True" />
</Columns>
</aspataGrid>
</SelectedItemTemplate>
</aspataList></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.
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.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;
using System.Configuration;
namespace TestCProject
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataList dtl1;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.DataGrid dgr1;
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
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(sSelect);
}
private DataSet GetReader(string sSQL)
{
DataSet ds = new DataSet();
SqlConnection oConnect = new
SqlConnection(ConfigurationSettings.AppSettings["SQLConnectionString"]);
oConnect.Open();
// SqlCommand oCommand = new SqlCommand(sSQL, oConnect);
//
// return oCommand.ExecuteReader(CommandBehavior.CloseConnection);
SqlDataAdapter SQLda = new SqlDataAdapter(sSQL,oConnect);
SQLda.Fill(ds, "SQLNewStart");
return ds;
}
#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
protected void BindGrid()
{
try
{
DataGrid1.PagerStyle.Mode = PagerMode.NumericPages;
DataGrid1.PagerStyle.PageButtonCount = 5;
DataGrid1.PageSize = 4;
DataGrid1.DataSource=GetNewStartData();
DataGrid1.DataBind();
}
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(ConfigurationSettings.AppSettings["SQLConnectionString"]);
SqlDataAdapter SQLda = new SqlDataAdapter(strSQL,myConnection);
SQLda.Fill(ds, "SQLNewStart");
return ds;
}
protected void DataGrid1_PageIndexChanged(object source,
System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
try
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
BindGrid();
}
catch (Exception Exp)
{
string Error = Exp.Message;
}
}
protected void BindOrdersGrid(object sender, DataListItemEventArgs
e)
{
ListItemType oType = new ListItemType();
oType = (ListItemType)(e.Item.ItemType);
if (oType == ListItemType.SelectedItem)
{
string sKey = dtl1.DataKeys[e.Item.ItemIndex].ToString();
Session["Status"] = sKey;
DataGrid oGrid = new DataGrid();
oGrid = (DataGrid)(e.Item.FindControl("dgr1"));
Session["Datagrid"]= oGrid;
oGrid.PagerStyle.Mode = PagerMode.NumericPages;
oGrid.PageSize = 2;
oGrid.DataSource = GetOrders(sKey);
oGrid.DataBind();
}
}
private void BindNewGrid(string currentState, DataGrid oGrid)
{
// DataGrid oGrid = new DataGrid();
//
// oGrid = (DataGrid)(e.Item.FindControl("dgr1"));
oGrid.PagerStyle.Mode = PagerMode.NumericPages;
oGrid.PagerStyle.PageButtonCount = 5;
oGrid.PageSize = 4;
oGrid.DataSource = GetOrders(currentState);
oGrid.DataBind();
}
protected void PageIndexChanged(object source,
System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
try
{
DataGrid oGrid = new DataGrid();
// oGrid = (DataGrid)(dtl1.FindControl("dgr1"));
oGrid = (DataGrid)(Session["Datagrid"]);
oGrid.CurrentPageIndex = e.NewPageIndex;
oGrid.PagerStyle.Mode = PagerMode.NumericPages;
oGrid.PageSize = 2;
oGrid.DataSource = GetOrders((string)Session["Status"]);
oGrid.DataBind();
}
catch (Exception Exp)
{
string Error = Exp.Message;
}
}
private DataSet GetOrders(string sKey)
{
string sSelect;
sSelect = "SELECT Orders.OrderID, Orders.OrderDate, " +
"Orders.RequiredDate, Orders.ShippedDate, Orders.Freight, " +
"Shippers.CompanyName As ShipperName " +
"FROM Orders JOIN Shippers " +
"ON Orders.ShipVia = Shippers.ShipperID " +
"WHERE CustomerID='" + sKey + "'";
return GetReader(sSelect);
}
protected void DoItemSelect(object sender,DataListCommandEventArgs
e)
{
if (e.CommandName == "Select")
{
dtl1.SelectedIndex = e.Item.ItemIndex;
dtl1.DataSource = GetCustomers();
dtl1.DataBind();
}
if (e.CommandName == "UnSelect")
{
dtl1.SelectedIndex = -1;
dtl1.DataSource = GetCustomers();
dtl1.DataBind();
}
}
}
}
Following is the Simplegrid.aspx file:
<%@ Page language="c#" Codebehind="SimpleGrid.aspx.cs"
AutoEventWireup="false" Inherits="TestCProject.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<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">
<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>
<aspataGrid id="DataGrid1" AutoGenerateColumns="false"
OnPageIndexChanged="DataGrid1_PageIndexChanged" AllowPaging="True"
runat="server">
<Columns>
<asp:BoundColumn DataField="CategoryId"
HeaderText="CategoryId" ItemStyle-Wrap="false"></asp:BoundColumn>
<asp:BoundColumn DataField="CategoryName"
HeaderText="CategoryName" ItemStyle-Wrap="false"></asp:BoundColumn>
<asp:BoundColumn DataField="description"
HeaderText="description" ItemStyle-Wrap="false"></asp:BoundColumn>
</Columns>
</aspataGrid></TD>
</TR>
<TR>
<TD>
<aspataList DataKeyField="CustomerID" id="dtl1" runat="server"
Width="397px" OnItemCommand="DoItemSelect"
OnItemDataBound="BindOrdersGrid">
<HeaderStyle Font-Bold="True" ForeColor="#ffffff"
BackColor="#b50055" />
<FooterStyle Font-Bold="True" ForeColor="#ffffff"
BackColor="#b50055" />
<ItemStyle BackColor="#FFF7E7" VerticalAlign="Top" />
<AlternatingItemStyle BackColor="#FFFFC0" />
<HeaderTemplate>
<b>Customer List</b>
</HeaderTemplate>
<ItemTemplate>
<asp:ImageButton CommandName="Select"
ImageUrl="~/images/click-down.gif" Width="16" Height="17"
runat="server" AlternateText="Click to view orders" ID="Imagebutton1"
/>
<%# DataBinder.Eval(Container.DataItem, "CustomerID") %>
<b>
<%# DataBinder.Eval(Container.DataItem, "CompanyName") %>
</b>
<%# DataBinder.Eval(Container.DataItem, "City") %>
<%# DataBinder.Eval(Container.DataItem, "Country") %>
Phone:
<%# DataBinder.Eval(Container.DataItem, "Phone") %>
</ItemTemplate>
<SelectedItemTemplate>
<asp:ImageButton CommandName="UnSelect"
ImageUrl="~/images/click-up.gif" Width="16" Height="17" runat="server"
AlternateText="Click to hide orders" ID="Imagebutton2" />
<%# DataBinder.Eval(Container.DataItem, "CustomerID") %>
<b>
<%# DataBinder.Eval(Container.DataItem, "CompanyName") %>
</b>
<%# DataBinder.Eval(Container.DataItem, "City") %>
<%# DataBinder.Eval(Container.DataItem, "Country") %>
Phone:
<%# DataBinder.Eval(Container.DataItem, "Phone") %>
<aspataGrid id="dgr1" runat="server" BorderStyle="None"
BorderWidth="0" BackColor="#DEBA84" CellPadding="3" CellSpacing="0"
OnPageIndexChanged="PageIndexChanged" AllowPaging="True"
DataKeyField="OrderID" Width="100%" AutoGenerateColumns="False">
<HeaderStyle BackColor="#c0c0c0" />
<Columns>
<asp:BoundColumn DataField="ShipperName" HeaderText="Via"
ReadOnly="True" />
</Columns>
</aspataGrid>
</SelectedItemTemplate>
</aspataList></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.