A Simple Method for adding a number column to a DataGrid
Source: Internet
Author: User
A Simple Method for adding a number column to a DataGrid
<Asp: templatecolumn headertext = "no.">
<Itemstyle horizontalalign = "center"> </itemstyle>
<Itemtemplate>
<Asp: Label runat = "server" text = '<% # dgcustomize. currentpageindex * dgcustomize. pagesize + dgcustomize. Items. Count + 1%>'>
</ASP: Label>
</Itemtemplate>
</ASP: templatecolumn>
In fact, currentpageindex starts from 0, so when the first master, currentpageindex * pagesize is always 0
DataGrid. Items. Count also starts from 0, so the index of the first record is 0, which also indicates that the editindexitem is not editable when it is editindexitem =-1, instead of 0.
Perform the following Code
Protected void grid_itemcommand (Object source, datagridcommandeventargs E)
{
If (E. commandname = "Bt1 ")
{
Response. Write (E. Item. ID );}
}
The result is that the corresponding e. commandname = "Bt1") of the row that does not have a data binding column but is a statistical row. The row number is displayed.
# Region summary rows in datagrid controls very good // <summary>
/// Http://msdn.microsoft.com/library/default.asp? Url =/library/en-US/dndive/html/data01102002.asp
/// </Summary>
/// <Returns> </returns> private dataset physicaldataread ()
{
String strcnn = "Server = localhost; database = northwind; Integrated Security = sspi ;";
Sqlconnection conn = new sqlconnection (strcnn); // command text using with Rollup
Stringbuilder sb = new stringbuilder ("");
SB. append ("select ");
SB. append ("case grouping (O. customerid) When 0 ");
SB. append ("then O. customerid else '(total) 'end as mycustid ,");
SB. append ("case grouping (OD. orderid) When 0 ");
SB. append ("then OD. orderid else-1 end as myorderid ,");
SB. append ("sum (OD. Quantity * OD. unitprice) as price ");
SB. append ("from orders o, [Order Details] OD ");
SB. append ("where year (orderdate) = @ nyear and OD. orderid = O. orderid ");
SB. append ("group by O. customerid, OD. orderid with rollup ");
SB. append ("order by O. customerid, price ");
String strcmd = sb. tostring ();
SB = NULL; sqlcommand cmd = new sqlcommand ();
Cmd. commandtext = strcmd;
Cmd. Connection = conn;
Sqldataadapter da = new sqldataadapter (strcmd, strcnn );
Da. selectcommand = cmd; // set the "year" parameter
Sqlparameter p1 = new sqlparameter ("@ nyear", sqldbtype. INT );
P1.direction = parameterdirection. input;
P1.value = convert. toint32 (txtyear. Text );
Cmd. Parameters. Add (P1); dataset DS = new dataset ();
Da. Fill (DS, "orders ");
Return Ds;
}
/// <Summary>
//// E. Item. cells. removeat (0); // remove custid
// E. Item. cells. removeat (0); // remove Order #, now the first
// Use statement E. item. itemtype = listitemtype. header to find the DataGrid header; // 2. Clear the default control in the header; // E. item. controls. clear (); // span and right-align the cell left
// E. Item. cells [0]. columnspan = 3;
// E. Item. cells [0]. horizontalalign = horizontalalign. Right;
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Public void itemcreated (Object sender, datagriditemeventargs E)
{
// Foreach (maid e in grid. Items)
//{
// Get the type of the newly created item
Listitemtype itemtype = E. Item. itemtype;
If (itemtype = listitemtype. Item | itemtype = listitemtype. alternatingitem)
{
Try
{
// Get the data bound to the current row
Datarowview DRV = (datarowview) E. Item. dataitem;
If (DRV! = NULL)
{
// Check here the app-specific way to detect whether
// Current row is a summary row} If (INT) DRV ["myorderid"] =-1)
{
// Modify style and layout here.
E. Item. backcolor = color. White;
E. Item. Font. Bold = true;
E. Item. cells. removeat (1); // remove the Order # Cell
E. Item. cells [0]. columnspan = 2; // span the custid Cell
E. Item. cells [1]. horizontalalign = horizontalalign. Right;
E. Item. cells [0]. Text = "total is ";
E. Item. cells [0]. attributes ["onmouseover"] = "javascript: This. style. backgroundcolor = '# aaaadd ';";
}
} Catch
{}}//}
} // <Summary>
///
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
// Public void itemdatabound (Object sender, datagriditemeventargs E)
//{
/// Foreach (datagriditem e in grid. Items)
////{
// Datarowview DRV = (datarowview) E. Item. dataitem;
// If (DRV = NULL)
// Return; // If (INT) DRV ["myorderid"] =-1)
//{
// If (DRV ["mycustomerid"]. tostring () = "(total )")
//{
// E. Item. backcolor = color. Yellow;
// E. Item. cells [0]. Text = "orders total ";
//}
// Else
// E. Item. cells [0]. Text = "customer subtotal ";
//}
////}
//}
Protected void grid_itemdatabound (Object sender, datagriditemeventargs E)
{
Datarowview DRV = (datarowview) E. Item. dataitem;
If (DRV = NULL)
Return; If (INT) DRV ["myorderid"] =-1)
{
If (DRV ["mycustid"]. tostring () = "(total )")
{
E. Item. backcolor = color. Yellow;
E. Item. cells [0]. Text = "orders total ";
E. Item. cells [0]. attributes ["onmouseover"] = "javascript: This. style. backgroundcolor = '# aaaadd ';";
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.