tags:

views:

238

answers:

2

hi,

i am usin rowdatabound event of gridview to do some formating in gridview column. But when i execute the code and debug it using immediate window i do not get anything in  e.Row.Cells[1].Text. I am populating the gridview from a datatable. It is displaying the records but i dont know why is it not getting in rowdatabound.  

following is my code for binding

<asp:GridView runat="server" AutoGenerateColumns="False"  
                        ID="gviewTemplate" onrowdatabound="gviewTemplate_RowDataBound" DataKeyNames="F1"
                        onrowcommand="gviewTemplate_RowCommand" 
                        onrowediting="gviewTemplate_RowEditing" 
                        onrowcancelingedit="gviewTemplate_RowCancelingEdit" 
                        onrowupdating="gviewTemplate_RowUpdating">
                        <Columns>
                            <asp:TemplateField>
                                <EditItemTemplate>
                                    <asp:Label ID="lblID" runat="server" Text='<%# Bind("F1") %>'></asp:Label>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label Runat="server" Text='<%# Bind("F1") %>' ID="lblID1"></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Uploaded Image">
                            <EditItemTemplate>
                                <asp:LinkButton Text="Reload" runat="server" CommandArgument='<%# Bind("F1") %>' CommandName="reload" ID="lbtnReloadImage"></asp:LinkButton>
                            </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label Runat="server" Text='<%# Eval("Uploaded") %>' ID="lblUploaded"></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Template Name">
                                <ItemStyle VerticalAlign="Top" HorizontalAlign="Center" />
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtTemplateName" Width="50" Runat="server" Text='<%# Eval("F2") %>'></asp:TextBox>
                                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" Runat="server" 
                                    ErrorMessage="You must provide a Product Name." ControlToValidate="txtTemplateName">*</asp:RequiredFieldValidator>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblTemplateName" runat="server" Text='<%# Eval("F2") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Heading">
                                <ItemStyle VerticalAlign="Top" HorizontalAlign="Center" />
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtHeading" Runat="server" Width="50" Text='<%# Eval("F3") %>'></asp:TextBox>
                                    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" Runat="server" 
                                    ErrorMessage="You must provide a Product Name." ControlToValidate="txtHeading">*</asp:RequiredFieldValidator>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblHeading" runat="server" Text='<%# Eval("F3") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Coupon Text">
                                <ItemStyle VerticalAlign="Top" HorizontalAlign="Center" />
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtCouponText" Runat="server" Width="50" Text='<%# Bind("F4") %>'></asp:TextBox>
                                    <asp:RequiredFieldValidator ID="RequiredFieldValidator3" Runat="server" 
                                    ErrorMessage="You must provide a Product Name." ControlToValidate="txtCouponText">*</asp:RequiredFieldValidator>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label Runat="server" Text='<%# Bind("F4") %>' ID="lblCouponText"></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>

and this is what i am doing in rowdatabound

protected void gviewTemplate_RowDataBound(object sender, GridViewRowEventArgs e)
{
        if (e.Row.Cells[1].Text != e.Row.Cells[2].Text)
        {
            e.Row.BackColor = System.Drawing.Color.Red;
        }
 }

i cant understand if the records are being displayed in grid the why cant i get it in rowdatabound

A: 

Because you are using the template field in the grid , so try to debug and hold the cell then try to find the textbox inside it and then read it's value

Amgad Fahmi
but how will i cange the color of the cell background now
pankaj
you can put a cxontainer inside the template like table or panel and then apply the back color on it
Amgad Fahmi
ok thanks titan, i am going to try that now
pankaj
hi titan, thanks your code is working can u pl tell me one more thing is there a way by which i can make a table occupy all the space of an cell inside a grid. I mean the table is in a cell and i want it to take all the space of the table.
pankaj
A: 

try this:
you need to check for the rowtype because in row databound it also includes header,footer as well

protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow & (e.Row.RowState == DataControlRowState.Normal | e.Row.RowState == DataControlRowState.Alternate)) {
            //your code here
        }
    }
Pankaj Kumar