[英]add a datagridviewrow with buttons to the datagridview
我想創建一個4列的datagridview。 第一列每行包含一個編輯按鈕。 第二個包含刪除按鈕,接下來的列應包含要顯示的對象數據,例如ID,Firstname等。
對於按鈕,我使用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;
}
}
當我運行程序時,嘗試在AddPersonRow
處的datagridview中添加新人員時代碼崩潰
personRow.Cells[0].Value
我收到ArgumentOutOfRangeException
因為personRow.Cells
的Count
為0。
如何在具有兩個按鈕列和兩個文本列的datagridview中添加新行?
初始化:
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);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.