将带有按钮的datagridviewrow添加到datagridview

时间:2019-02-02 15:32:32

标签: c# winforms datagridview

我想创建一个包含4列的datagridview。第一列每行包含一个编辑按钮。第二个包含删除按钮和下一列应包含对象数据到像ID,名字等等的显示。

对于按钮,我使用DataGridViewButtonColumn,对于其他按钮,我使用DataGridViewColumn。在运行程序时,我在初始化表单后初始化了datagridview。

添加新行时,我创建了两个按钮,并尝试填充这四列。

这是我的代码

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;
    }
}

当我运行程序代码崩溃当我尝试一种新的人在添加到DataGridView AddPersonRow

personRow.Cells[0].Value

我得到一个ArgumentOutOfRangeException,因为personRow.Cells的{​​{1}}为0。

我如何添加一个新行有两个按钮,列和两分文本列在DataGridView?

1 个答案:

答案 0 :(得分:1)

初始化:

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;

没有必要创建的按钮。它们会自动创建,因为使用了DataGridViewButtonColumn

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;
}

在这里,我们对按下按钮有反应:

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);
        }
    }
}