i have a gridview that I need to have paged. When I allowPaging=true, It shows 145678910.... It won't show pages 2 and 3. If I try to change the Mode to NextPreviousFirstLast, It only shows 'Next' on the first page, and if I click that, it only shows 'First' and 'Last' on the second page. Here is the code:
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="Black" AutoGenerateColumns="False" BackColor="#CCCCCC"
BorderColor="#666666" OnRowDataBound = "HandleRowDataBound" BorderStyle="Solid" BorderWidth="1px" CellSpacing="2" Width ="942px"
Font-Names="Arial" Font-Size="8pt" ShowHeaderWhenEmpty="True" CSSClass="mytable" AllowPaging="true" OnPageIndexChanging="GridView1_PageIndexChanging"
PageSize="25">
<PagerSettings Mode="NextPreviousFirstLast" FirstPageText="First" PreviousPageText="Previous" NextPageText="Next" LastPageText="Last" />
<Columns>
<asp:TemplateField HeaderText="Select" ControlStyle-Width = "30px">
<ItemTemplate>
<asp:CheckBox runat="server" ID="DiscontinuedCheckBox" CssClass="SelectCheckBox" />
</ItemTemplate>
<ControlStyle Width="30px"></ControlStyle>
<ItemStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Recall Qty" ItemStyle-Wrap="true" ItemStyle-Width="100px" >
<ItemTemplate >
<img id="star1" runat ="server" src="../Images/red_star.gif" alt="Red Star" class="RedStars"/>
<asp:TextBox runat ="server" ID="textBoxQty" Width = "50px" Text ="1" ReadOnly="false" />
</ItemTemplate>
<HeaderStyle Width = "100px" />
<ItemStyle Width="100px" />
<ItemStyle Wrap="False" Width="60px"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delivery Location" >
<ItemTemplate >
<div style="width:200px">
<div style ="float : left; ">
<asp:CheckBox runat="server" ID="packShipCB" Text ="Pack Ship or " CssClass="SendToShip" AutoPostBack="false" />
</div>
<asp:TextBox runat ="server" ID="deliveryLocationTB" CssClass="AllDelivery" ReadOnly ="false" Width="80px" />
</div>
</ItemTemplate>
<HeaderStyle Width = "200px" />
<ItemStyle Width="200px" />
<ItemStyle Width ="160px" />
</asp:TemplateField>
<%-- <asp:BoundField HeaderText="Data" />--%>
<asp:BoundField HeaderText="Request Number" DataField="requestNumber" ItemStyle-Width = "50px">
<HeaderStyle Wrap="True" Width="50px" />
<ItemStyle Wrap="True" Width="50px" />
</asp:BoundField>
<asp:BoundField HeaderText="Item Number" DataField="materialLineItemID" />
<asp:BoundField HeaderText="Program" DataField="programName" >
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField HeaderText="Program POC" DataField="programPOC" >
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField HeaderText="Material Number" DataField="materialNumber" ItemStyle-Width="100px">
<HeaderStyle Wrap="False" />
</asp:BoundField >
<asp:BoundField HeaderText="Qty in Storage" DataField="availableQty" ItemStyle-Width="100px">
<ItemStyle Width="100px" Wrap="False"></ItemStyle>
</asp:BoundField >
<asp:BoundField HeaderText="Description" DataField="Description" ItemStyle-Width="300px" >
<ItemStyle Width="300px" Wrap="False"></ItemStyle>
</asp:BoundField>
<asp:BoundField HeaderText="Serialized" DataField="Serialized" ItemStyle-Width="100px">
<ItemStyle Width="100px"></ItemStyle>
</asp:BoundField>
<asp:BoundField HeaderText="Serial Number" DataField="serialNumber" ItemStyle-Width="200px">
<ItemStyle Width="200px" Wrap="False"></ItemStyle>
</asp:BoundField>
<asp:BoundField HeaderText="Shelf Life" DataField="shelfLIfe" DataFormatString="{0:d}" HtmlEncode="false" ItemStyle-Width="100px" >
<ItemStyle Width="100px"></ItemStyle>
</asp:BoundField>
<asp:BoundField HeaderText="Cost Center" DataField="costCenter" ItemStyle-Width="200px">
<ItemStyle Width="200px" Wrap="False"></ItemStyle>
</asp:BoundField>
<asp:BoundField HeaderText="Charge Number" DataField="chargeNumber" ItemStyle-Width="200px">
<ItemStyle Width="200px" Wrap="False"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="contractNumber" HeaderText="Contract Number" ItemStyle-Width="200px">
<HeaderStyle Wrap="False" />
<ItemStyle Width="200px" />
</asp:BoundField>
<asp:BoundField DataField="assetTagNumber" HeaderText="Asset Tag Number" ItemStyle-Width="200px">
<HeaderStyle Wrap="False" />
<ItemStyle Width="200px" />
</asp:BoundField>
<asp:BoundField HeaderText="Storage Duration" DataField="Duration" ItemStyle-Width="200px">
<ItemStyle Width="200px" Wrap="False"></ItemStyle>
</asp:BoundField>
<asp:BoundField HeaderText="Handling Unit Number" DataField="handlingUnitNumber" ItemStyle-Width="100px">
<ItemStyle Width="100px" Wrap="False"></ItemStyle>
</asp:BoundField >
<asp:BoundField HeaderText="Storage Location" DataField="storageLocation" ItemStyle-Width="100px">
<ItemStyle Width="100px" Wrap="False"></ItemStyle>
</asp:BoundField >
<asp:BoundField HeaderText="Time in Storage" DataField="storageTime" ItemStyle-Width="200px">
<ItemStyle Width="200px" Wrap="False"></ItemStyle>
</asp:BoundField>
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="#999999" Font-Bold="True" ForeColor="#880C1B"
HorizontalAlign="Center" Font-Size="9pt" Wrap="true" BorderStyle="Solid" VerticalAlign="Middle"
BorderWidth="1px"/>
<PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
<RowStyle BackColor="White" HorizontalAlign="Center" CssClass="Unselected"
Wrap="false" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#808080" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
</asp:GridView>
and here is the code behind:
protected void Page_Load(object sender, EventArgs e)
{
siteID = int.Parse(Request.QueryString["s"].ToString());
FillDetails();
}
//Pulls data from database for items to be recalled based on site, and user rights and enables to functions of the controls
private void FillDetails()
{
DataSet materials = RequestDB2.GetStoredMaterialsView(siteID);
GridView1.DataSource = materials;
GridView1.DataBind();
if (GridView1.Rows.Count >= 9)
paneling.Style["height"] = "300px";
//if not items exit
if (materials == null || materials.Tables[0].Rows.Count == 0)
{
Error.InnerText = "You have no materials to be recalled from this site.";
}
else
{
//Adds thead and tbody tags
GridView1.HeaderRow.TableSection = TableRowSection.TableHeader;
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
protected void HandleRowDataBound(object sender, GridViewRowEventArgs e)
{
}
Here is what I see:
WHen I look at firebug, I see that pages 2 and 3 have display:none. ANy thoughts on why this might be happening?
You are not assigning data source in GridView1_PageIndexChanging. You need to assigned datasource before binding. You better try calling FillDetails()
Change
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
To
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
FillDetails();
}
Try using PagerSetting as..
<PagerSettings FirstPageText="First Page" LastPageText="Last Page"
Mode="NumericFirstLast" Position="TopAndBottom" />
protected void grdView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.DataSource = RequestDB2.GetStoredMaterialsView(siteID);
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
Related
I'm new to ASP MVC. I want to load data from a database in DropDownList into a gridview. But DATA cannot load.
HTML CODE
<asp:BoundField DataField="id" HeaderText="Code">
<HeaderStyle Width="106px" HorizontalAlign="center" BorderColor="White" BorderWidth="1" />
<ItemStyle Width="102px" HorizontalAlign="center" BorderWidth="1px" />
</asp:BoundField>
<asp:BoundField DataField="name" HeaderText="Nom Cours">
<HeaderStyle Width="300px" HorizontalAlign="left" BorderColor="White" BorderWidth="1" />
<ItemStyle Width="300px" HorizontalAlign="left" BorderWidth="1px" />
</asp:BoundField>
<asp:templatefield HeaderText="Prix / Heure (HTG)">
<HeaderStyle Width="160px" HorizontalAlign="center" BorderColor="White" BorderWidth="1" />
<ItemStyle Width="160px" HorizontalAlign="center" BorderWidth="1px" />
<itemtemplate>
<asp:DropDownList ID="ddlprice" Width="160px" HorizontalAlign="center" runat="server">
</asp:DropDownList>
</itemtemplate>
</asp:templatefield>
<asp:templatefield HeaderText="Sélectionner">
<HeaderStyle Width="300px" HorizontalAlign="center" BorderColor="White" BorderWidth="1" />
<ItemStyle Width="300px" HorizontalAlign="center" BorderWidth="1px" />
<itemtemplate>
<asp:checkbox ID="cbSelect" CssClass="gridCB" runat="server" HorizontalAlign="center"></asp:checkbox>
</itemtemplate>
</asp:templatefield>
</Columns>
<FooterStyle BackColor="Tan" Height="30px" HorizontalAlign="Center" />
<HeaderStyle BackColor="Navy" Font-Bold="True" Height="22px" HorizontalAlign="Left"
ForeColor="WhiteSmoke" BorderColor="Navy" VerticalAlign="Top" BorderWidth="2px" Width="910px" Font-Size="Small" />
<PagerSettings Mode="NumericFirstLast" />
<PagerStyle BackColor="SkyBlue" ForeColor="WhiteSmoke"
HorizontalAlign="Center" />
<RowStyle Height="5px" Font-Size="Smaller" />
<SelectedRowStyle BackColor="Aquamarine" ForeColor="GhostWhite" BorderColor="Silver"
BorderStyle="None" />
<SortedAscendingCellStyle BackColor="SkyBlue" />
<SortedAscendingHeaderStyle BackColor="#DAC09E" />
<SortedDescendingCellStyle BackColor="#E1DB9C" />
<SortedDescendingHeaderStyle BackColor="#C2A47B" />
</asp:GridView>
</div>
C# CODE
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ddlClassroom.SelectedValue = "-1";
BindDataGridClass();
BindDataGridCourse();
loadListPrice();
}
private void loadListPrice()
{
List<Course> listPrice = Course.getListCoursePriceOrdered();
ddlprice.DataValueField = "id";
ddlprice.DataTextField = "price";
ddlprice.DataSource = listPrice;
ddlprice.DataBind();
//ddlprice.Items.Insert(0, new DropDownListItem("--Selectionner--", "-1"));
ddlprice.SelectedValue = "0";
}
First add the OnRowDataBound event to the GridView
<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound">
Then in code behind
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//check if the row is a datarow
if (e.Row.RowType == DataControlRowType.DataRow)
{
//find the dropdownlist in the row with findcontrol and cast it back to one
DropDownList ddlprice = e.Row.FindControl("ddlprice") as DropDownList;
//you now have access to all the dropdownlist properties
ddlprice.DataSource = Course.getListCoursePriceOrdered();
ddlprice.DataValueField = "id";
ddlprice.DataTextField = "price";
ddlprice.DataBind();
ddlprice.SelectedValue = "0";
}
}
I'm applying edit functionality on grid view with object data source.I want to take previous value when i click edit button that's need for another purpose here i'm trying to write code but it gives current value.how to get previous value.......hope you can help me
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = (GridView1.Rows[e.RowIndex] as GridViewRow);
string totalQuantity = (row.Cells[5].Controls[0] as TextBox).Text;//It gives current value but i want previous value
string totalPurchasePrice = (row.Cells[6].Controls[0] as TextBox).Text; //It gives current value but i want previous value
GridView1.EditIndex = -1;
//value store in session start
Session["totalQuantity"] = totalQuantity;
Session["totalPurchasePrice"] = totalPurchasePrice;
//value store in session end
}
and grid view code
<asp:GridView ID="GridView1" runat="server" CssClass="dataGridTable" AutoGenerateColumns="False" Width="100%" AllowPaging="True" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataSourceID="ObjectDataSource1" HeaderStyle-Height="30" OnRowCreated="GridView1_RowCreated" PageSize="30" DataKeyNames="invoiceNumber,productName,productCategory" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnRowUpdating="GridView1_RowUpdating">
<Columns>
<asp:CommandField ShowEditButton="true" ShowSelectButton="True" />
<asp:CommandField ShowDeleteButton="true"/>
<asp:BoundField DataField="invoiceNumber" HeaderText="Invoice" ReadOnly="true" SortExpression="invoiceNumber" />
<asp:BoundField DataField="productName" HeaderText="Name" ReadOnly="true" SortExpression="productName" />
<asp:BoundField DataField="productCategory" HeaderText="Category" ReadOnly="true" SortExpression="productCategory" />
<asp:BoundField DataField="totalQuantity" HeaderText="Quantity" SortExpression="totalQuantity" />
<asp:BoundField DataField="totalPurchasePrice" HeaderText="Total Price" SortExpression="totalPurchasePrice" />
<asp:BoundField DataField="salePricePerItem" HeaderText="Sale Price/Item" SortExpression="salePricePerItem" />
<asp:BoundField DataField="comments" HeaderText="Comments" SortExpression="comments" />
<asp:BoundField DataField="date" HeaderText="Date" ReadOnly="true" SortExpression="date" />
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" Height="24px" ImageUrl="~/Images/detailsInfo.png" Width="24px" OnClick="ImageButton1_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" CssClass="gridHeaderAlignment" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<RowStyle ForeColor="#000066" HorizontalAlign="Center" VerticalAlign="Middle" />
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>
I want to show Checkbox in Gridview,
But now, it only show dynamic data in Gridview,
Didn't show dynamic checkbox in Gridview.
How can I fix problem, thanks.
.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
using (var conn = new SqlConnection(strConn))
{
try
{
conn.Open();
ckloginConnectionString.SelectCommand = #"select a,b,c,d ,e from testTable where a =#a ";
ckloginConnectionString.SelectParameters.Add("a", a);
//==================
DataView dv = (DataView)ckloginConnectionString.Select(new DataSourceSelectArguments());
GridView1.DataSource = dv;
GridView1.DataBind();
//==================
ckloginConnectionString.DataBind();
ckloginConnectionString.DataBind();
}
catch (Exception ex)
{
ex.ToString();
}
}
}
.aspx
<div class="table-wrapper">
<asp:GridView runat="server" ID="GridView1" CssClass="alt" DataKeyNames="InsertedDate,Upload_Schoo_No" AutoGenerateColumns="False" OnRowCommand="grvclscour_RowCommand">
<Columns>
<asp:TemplateField HeaderText="check" InsertVisible="false" ItemStyle-BorderStyle="Double">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Visible="true" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="a" HeaderText="a">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="b" HeaderText="b">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="c" HeaderText="c">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:ButtonField CommandName="d" HeaderText="d">
<ItemStyle HorizontalAlign="Center" />
</asp:ButtonField>
<asp:ButtonField CommandName="e" HeaderText="e">
<ItemStyle HorizontalAlign="Center" />
</asp:ButtonField>
</Columns>
</asp:GridView>
</div>
Eval would do what you want i guess.
change your grid desing like this:
<asp:GridView runat="server" ID="GridView1" CssClass="alt" DataKeyNames="InsertedDate,Upload_Schoo_No" AutoGenerateColumns="False" OnRowCommand="grvclscour_RowCommand">
<Columns>
<asp:TemplateField HeaderText="check" InsertVisible="false" ItemStyle-BorderStyle="Double">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Visible="true" Checked='<%# Eval("YourDataField") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="a" HeaderText="a">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="b" HeaderText="b">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="c" HeaderText="c">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:ButtonField CommandName="d" HeaderText="d">
<ItemStyle HorizontalAlign="Center" />
</asp:ButtonField>
<asp:ButtonField CommandName="e" HeaderText="e">
<ItemStyle HorizontalAlign="Center" />
</asp:ButtonField>
</Columns>
</asp:GridView>
SQL Server table structure:
ChapName varchar(200)
Status varchar(1)
Requirement:
I want to display checkbox in an ASP.NET gridview from Visual Studio 2010
if the value of status column is T, let it be checked and unchecked otherwise.
but it shows only textbox.
I have tried <asp:templatefield> and <asp:itemtemplate> but it throws error if I try to bind this checkbox.
any sample code is required as I am beginner in this field.
The code I tried:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
CheckBox c = (CheckBox)GridView1.FindControl("ChkStatus");
TextBox TB = (TextBox)GridView1.FindControl("Status");
//Response.Write(TB.Text);
if (TB.Text == "T")
{
c.Checked = true;
}
else
{
c.Checked = false;
}
}
The error I got
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web
request. Please review the stack trace for more information about the
error and where it originated in the code.
Exception Details:
System.NullReferenceException: Object reference not set to an instance
of an object.
Aspx markup:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="Comp,QTypeCode" DataSourceID="SDS_QType_Edit"
BackColor="White" BorderColor="#DEDFDE" BorderStyle="None"
BorderWidth="1px"
CellPadding="4" ForeColor="Black" GridLines="Vertical"
AllowPaging="True" AllowSorting="True"
onselectedindexchanged="GridView1_SelectedIndexChanged"
onrowdatabound="GridView1_RowDataBound">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:CommandField ShowEditButton="True" ShowSelectButton="True" />
<asp:BoundField DataField="QTypeCode" HeaderText="QTypeCode"
SortExpression="QTypeCode" InsertVisible="False"
ReadOnly="True" />
<asp:BoundField DataField="Descr" HeaderText="Descr" SortExpression="Descr" />
<asp:CheckBoxField DataField="AnsReq" HeaderText="AnsReq" ReadOnly="True"
SortExpression="AnsReq" />
<asp:CheckBoxField DataField="OptionPrint" HeaderText="OptionPrint"
ReadOnly="True" SortExpression="OptionPrint" />
<asp:BoundField DataField="Status" HeaderText="Status"
SortExpression="Status" />
<asp:BoundField DataField="TransDate" HeaderText="TransDate"
SortExpression="TransDate" />
<asp:BoundField DataField="UserName" HeaderText="UserName"
SortExpression="UserName" />
<asp:TemplateField HeaderText="Check Box" >
<ItemTemplate>
<asp:CheckBox ID ="ChkStatus" Text="text" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle Wrap="False" />
<EmptyDataRowStyle Wrap="False" />
<FooterStyle BackColor="#CCCC99" />
<HeaderStyle Wrap="False" BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle Wrap="False" BackColor="#F7F7DE" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White"
Wrap="False" />
<SortedAscendingCellStyle BackColor="#FBFBF2" />
<SortedAscendingHeaderStyle BackColor="#848384" />
<SortedDescendingCellStyle BackColor="#EAEAD3" />
<SortedDescendingHeaderStyle BackColor="#575357" />
</asp:GridView>
Assume you have grid defined as below on asmx
<asp:GridView ID="GridView1" runat="server"
onrowdatabound="GridView1_RowDataBound" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="ChapName" HeaderText="ChapName" />
<asp:TemplateField HeaderText="Status" Visible ="false">
<ItemTemplate>
<asp:Label ID="Status" runat="server" Text='<%# Bind("Status") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Check Box" >
<ItemTemplate>
<asp:CheckBox ID ="ChkStatus" Text="text" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
on Row Data Bound event you can find controls as below
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
CheckBox c = e.Row.FindControl("ChkStatus") as CheckBox;
Label lbl = e.Row.FindControl("Status") as Label;
if (c!= null && lbl != null )
{
c.Checked = (lbl.Text == "T");
}
}
I have created an event handler for the OnSortCommand of DataGrid:
<asp:DataGrid id="dtgBatches" runat="server" Width="100%" CssClass="intTable" EnableViewState="False" DataKeyField="bat_GUID"
GridLines="Horizontal" AllowSorting="True" AutoGenerateColumns="False" AllowPaging="False" >
<SelectedItemStyle BackColor="#FFFF99"></SelectedItemStyle>
<AlternatingItemStyle CssClass="intTableEntry"></AlternatingItemStyle>
<ItemStyle CssClass="intTableEntry2"></ItemStyle>
<HeaderStyle ForeColor="Black" CssClass="tableHeader"></HeaderStyle>
<Columns>
<asp:TemplateColumn >
<HeaderStyle Width="5%"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<img src="../images/icons/cog.png" onclick="universalAlert('Loading...',4,false); ViewBatch('<%# DataBinder.Eval(Container.DataItem, "bat_GUID") %>')" alt="view"/>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="bat_Name" SortExpression="bat_Name" HeaderText="<%$ Resources:AI360Resource, lnkbtn_Name %>">
<HeaderStyle Width="10%"></HeaderStyle>
<ItemStyle HorizontalAlign="Left"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="bat_Id" SortExpression="bat_Id" HeaderText="<%$ Resources:AI360Resource, ltxt_ID %>">
<HeaderStyle Width="10%"></HeaderStyle>
<ItemStyle HorizontalAlign="Left"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="bat_Date" SortExpression="bat_Date" HeaderText="<%$ Resources:AI360Resource, alt_date %>" DataFormatString="{0:d}">
<HeaderStyle HorizontalAlign="right" Width="10%"></HeaderStyle>
<ItemStyle HorizontalAlign="Right"></ItemStyle>
</asp:BoundColumn>
<asp:TemplateColumn>
<HeaderStyle Width="1%"></HeaderStyle>
</asp:TemplateColumn>
<asp:BoundColumn DataField="bat_Close_date" SortExpression="bat_Close_date" HeaderText="<%$ Resources:AI360Resource, ltxt_closed %>" DataFormatString="{0:d}">
<HeaderStyle Width="29%"></HeaderStyle>
<ItemStyle HorizontalAlign="Left"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="bat_Cont_Amount" SortExpression="bat_Cont_Amount" HeaderText="<%$ Resources:AI360Resource, alt_receipts %>" DataFormatString="{0:c}">
<HeaderStyle Width="10%"></HeaderStyle>
<ItemStyle HorizontalAlign="Left"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="bat_Disb_Amount" SortExpression="bat_Disb_Amount" HeaderText="<%$ Resources:AI360Resource, alt_disb %>" DataFormatString="{0:c}">
<HeaderStyle Width="25%"></HeaderStyle>
<ItemStyle HorizontalAlign="Left"></ItemStyle>
</asp:BoundColumn>
</Columns>
</asp:DataGrid>
And the code of handler is as below:
protected void dtgBatches_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
string strCurrentSort = dtgBatches.Attributes["SortExpr"];
string strNewSort = e.SortExpression;
if ((strCurrentSort != null) && (strCurrentSort == strNewSort))
{
//reverse direction
strNewSort += " DESC";
}
// Code to Set DataView dv
dv.Sort = strNewSort;
dtgBatches.DataSource = dv;
dtgBatches.DataBind();
}
The problem is that the handler never executes.
The registartion for the handler is done as below:
private void InitializeComponent()
{
this.dtgBatches.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.dtgBatches_SortCommand);
}
ViewState needs to be enabled for sorting.
http://msdn.microsoft.com/en-us/library/ms972427.aspx