Reputation: 3742
I would like to create a datagridview with 4 columns. The first column contains an edit button per row. The second contains a delete button and the next columns should contain the object data to display like ID, Firstname and so on.
For the buttons I use the DataGridViewButtonColumn
for the other ones I use the DataGridViewColumn
. When running the program I initialize the datagridview once after initializing the form.
When adding a new row I create the two buttons and try to fill these four columns.
So this is my code
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
InitializePeopleList();
}
private void InitializePeopleList()
{
DataGridViewButtonColumn editButtonColumn = new DataGridViewButtonColumn();
DataGridViewButtonColumn deleteButtonColumn = new DataGridViewButtonColumn();
DataGridViewColumn idColumn = new DataGridViewColumn();
idColumn.HeaderText = "ID";
DataGridViewColumn firstNameColumn = new DataGridViewColumn();
firstNameColumn.HeaderText = "FirstName";
dgvPeople.Columns.Add(editButtonColumn);
dgvPeople.Columns.Add(deleteButtonColumn);
dgvPeople.Columns.Add(idColumn);
dgvPeople.Columns.Add(firstNameColumn);
}
private void CreatePerson()
{
// open a new dialog, create a new Person object and add it to the data list
AddPersonRow(newPerson); // Update GUI
}
private void AddPersonRow(Person newPerson)
{
DataGridViewRow personRow = new DataGridViewRow(); // Create a new row
Button editButton = new Button(); // create a new edit button for the first column
editButton.Text = "Edit";
editButton.Click += (object sender, EventArgs e) => UpdatePersonFirstName(newPerson.ID, personRow.Cells[3]);
Button deleteButton = new Button(); // create a new delete button for the second column
editButton.Text = "Delete";
editButton.Click += (object sender, EventArgs e) => RemovePerson(newPerson.ID, personRow);
personRow.Cells[0].Value = editButton;
personRow.Cells[1].Value = deleteButton;
personRow.Cells[2].Value = newPerson.ID; // Display the ID in the third column
personRow.Cells[3].Value = newPerson.FirstName; // Display the First Name in the fourth column
dgvPeople.Rows.Add(personRow); // add this row to the datagridview
}
private void RemovePerson(Guid personId, DataGridViewRow personRow)
{
// Remove the person from the data list
dgvPeople.Rows.Remove(personRow); // Update GUI
}
private void UpdatePersonFirstName(Guid personId, DataGridViewCell firstNameCell)
{
// open a new dialog and edit the Person
// update the person in the data list
firstNameCell.Value = updatedFirstName;
}
}
When I run the program the code crashes when I try to add a new person to the datagridview at AddPersonRow
at
personRow.Cells[0].Value
I get an ArgumentOutOfRangeException
because personRow.Cells
has a Count
of 0.
How can I add a new row to the datagridview that has two button columns and two text columns?
Upvotes: 1
Views: 3017
Reputation: 14231
Initialization:
var editButtonColumn = new DataGridViewButtonColumn();
editButtonColumn.Text = "Edit";
editButtonColumn.UseColumnTextForButtonValue = true;
var deleteButtonColumn = new DataGridViewButtonColumn();
deleteButtonColumn.Text = "Delete";
deleteButtonColumn.UseColumnTextForButtonValue = true;
var idColumn = new DataGridViewTextBoxColumn();
idColumn.HeaderText = "ID";
var firstNameColumn = new DataGridViewTextBoxColumn();
firstNameColumn.HeaderText = "FirstName";
dgvPeople.Columns.Add(editButtonColumn);
dgvPeople.Columns.Add(deleteButtonColumn);
dgvPeople.Columns.Add(idColumn);
dgvPeople.Columns.Add(firstNameColumn);
dgvPeople.CellContentClick += DgvPeople_CellContentClick;
No need to create buttons. They will be created automatically because DataGridViewButtonColumn
is used.
private void AddPersonRow(Person newPerson)
{
var rowIndex = dgvPeople.Rows.Add();
var row = dgvPeople.Rows[rowIndex];
row.Cells[2].Value = newPerson.ID;
row.Cells[3].Value = newPerson.FirstName;
}
Here we react to button presses:
private void DgvPeople_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0) // exclude header
{
if (e.ColumnIndex == 0)
{
// edit action
}
else if (e.ColumnIndex == 1)
{
// delete action
//dgvPeople.Rows.RemoveAt(e.RowIndex);
}
}
}
Upvotes: 3