0

I'm using a datagridview on 4.5 version of .NET with Visual Studio Express 2012. My code is currently the following:

        this.dgv_proveedores.AllowUserToAddRows = false;
        this.dgv_proveedores.AllowUserToDeleteRows = false;
        this.dgv_proveedores.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        this.dgv_proveedores.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
        this.id,
        this.razon_social,
        this.rubro,
        this.cuit,
        this.seleccionar});
        this.dgv_proveedores.Location = new System.Drawing.Point(34, 202);
        this.dgv_proveedores.Margin = new System.Windows.Forms.Padding(2);
        this.dgv_proveedores.Name = "dgv_proveedores";
        this.dgv_proveedores.ReadOnly = true;
        this.dgv_proveedores.RowTemplate.Height = 24;
        this.dgv_proveedores.Size = new System.Drawing.Size(486, 154);
        this.dgv_proveedores.TabIndex = 13;
        this.dgv_proveedores.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgv_proveedores_CellContentClick);
 private void dgv_proveedores_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0)
            {
                return;
            }
            if (e.ColumnIndex == dgv_proveedores.Columns["seleccionar"].Index)
            {
                int id = int.Parse(dgv_proveedores.Rows[e.RowIndex].Cells[0].Value.ToString());
                //TODO llamar a DAO proveedor para conseguir el proveedor con el ID

            }
        }

With the event as coded above, I only get the "Seleccionar" Cell, and with Cells[1] I get null.

I don't want to select the row before clicking the button, if possible.

timmebee
  • 89
  • 1
  • 2
  • 12
  • https://stackoverflow.com/a/45410557/8424730 this may help, find control inside cell ,check is it button, if yes then Control.Select(); – Prashant Manjule Dec 08 '19 at 17:51

1 Answers1

0

Try this, you can always add more columns, below is just button example

index.aspx

    <asp:GridView ID="GvMyTable" runat="server">
        <Columns>                   
            <asp:TemplateField HeaderText="Download">
                <ItemTemplate>
                    <asp:Button ID="BtnDownload" Text="Download" CommandArgument='<%# Eval("Id") %>' runat="server" OnClick="BtnDownload_Click"></asp:Button>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

index.aspx.cs

protected void BtnDownload_Click(object sender, EventArgs e)
{
    string Id = (sender as Button).CommandArgument;
    //Get remaining data by Id or send from CommandArgument
}
Ajay
  • 209
  • 2
  • 13