I am facing a strange problem when using the gridview control in ASP.NET. I am using paging in it.
The second column in the grid is an “Order status” column which contains values such as “D”, “S”, etc. The “D” and “S” are database values representing “Delivered” and “Shipped”. We are using these representations to save space in the database.
Now, while displaying it in the GridView, I would like it to be displayed as “Displayed” or “Shipped” instead of the “D” and “S” which comes as part of the data source. When loading the page for the first time, I am handling it by getting the rows of the current page and changing the “D” and “S” of cells with “Delivered” and “Shipped” with the help of the Page Load Event.
But, the problem occurs when I go to the next page using the paging controls of the grid view.
The values in the “Order Status” column doesn’t get updated to the “D” and “S” in the second page and hence my function will fail to pick them up and fail to convert them to “Delivered” and “Shipped”.
I tried the OnPageIndexChanged event as well. Not working even with that 🙁
Here’s the code.
GRIDVIEW CONTROL
<asp:GridView ID="OrderGrid" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" DataSourceID="OrderHistory" AlternatingRowStyle-CssClass="alt"
CssClass="order_grid" DataKeyNames="order_id" OnPageIndexChanging="OrderGrid_PageIndexChanging">
SET ORDER STATUS METHOD
public void SetOrderStatus()
{
string Delivered = "Delivered";
string Shipped = "Shipped";
string Processing = "Processing";
for (int i = 0; i < OrderGrid.Rows.Count; i++)
{
if (OrderGrid.Rows[i].Cells[1].Text == "D")
{
OrderGrid.Rows[i].Cells[1].Text = Delivered;
OrderGrid.Rows[i].Cells[1].BackColor = System.Drawing.Color.FromName("#127b41");
OrderGrid.Rows[i].Cells[1].ForeColor = System.Drawing.Color.FromName("#fdfdfd");
}
if (OrderGrid.Rows[i].Cells[1].Text == "S")
{
OrderGrid.Rows[i].Cells[1].Text = Shipped;
}
if (OrderGrid.Rows[i].Cells[1].Text == "P")
{
OrderGrid.Rows[i].Cells[1].Text = Processing;
}
}
}
PAGE INDEX CHANGING FUNCTION
protected void OrderGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
OrderGrid.PageIndex = e.NewPageIndex;
OrderGrid.DataBind();
SetOrderStatus();
string o = OrderGrid.Rows[0].Cells[1].Text;
}
I would not normally post all my code, but I am desperate to get a solution to this problem. I would greatly appreciate any help that comes.
Thanks in advance!
Try updating this in the RowDataBound event. This allows you to make an adjustment at the binding of each row.
Here’s an example:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx