[英]DataGridView with Button Control - Delete Row
我想要在DataGridView
的每一行的末尾有一個刪除按鈕,通過單擊我想從綁定列表中刪除所需的行,綁定列表是我的網格的數據源。
但我似乎無法做到我在產品類中創建了一個按鈕對象並使用唯一的 id 實例化它以從列表中刪除該對象。 但按鈕未顯示在行中。
表單中有 TextBox,用戶可以輸入文本,當他們按下 Add 按鈕時,一個新的 product 對象用提供的字段實例化,然后它被添加到BindingList
。
最后,此列表綁定到DataGridView
,詳細信息顯示在網格中。 (我已經完成了這部分)。
最后點擊保存按鈕,列表被保存在數據庫中。
public class Product{
public string Brand { get; set; }
public int ProductPrice { get; set; }
public int Quantity { get; set; }
public product(string brand,int productPrice, int quantity){
this.Brand = brand;
this.ProductPrice = productPrice;
this.Quantity = quantity;
}
}
public partial class MainForm: Form{
.....
BindingList<Product> lProd = new BindingList<Product>();
private void btnAddProduct_Click(object sender, EventArgs e){
string Brand = txtProBrand.Text;
int Price = Convert.ToInt32(txtPrice.Text);
int Quantity = Convert.ToInt32(txtQuantity.Text);
Product pro = new Product(Brand, Price, Quantity);
lProd.Add(pro);
dataGridView1.DataSource = null;
dataGridView1.DataSource = lProd;
}
.....
}
要在DataGridView
行上顯示按鈕,您應該向DataGridViewButtonColumn
的列添加DataGridViewButtonColumn
。 以下是使用按鈕列時您應該了解的一些常見任務:
將按鈕列添加到 DataGridView
要在網格的每一行上顯示一個按鈕,您可以以編程方式或使用設計器將DataGridViewButtonColumn
添加到DataGridViewButtonColumn
的列中:
var deleteButton=new DataGridViewButtonColumn();
deleteButton.Name="dataGridViewDeleteButton";
deleteButton.HeaderText="Delete";
deleteButton.Text="Delete";
deleteButton.UseColumnTextForButtonValue=true;
this.dataGridView1.Columns.Add(deleteButton);
在按鈕上顯示圖像
如果你更喜歡在按鈕上繪制圖像,你應該在資源中有一個圖像,然后處理網格的CellPainting
事件:
void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex == dataGridView1.NewRowIndex || e.RowIndex < 0)
return;
if (e.ColumnIndex == dataGridView1.Columns["dataGridViewDeleteButton"].Index)
{
var image = Properties.Resources.DeleteImage; //An image
e.Paint(e.CellBounds, DataGridViewPaintParts.All);
var x = e.CellBounds.Left + (e.CellBounds.Width - image.Width) / 2;
var y = e.CellBounds.Top + (e.CellBounds.Height - image.Height) / 2;
e.Graphics.DrawImage(image, new Point(x, y));
e.Handled = true;
}
}
設置按鈕文本
您可以使用以下任一選項:
您可以設置DataGridViewButtonColumn
Text
屬性,並將其UseColumnTextForButtonValue
設置為true
,這樣文本將顯示在該列的每個單元格上。
deleteButton.Text="Delete";
deleteButton.UseColumnTextForButtonValue=true;
您也可以使用單元格的Value
屬性:
this.dataGridView1.Rows[1].Cells[0].Value = "Some Text";
作為另一種選擇,您可以處理網格的CellFormatting
事件。 當您想為按鈕設置不同的文本時,這種方式可能很有用。
void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
//If this is header row or new row, do nothing
if (e.RowIndex < 0 || e.RowIndex == this.dataGridView1.NewRowIndex)
return;
//If formatting your desired column, set the value
if (e.ColumnIndex=this.dataGridView1.Columns["dataGridViewDeleteButton"].Index)
{
e.Value = "Delete";
}
}
處理按鈕的點擊事件
要處理單擊按鈕,您可以處理CellClick
或CellContentClick
事件。 這兩個事件都通過單擊和按Space鍵觸發。
void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
//if click is on new row or header row
if( e.RowIndex == dataGridView1.NewRowIndex || e.RowIndex < 0)
return;
//Check if click is on specific column
if( e.ColumnIndex == dataGridView1.Columns["dataGridViewDeleteButton"].Index)
{
//Put some logic here, for example to remove row from your binding list.
//yourBindingList.RemoveAt(e.RowIndex);
// Or
// var data = (Product)dataGridView1.Rows[e.RowIndex].DataBoundItem;
// do something
}
}
獲取點擊事件記錄的數據
你有e.RowIndex
,然后你可以得到行后面的數據:
var data = (Product)dataGridView1.Rows[e.RowIndex].DataBoundItem;
// then you can get data.Id, data.Name, data.Price, ...
您需要將其轉換為 recore 的數據類型,例如讓我們說Product
。
如果數據綁定已設置為使用 DataTable, DataRowView
的類型為DataRowView
。
您還可以使用dataGridView1.Rows[e.RowIndex].Cells[some cell index].Value
來獲取特定單元格的值,但是DataBoundItem
更有意義。
筆記
BindingList
時,您不需要將網格的數據源設置為 null 並在每次更改時返回綁定列表。 BindingList
本身反映了對DataGridView
更改。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.