1

Estou com o seguinte problema ao deletar uma linha do asp:gridview.

Quando eu deixo o código(chave) visible="false" no gridview, o valor retornado na minha variável chaveDelete vem empty. Mas quando eu deixo visible="true" a função pega o valor correto, alguém sabe de uma solução pra pegar o valor mesmo com o visible="false" da minha chave?

Protected Sub gvLivros_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles gvLivros.RowCommand
    Dim objLivros As New ClasseLivros

    If e.CommandName = "Excluir" Then
        Dim index As Integer = Integer.Parse(DirectCast(e.CommandArgument, String))
        Dim chaveDelete = gvLivros.Rows(index).Cells(0).Text

        objLivros.ExcluirLivro(Convert.ToInt32(chaveDelete))
    End If
End Sub
3
  • 1
    Conseguiu resolver o problema?
    – stderr
    17/05/2015 às 19:28
  • 1
    Funcionou perfeitamente cara, vlw!!!
    – WellDotCom
    17/05/2015 às 20:17
  • Fico feliz em ter ajudado :). Se possível marque a reposta como aceita.
    – stderr
    17/05/2015 às 20:19

1 Resposta 1

1

Uma das maneiras de fazer isto é usar a propriedade DataKeyNames do GridView. Para usá-la, basta especificar o nome da(s) coluna(s) em DataKeyNames.

<asp:GridView ID="gvLivros" runat="server" AutoGenerateColumns="false" DataKeyNames="Chave, Secao" OnRowCommand="gvLivros_RowCommand">
<Columns>
    <asp:BoundField DataField="Livro" HeaderText="Livro" ItemStyle-Width="150" />
    <asp:BoundField DataField="Preco" HeaderText="Preço" ItemStyle-Width="100" />
    <asp:ButtonField CommandName="Comprar" Text="Comprar" ButtonType="Button" />
</Columns>
</asp:GridView>

Populando o GridView:

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim dt As New DataTable()
        dt.Columns.AddRange(New DataColumn(3) {New DataColumn("Chave"), New DataColumn("Secao"), New DataColumn("Livro"), New DataColumn("Preco")})
        dt.Rows.Add(1, "A", "Foo", "5")
        dt.Rows.Add(2, "B", "Bar", "10")
        dt.Rows.Add(3, "A", "Baz", "15")
        dt.Rows.Add(4, "B", "Poo", "20")
        GridView1.DataSource = dt
        GridView1.DataBind()
    End If
End Sub

Evento RowCommand:

Protected Sub gvLivros_RowCommand(sender As Object, e As GridViewCommandEventArgs)
    Dim index As Integer = Convert.ToInt32(e.CommandArgument)

    ' Obter o valor das colunas a partir de DataKeys usando como índice a variável index.
    Dim chave As Integer = Convert.ToInt32(gvLivros.DataKeys(index).Values(0))
    Dim secao As String = gvLivros.DataKeys(index).Values(1).ToString()
End Sub

Referência:

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .