0

I have a DataGridView bound to a DataTable. I added a DataGridViewButtonColumn that serves as the delete button for the row.

Since it is bound to a DataTable, I'd like to be able to get the object of the row with the delete button clicked so I can delete it from the DataTable and then refresh the DataGrid.

This is how my button is setup:

  var colDelete = new DataGridViewButtonColumn();
  colDelete.Name = "DeleteButton";
  colDelete.HeaderText = "Delete";
  colDelete.Text = "Delete";
  colDelete.UseColumnTextForButtonValue = true;
  colDelete.DataPropertyName = "EthnicityDetailID";
  colDelete.DisplayIndex = 10;
  dataGridEthnicityData.Columns.Add(colDelete);

This is how I'm planning to handle the button click event:

private void dataGridEthnicityData_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        try
        {
            var senderGrid = (DataGridView)sender;

            if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn &&
                e.ColumnIndex == senderGrid.Columns["DeleteButton"].Index)
            {
                

                //ethnicityDataTable.Rows.RemoveAt(e.RowIndex);
                dataGridEthnicityData.Refresh();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error deleting ethnicity data: " + ex.Message + " " + ex.StackTrace);
        }

    }

How do I assign EthnicityDetailID as the value for the button and how do I retrieve the row with the button clicked as an object so I can retrieve the value and delete accordingly in the datatable?

Thank you.

Kate B
  • 65
  • 8
  • 1
    [DataGridView with Button Control - Delete Row](https://stackoverflow.com/a/33549704/3110834) – Reza Aghaei Jan 07 '21 at 06:20
  • 1
    Get the bound data using [DataBoundItem](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.datagridviewrow.databounditem?view=net-5.0&WT.mc_id=DT-MVP-5003235): `var data = (DataRowView)senderGrid.Rows[e.RowIndex].DataBoundItem;` – Reza Aghaei Jan 07 '21 at 06:23
  • @RezaAghaei Exactly what I'm looking for. Thanks a lot for your help! – Kate B Jan 07 '21 at 11:06

1 Answers1

1

Apparently you have bound your DataTable to the DataSource of your DataGridView. If you have separated your Data from how the data is displayed, you will have code that is similar to:

BindingList<Ethnicity> DisplayedEthnicities
{
    get => (BindingList<Ethnicity>) this.dataGridView1.DataSource,
    set => this.dataGridView1.DataSource = value;
}

Ethnicity GetEthnicity(DataGridViewRow row)
{
    return (Ethnicity)row.DataboundItem;
}

Ethnicity CurrentEthnicity => this.dataGridView1.CurrentCell?.OwnindRow as Ethnicity;

IEnumerable<Ethnicity> SelectedEthnicities => this.dataGridView1.SelectedRows
    .Select(row => this.GetEthnicity(row));

You also need conversions between Datatable and Ethnicity:

void IEnumerable<Ethnicities> ToEthnicities(DataTable dataTable)
{
      // TODO: convert the rows of the datatable into Ethnicities
}

You know better than I do how the ethnicities are in your datatable.

After this, filling the DataGridView will be easy:

DataTable CreateDataTable()
{
    // TODO: fill the datatable with data from database? CSV file? Json?
}

void FillDataGridView()
{
    var dataTable = this.CreateDataTable();
    var ethnicities = this.ToEthnicities(dataTable);
    this.DisplayedEthnicities = new BindingList<Enthnicity>(ethnicities.ToList());
}

I'd like to be able to get the object of the row with the delete button clicked

After you've done it like this, getting the databound item will be easy:

void OnCellContent_Clicked(object sender, DataGridViewCellEventArgs e)
{
    DataGridViewRow row = this.DataGridView1.Rows[e.RowIndex];
    Ethnicity ethnicity = this.GetEthnicity(row);
    ProcessEthnicity(ethnicity);
}
Harald Coppoolse
  • 28,834
  • 7
  • 67
  • 116