469,358 Members | 1,659 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,358 developers. It's quick & easy.

GridView RowDataBound not called after postback

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
Feb 17 '06 #1
0 5079

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Giovanni | last post: by
6 posts views Thread by Kevin Attard | last post: by
3 posts views Thread by ryan.mclean | last post: by
6 posts views Thread by AG | last post: by
1 post views Thread by =?Utf-8?B?V2VzbGV5IERhdmlzLCBHZW5lcmFsIER5bmFtaWNz | last post: by
reply views Thread by F | last post: by
17 posts views Thread by ata | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.