Krasimir
Krasimir

Reputation: 259

GridViewCosts.SelectedRow.Cells[1].Text dont work with itemTemplate

I have this gridView:

<div class ="gridView">
    <asp:GridView ID="GridViewCosts" runat="server" ShowFooter="True" ShowHeaderWhenEmpty="True"
        AutoGenerateColumns="False" OnRowDeleting="GridViewCosts_RowDeleting" Width="387px"
        OnSelectedIndexChanged="GridViewCosts_SelectedIndexChanged"
        OnPageIndexChanging="GridViewCosts_PageIndexChanging"
        AllowPaging="True"
        CssClass="mGrid"  
    PagerStyle-CssClass="pgr"  
    AlternatingRowStyle-CssClass="alt" PageSize="5">
<AlternatingRowStyle CssClass="alt"></AlternatingRowStyle>
        <Columns>
            <asp:BoundField DataField="Id" HeaderText="Номер" />
              <asp:TemplateField HeaderText="Стойност">
                  <EditItemTemplate>
                      <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Value") %>'></asp:TextBox>
                  </EditItemTemplate>
                  <ItemTemplate>
                      <asp:Label ID="Label2" runat="server" Text='<%# Bind("Value") %>'></asp:Label>
                  </ItemTemplate>
                  <ControlStyle Width="100px" />
            </asp:TemplateField>

And in code behind i Want :

 protected void GridViewCosts_SelectedIndexChanged(object sender, EventArgs e)
        {
            TextBoxValue.Text = GridViewCosts.SelectedRow.Cells[1].Text;
}

this is not working when the column Value is TemplateField. If the column is BoundFIeld it's working . What should i do ?

Upvotes: 0

Views: 1889

Answers (2)

Akshay Joy
Akshay Joy

Reputation: 1765

Try this sample code

For Each gvr As GridViewRow In gvInvoice.Rows
 Dim TctValue As TextBox= DirectCast(GridViewCosts.Cells(1).FindControl("TextBox2"), TextBox)
 totamount = totamount + Convert.ToDouble(lblAmount.Text)
Next
txtTotal.Text = totamount.ToString() 

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460208

If you use TemplateFields you have to use FindControl to get a reference to the control:

Label Label2 = (Label)GridViewCosts.SelectedRow.FindControl("Label2");
TextBoxValue.Text = Label2.Text;

Upvotes: 1

Related Questions