Get the ID of a line from a Gridview query

Asked

Viewed 581 times

2

I made a query where it is populated a gridview, the columns are code, category, noprazo, foradoprazo. In the columns noprazo and foradoprazo, I left them as linkbuttom that by clicking on the resulting item line or noprazo or out of date, returns me the ID of the clicked line.

This is performed on GridView1_RowCommand as follows below, but does not return me the id of the clicked row.

What should I do?

   protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Detalhar")
        {

            chamadosEntities carga1 = new chamadosEntities();
            int index = Convert.ToInt32(e.CommandArgument);
            GridView1.SelectedIndex = index;
            int codigoId = Convert.ToInt32(GridView1.DataKeys[index].Value);

2 answers

1

See an example below.

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Detalhar")
        {

            chamadosEntities carga1 = new chamadosEntities();
            ButtonCommand Btn = new ButtonCommand(sender);
            int index = Btn.ArgumentAsInt;


            GridView1.SelectedIndex = index;
            int codigoId = Convert.ToInt32(GridView1.DataKeys[index].Value);





   public class ButtonCommand
    {
        private IButtonControl m_Btn = null;

        public ButtonCommand(object sender)
        {
            m_Btn = sender as IButtonControl;
            if (m_Btn == null)
                throw new Exception("sender não é IButtonControl");
        }


        public Int32 ArgumentAsInt
        {
            get
            {
                Int32 Result = 0;
                Int32.TryParse(m_Btn.CommandArgument, out Result);
                return Result;
            }
        }
    }
  • acredito que a linha : int codigoId = Convert.ToInt32(GridView1.DataKeys[index].Value); posssa ser substituida por <asp:GridView ID="Grid" runat="server" DataKeyNames="codigoId "> e no codo behind Int32 codigoId = (Int32)Grid.DataKeys[row.RowIndex]. Value;

  • Vlw Marconcilio, it worked... Thank you very much

0

See if this example fits:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
  {
    try
    {
      int id = Convert.ToInt32(e.CommandArgument);
      if (e.CommandName.Equals("Detalhar"))
      {
        carga1.Id = Convert.ToInt32(this.GridView1.DataKeys[id]["ID"]);

        this.GridView1.UseAccessibleHeader = true;
        this.GridView1.HeaderRow.TableSection = TableRowSection.TableHeader;

        Session.Add("SESSAO", carga1);
      }
    }
    catch (Exception)
    {
      Show("Não foi possível realizar a operação.");
    }
  }

Browser other questions tagged

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