Problem when viewing data from a grid within code Behind

Asked

Viewed 179 times

0

On a Gridview I’m trying to get the data referring to a line where the checkbox == true.

I can get the value of the checkbox, but I can’t get the other values for the line.

while (i < gvwNot.Rows.Count)
{
    GridViewRow row = gvwNot.Rows[i];
    CheckBox isChecked = (CheckBox)row.FindControl("chkSelect");
    //TextBox id = (TextBox)row.FindControl("ID");
    //TextBox nome = (TextBox)row.FindControl("NOME");

    if (isChecked.Checked == true)
    {
        string id = gvwNot.Rows[i].Cells[1].Text;
        string nome = gvwNot.Rows[i].Cells[3].Text;

        qry = sb.ToString();
        qtd = qtd + 1;
    }

    i++;
}



<asp:GridView ID="gvwNot" runat="server"
    AutoGenerateColumns="False"
    DataKeyNames="Id"
    AllowPaging="True"
    AllowSorting="true"
    PageSize="10"
    CssClass="table table-bordered table-hover table-striped" >

    <Columns>
         <asp:TemplateField HeaderText="Select">
            <ItemTemplate>
                <asp:CheckBox ID="chkSelect" runat="server" />
            </ItemTemplate>
            <HeaderTemplate>
                <input id="chkAll" onclick="javascript: SelecionaTodosChecks(this);" runat="server" type="checkbox"  />
            </HeaderTemplate>
        </asp:TemplateField>


        <asp:TemplateField HeaderText="Id">
            <ItemTemplate><%#Eval("ID") %>    </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Protocolo">
            <ItemTemplate><%#Eval("NOME") %>    </ItemTemplate>
        </asp:TemplateField>

    </Columns>
    <PagerSettings Mode="NextPrevious" NextPageText="Próximo" PreviousPageText="Anterior" />
</asp:GridView>
  • You speak the ID and the name?

  • @Marconi Yes, in code behide, it appears empty.

  • Is called some event from some button to run this code?

  • yes, that’s inside the Onclick button, where tb have an Onclientclick with a Javascript validation where it checks if the field was typed.

  • Got it, your button is inside the grid or outside?

  • Out of the gridview.

  • Have a look here. There’s even a demo. http://www.aspsnippets.com/Articles/GridView-with-CheckBox-Get-Selected-Rows-ASPNet.aspx

Show 3 more comments

1 answer

0

From what I understand you need only show the ID and the Name on your gridview. You can change to Boundfield, which will show you the text and when you try to access the Behind code it will work normally. Change your Id and Name code snippet to the snippet below:

<asp:BoundField DataField="ID" HeaderText="Id" ItemStyle-HorizontalAlign="Center" />

<asp:BoundField DataField="NOME" HeaderText="Protocolo" ItemStyle-HorizontalAlign="Center" />

Browser other questions tagged

You are not signed in. Login or sign up in order to post.