GridView - criando colunas CommandField

<< Click to Display Table of Contents >>

Navigation:  ASP.NET > ASP Controls > GridView >

GridView - criando colunas CommandField

Previous pageReturn to chapter overviewNext page

Tela

 

columnfield

 

Pessoa.cs

 

using System;

using System.Collections.Generic;

 

namespace WebApplication4

{

  public class Pessoa

   {

      public int Id { get; set; }

      public string Nome { get; set; }

      public decimal Valor { get; set; }

      public DateTime Data { get; set; }

 

      public static List<Pessoa> Get()

       {

          List<Pessoa> lista = new List<Pessoa>();

           lista.Add(new Pessoa() { Id = 1, Nome = "Junior", Valor = (decimal)10.02, Data = Convert.ToDateTime("01/01/2010") });

           lista.Add(new Pessoa() { Id = 2, Nome = "Flavio", Valor = (decimal)98.10, Data = Convert.ToDateTime("01/01/2009") });

           lista.Add(new Pessoa() { Id = 3, Nome = "Mario", Valor = (decimal)43.45, Data = Convert.ToDateTime("01/01/1990") });

           lista.Add(new Pessoa() { Id = 4, Nome = "Luidi", Valor = (decimal)99.25, Data = Convert.ToDateTime("01/01/1992") });

           lista.Add(new Pessoa() { Id = 5, Nome = "Peach", Valor = (decimal)76.44, Data = Convert.ToDateTime("01/01/1977") });

           lista.Add(new Pessoa() { Id = 6, Nome = "Toad", Valor = (decimal)98.30, Data = Convert.ToDateTime("01/01/1980") });

           lista.Add(new Pessoa() { Id = 7, Nome = "Rosalina", Valor = (decimal)25.12, Data = Convert.ToDateTime("01/01/2003") });

           lista.Add(new Pessoa() { Id = 8, Nome = "Inara", Valor = (decimal)78.99, Data = Convert.ToDateTime("01/01/2007") });

          return lista;

       }

   }

}

 

Default.aspx

 

      <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Id"

          OnSelectedIndexChanged="GridView1_SelectedIndexChanged">

          <Columns>

              <asp:BoundField DataField="Id" HeaderText="Código" />

              <asp:BoundField DataField="Nome" HeaderText="Nome" />

              <asp:BoundField DataField="Valor" DataFormatString="{0:0.00}" HeaderText="Valor" />

              <asp:BoundField DataField="Data" DataFormatString="{0:dd/MMM/yy}" HeaderText="Data" />

              <asp:CommandField HeaderText="Comando" SelectText="Aqui" ShowSelectButton="True">

                  <ItemStyle HorizontalAlign="Center" />

              </asp:CommandField>

          </Columns>

      </asp:GridView>

 

Default.aspx.cs

 

using System;

 

namespace WebApplication4

{

  public partial class WebForm1 : System.Web.UI.Page

   {

      protected void Page_Load(object sender, EventArgs e)

       {

          if (!IsPostBack)

           {

               CarregarDados();

           }

       }

 

      void CarregarDados()

       {

           GridView1.DataSource = Pessoa.Get();

           GridView1.DataBind();

       }

 

      protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)

       {

          // selectedValue = DataKeyNames no aspx

          int id = (int)GridView1.SelectedValue;

           Response.Write(id.ToString());

       }

   }

}

 


 

Usando mesmo projeto anterior, vamos criar um CommandField Delete

 

Default.aspx

 

      <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"

          onrowdeleting="GridView1_RowDeleting">

          <Columns>

              <asp:BoundField DataField="Id" HeaderText="Código" />

              <asp:BoundField DataField="Nome" HeaderText="Nome" />

              <asp:BoundField DataField="Valor" DataFormatString="{0:0.00}" HeaderText="Valor" />

              <asp:BoundField DataField="Data" DataFormatString="{0:dd/MMM/yy}" HeaderText="Data" />

              <asp:CommandField HeaderText="Selecionar" SelectText="[Select]"

                  ShowSelectButton="True">

                  <ItemStyle HorizontalAlign="Center" />

              </asp:CommandField>

              <asp:CommandField DeleteText="[Excluir]" ShowDeleteButton="True" />

          </Columns>

      </asp:GridView>

 

 

Default.aspx.cs

 

      protected void GridView1_RowDeleting(object sender, System.Web.UI.WebControls.GridViewDeleteEventArgs e)

       {

          int id = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());

           Response.Write("Excluir Comando: " + id.ToString());

       }