3

I have a Gridview loaded from a database. The Gridview has BoundFields, Buttons, Textboxes, and Dropdown lists. If I select any row but the first row I am able to collect the data from that row. If I select the first row I can see the 0 being pasted to the Method. The Dropdown list and the Textboxes give me their data, but the BoundFields do not.

ASP File

<asp:GridView ID="GridViewListComp" runat="server" AutoGenerateColumns="False" BackColor="White"
    BorderWidth="2px" Width="1500px" 
        onrowupdating="GridViewListComp_RowUpdated">

    <Columns>    
        <asp:BoundField DataField="First_Name" HeaderText="First Name" SortExpression="First_Name" />

        <asp:BoundField DataField="Last_Name" HeaderText="Last Name" SortExpression="Last_Name" />

        <asp:BoundField ItemStyle-Width="75px" DataField="DOB" HeaderText="Date of Birth"
            SortExpression="DOB" DataFormatString="{0:MM-dd-yy}" HtmlEncode="false">
            <ItemStyle Width="75px"></ItemStyle>
        </asp:BoundField>


        <%--Policy Text Box Code--%>
        <asp:TemplateField HeaderText="Policy">
            <ItemTemplate>
                <asp:TextBox ID="txtPolicy" runat="server" Text='<%# Eval("[POLICY_NUMBER]") %>'></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <%--Policy Text Box END--%>

    <%--DropDown Code--%>
        <asp:TemplateField HeaderText="Status">
            <ItemTemplate>
                <%-- <asp:DropDownList Width="50" runat="server" ID="ddlStatus" AutoPostBack="true" OnLoad="ddlStatus_onload" OnSelectedIndexChanged="ddlStatus_SelectedIndexChanged">--%>
                <asp:DropDownList Width="180" runat="server" ID="ddlStatus" AutoPostBack="false" >
                    <asp:ListItem Text="Review" Value="R"></asp:ListItem>
                    <asp:ListItem Text="Pending" Value="P"></asp:ListItem>
                    <asp:ListItem Text="Paid Without Policy" Value="W"></asp:ListItem>
                    <asp:ListItem Text="Ready to Pay" Value="T"></asp:ListItem>
                    <asp:ListItem Text="Deduct" Value="D"></asp:ListItem>
                    <asp:ListItem Text="Matched" Value="M"></asp:ListItem>
                    <asp:ListItem Text="Paid without Policy Matched" Value="O"></asp:ListItem>
                    <asp:ListItem Text="Exceeded Payment Override" Value="E"></asp:ListItem>
                    <asp:ListItem Text="Fixed Cost" Value="F"></asp:ListItem>
                    <asp:ListItem Text="Other Insured" Value="I"></asp:ListItem>
                </asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>
        <%--DropDown Code END--%>

        <asp:ButtonField ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle" ItemStyle-Width="80px" ControlStyle-Width="70px" 
        ControlStyle-Height="30px" ButtonType="Button" CommandName="Update" HeaderText="Invoice ID" ShowHeader="True" Text="Update" /> 

        <%--Paid Amount Text Box Code--%>
        <asp:TemplateField HeaderText="Paid Amount">
            <ItemTemplate>$<br />
                <asp:TextBox Width="75px" ID="PAID_AMT" runat="server" Text='<%# Eval("[PAID_AMT]") %>' ></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <%--Paid Amount Text Box END--%>

Code Behind File

protected void GridViewListComp_RowUpdated(object sender, GridViewUpdateEventArgs e)
    {

        int grIndex = e.RowIndex;
        GridViewRow rowGr = GridViewListComp.Rows[grIndex];
        string txtFName = GridViewListComp.Rows[grIndex].Cells[0].Text;
        string txtLName = GridViewListComp.Rows[grIndex].Cells[1].Text;
        string txtDOB = GridViewListComp.Rows[grIndex].Cells[2].Text;
        string txtPolicy = ((TextBox)GridViewListComp.Rows[grIndex].FindControl("txtPolicy")).Text; 

        DropDownList ddl = GridViewListComp.Rows[grIndex].FindControl("ddlStatus") as DropDownList;

        //gets the value of Status Dropdown list
        string valueStatus = ddl.SelectedValue.ToString(); //This gets the value of Status


        string txtPaidAmt = ((TextBox)GridViewListComp.Rows[grIndex].FindControl("PAID_AMT")).Text; 
MeGreeny
  • 49
  • 5
  • just off top of head without checking...do the bound fields need a "value" or "text" attribute set so that the DataField is set to the text/value attribute (i.e. Text="{0}"...) just a shot in the dark – Dave Jul 06 '16 at 16:45
  • Thanks Dave, I am not sure do you have an example of what that might look like. – MeGreeny Jul 07 '16 at 10:52
  • only thing I found is DataFormatString="{0}" so might not work...might have solution for you... – Dave Jul 07 '16 at 20:34
  • 1
    I found the issue it was another part of the code that was messing it up. – MeGreeny Aug 08 '16 at 14:57

1 Answers1

1

Try...

string txtFName = ((TextBox)GridViewListComp.Rows[grIndex].Cells[0].Controls[0]).Text;

...repeat same template for others...

From article...

http://forums.asp.net/t/1332603.aspx?How+to+retrive+Boundfield+value+of+gridview+on+rowupdating+event

HTH

Dave

Dave
  • 740
  • 1
  • 6
  • 17