GridView RowDataBound not called after postback

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
 
Joined
Dec 14, 2010
Messages
1
Reaction score
0
Bind grid to call RowDataBound

You need to call Gridview.DataBind() method to raise RowDataBound event for a grid.
 

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

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top