GridView RowDataBound not called after postback

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to modify the layout of a row based on the value in some data (I
am creating a subtotal line). This works fine when the page is first loaded,
but when it's reloaded, the wrong column is restored.

<asp:GridView ID="grdvOrder" runat="server" AutoGenerateColumns="False"
OnRowCommand="grdvOrder_RowCommand" OnRowDataBound="grdvOrder_RowDataBound"
OnRowCreated="grdvOrder_RowCreated">
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server"
CausesValidation="false" CommandName="DeleteLine"
ImageUrl="~/Images/btn_delete.gif" Text="" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox ID="txtQty" runat="server" Columns="4"
Text='<%# Bind("QtyOrdered") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:HyperLinkField DataNavigateUrlFields="Sku"
DataNavigateUrlFormatString="product/{0}"
DataTextField="ProductName" HeaderText="Product" />
<asp:BoundField DataField="ProductId" HeaderText="Id" />
<asp:BoundField DataField="Price" HeaderText="Price"
DataFormatString="{0:c}" HtmlEncode="False" />
<asp:BoundField DataField="ExtPrice" HeaderText="Ext. Price"
DataFormatString="{0:c}" HtmlEncode="False" />
</Columns>
</asp:GridView>


protected void grdvOrder_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Check for sub-total line
DataRowView rowView = (DataRowView)e.Row.DataItem;
if (Convert.ToInt32(rowView["OrderLineId"]) == 0)
{
// Remove columns 0, 1, 3, 4
e.Row.Cells.RemoveAt(0);
e.Row.Cells.RemoveAt(0);
e.Row.Cells.RemoveAt(1);
e.Row.Cells.RemoveAt(1);
// Make column 2 span columns 1-4
e.Row.Cells[0].ColumnSpan = 5;
// Remove the hyperlink from the subtotal line
HyperLink lnk = (HyperLink) e.Row.Cells[0].Controls[0];
lnk.NavigateUrl = null;
}
}
}

On postback, RowDataBound is not called and the columns are all wrong.

I tried the RowCreated event, which did get called on postbacks, but then
the HyperLink control was not defined so setting the NavigateUrl to null had
no effect, and on postback got a null for (DataRowView)e.Row.DataItem,
causing an exception.

Any recommendations on how I can adjust the contents based on the data in
the row?

Thanks in advance,

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top