I have this code that generates Subheadings into a DataGrid. I have paging as well and I want to add a subheading on each page that doesn't have on, if the category is broken in paging.
I hope you understand.
The main thing is that I just want to add a row to the top of the DataGrid where it says "ADD ROW AT TOP OF DATAGRID WITH A SUBHEADING".
<script runat="server">
SqlConnection Connection = new SqlConnection("server=(local); database=test2; trusted_connection=true;");
public string strNext;
public string strPrevious;
public int intPage;
public int intPages;
public int counter;
void Page_Load()
{
SqlDataAdapter Adapter = new SqlDataAdapter("Select CategoryName, ProductName, Cast(UnitPrice as varchar(50)) as unitprice, UnitsinStock from Products INNER JOIN Categories On Products.categoryID = Categories.CategoryID order by Products.categoryID", Connection);
DataSet Set = new DataSet();
Adapter.Fill(Set);
knoppen
Member
150 Points
54 Posts
Add row at top of databound DataGrid
Mar 08, 2006 10:18 AM|LINK
Hello!
I have this code that generates Subheadings into a DataGrid. I have paging as well and I want to add a subheading on each page that doesn't have on, if the category is broken in paging.
I hope you understand.
The main thing is that I just want to add a row to the top of the DataGrid where it says "ADD ROW AT TOP OF DATAGRID WITH A SUBHEADING".
Cheers!
<%@ Page Language="c#" debug="true" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<%@ import Namespace="System.Drawing" %>
<script runat="server">
SqlConnection Connection = new SqlConnection("server=(local); database=test2; trusted_connection=true;");
public string strNext;
public string strPrevious;
public int intPage;
public int intPages;
public int counter;
void Page_Load()
{
SqlDataAdapter Adapter = new SqlDataAdapter("Select CategoryName, ProductName, Cast(UnitPrice as varchar(50)) as unitprice, UnitsinStock from Products INNER JOIN Categories On Products.categoryID = Categories.CategoryID order by Products.categoryID", Connection);
DataSet Set = new DataSet();
Adapter.Fill(Set);
string curCat;
string prevCat = "";
TableRow row;
int i = 0;
while(i <= Set.Tables[0].Rows.Count - 1)
{
curCat = Set.Tables[0].Rows[i]["CategoryName"].ToString();
if(curCat != prevCat)
{
prevCat = curCat;
DataRow shRow = Set.Tables[0].NewRow();
shRow["ProductName"] = Set.Tables[0].Rows[i][0];
shRow["UnitPrice"] = "SubHead";
shRow["UnitsinStock"] = "SubHead";
Set.Tables[0].Rows.InsertAt(shRow, i);
i += 1;
}
i += 1;
}
if(Set.Tables[0].Rows.Count == 0) {
DataGrid1.Visible = false;
} else {
PagedDataSource Paging = new PagedDataSource();
Paging.DataSource = Set.Tables[0].DefaultView;
Paging.AllowPaging = true;
Paging.PageSize = 9;
if(Request.QueryString["page"] != null) {
intPage = Convert.ToInt32(Request.QueryString["page"]);
} else {
intPage = 1;
}
intPages = Paging.PageCount;
Paging.CurrentPageIndex = intPage -1;
if(!Paging.IsLastPage) {
strNext = "<a href=\"/test4.aspx?page=" + Convert.ToString(intPage +1) + "\">Next</a>";
}
if(!Paging.IsFirstPage) {
strPrevious = "<a href=\"/test4.aspx?page=" + Convert.ToString(intPage -1) + "\">Previous</a> ";
}
DataGrid1.DataSource = Paging;
DataGrid1.DataBind();
DataGrid1.Visible = true;
}
}
private void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
if(e.Item.ItemIndex == 8)
{
if(e.Item.Cells[1].Text.ToString() == "SubHead")
{
e.Item.CssClass = "hide";
}
}
if(e.Item.ItemIndex == 0)
{
if(e.Item.Cells[1].Text.ToString() != "SubHead")
{
// ADD ROW AT TOP OF DATAGRID WITH A SUBHEADING
}
}
if(e.Item.Cells[1].Text.Equals("SubHead"))
{
e.Item.Cells[0].Attributes.Add("align", "Left");
e.Item.Cells[0].ColumnSpan = 3;
e.Item.Cells[0].Font.Bold = true;
e.Item.Cells.RemoveAt(2);
e.Item.Cells.RemoveAt(1);
e.Item.BackColor = Color.FromArgb(204, 204, 255);
}
}
}
</script>
<html>
<head>
<title>Test</title>
<link rel="stylesheet" href="/_style/style.css">
</head>
<body bgcolor="cccccc">
<form runat="server">
<asp:datagrid id="DataGrid1" width="450" border="0" cellpadding="3" cellspacing="1" runat="server" AutoGenerateColumns="False" EnableViewState="False" ForeColor="Black" BackColor="White" GridLines="None" OnItemDataBound="DataGrid1_ItemDataBound">
<HeaderStyle font-bold="True" forecolor="White" backcolor="#4A3C8C"></HeaderStyle>
<ItemStyle backcolor="#DEDFDE"></ItemStyle>
<Columns>
<asp:BoundColumn DataField="ProductName" HeaderText="Product Name"></asp:BoundColumn>
<asp:BoundColumn DataField="UnitPrice" HeaderText="Unit Price"></asp:BoundColumn>
<asp:BoundColumn DataField="UnitsInStock" HeaderText="Stock Level"></asp:BoundColumn>
</Columns>
</asp:datagrid>
</form>
<table border="0" cellpadding="3" cellspacing="1" width="450"
<tr>
<td align="left" width="150"><% Response.Write(strPrevious); %></td>
<td align="center" width="150"><% Response.Write(intPage + " av " + intPages); %></td>
<td align="right" width="150"><% Response.Write(strNext); %></td>
</tr>
</table>
</body>
</html>