Retrieve the ID of a DataTable within a GridView

2

I'm trying to rescue the ID from my client that is in GridView , but I'm using DataTable to fill 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 is 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>
    
asked by anonymous 28.04.2014 / 17:00

1 answer

2

In your GridView , a TemplateField was added that has as CommandName="PegarID" and CommandSource='<%# Eval("ID")%>' with the value of ID per row.

By clicking on the LinkButton that is within TemplateField the gdvCliente_RowCommand will redeem on each click the corresponding ID and show 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();
     }
}

This is a way to retrieve the corresponding ID of each line.

  

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

    
28.04.2014 / 17:24