Redeem a Datatable ID inside a Gridview

Asked

Viewed 424 times

2

I’m trying to rescue the ID of my client who is in a GridView, but I’m using a DataTable to fill the GridView.

Datatable

public DataTable SelecionarDT()
{
    objDAO = new ClienteDAO();
    DataRow dr;
    DataTable dt = new DataTable();
    IList<Cliente> lista = new List<Cliente>();
    dt.Columns.Add("ID");
    dt.Columns.Add("Nome");
    dt.Columns.Add("CPF/CNPJ");
    dt.Columns.Add("Telefone");
    lista = objDAO.Buscar();
    if (lista != null)
    {
        foreach (Cliente cliente in lista)
        {
            dt.DefaultView.Sort = "Nome";
            dr = dt.NewRow();
            dr["ID"]= cliente._ClienteID;
            dr["Nome"] = cliente._Nome;
            dr["CPF/CNPJ"] = cliente._Cpf_cnpj;
            dr["Telefone"] = cliente._Telefone;
            dt.Rows.Add(dr);
        }
    }
    return dt;
}

This one and Gridview

<asp:GridView ID="gdvCliente" runat="server" CssClass="table table-condensed" AllowPaging="True" CellPadding="4" ForeColor="#333333" GridLines="None" OnPageIndexChanging="gdvCliente_PageIndexChanging" PageSize="5" DataKeyNames="ID" OnRowCommand="gdvCliente_RowCommand">
        <AlternatingRowStyle BackColor="White" />
        <Columns>
            <asp:CommandField EditText="" SelectText="" ShowSelectButton="True" ControlStyle-CssClass="glyphicon glyphicon-edit" ControlStyle-Width="1" />
        </Columns>
        <EditRowStyle BackColor="#2461BF" />
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <PagerStyle CssClass="" BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#EFF3FB" />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#F5F7FB" />
        <SortedAscendingHeaderStyle BackColor="#6D95E1" />
        <SortedDescendingCellStyle BackColor="#E9EBEF" />
        <SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>

1 answer

2

In his Gridview, an added TemplateField that has as CommandName="PegarID" and CommandSource='<%# Eval("ID")%>' with the value of ID per line (Row).

By clicking on LinkButton that’s inside the TemplateField gdvCliente_RowCommand will redeem the corresponding ID on each click and display on a Label (LblId).

    <asp:GridView ID="gdvCliente" runat="server" CssClass="table table-condensed" AllowPaging="True" CellPadding="4" ForeColor="#333333" GridLines="None" OnPageIndexChanging="gdvCliente_PageIndexChanging" PageSize="5" DataKeyNames="ID" OnRowCommand="gdvCliente_RowCommand">
                <AlternatingRowStyle BackColor="White" />
                <Columns>
                    <asp:TemplateField HeaderText="Pegar ID" ShowHeader="False">
                        <ItemTemplate>
                            <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="false" CommandName="PegarID"  CommandArgument='<%# Eval("ID") %>' Text="Pegar"></asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <EditRowStyle BackColor="#2461BF" />
                <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <PagerStyle CssClass="" BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                <RowStyle BackColor="#EFF3FB" />
                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                <SortedAscendingCellStyle BackColor="#F5F7FB" />
                <SortedAscendingHeaderStyle BackColor="#6D95E1" />
                <SortedDescendingCellStyle BackColor="#E9EBEF" />
                <SortedDescendingHeaderStyle BackColor="#4870BE" />    
    </asp:GridView>

protected void gdvCliente_RowCommand(object sender, GridViewCommandEventArgs e)
{
     if (e.CommandName.Equals("PegarID"))
     {
         LblId.Text = e.CommandArgument.ToString();
     }
}

It is a way to rescue the corresponding ID of each line.

Obs: your routine of conversion has no purpose, you could play straight to objDao.Busca() to the DataSource of GridView that has the same effect, eliminating there an unnecessary conversion, increasing performace and ensuring a more standard code.

Browser other questions tagged

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