问题描述
我想要在 DataGridView
的每一行的末尾有一个删除按钮,单击它我想从绑定列表中删除所需的行,绑定列表是我的网格的数据源.
I want a Delete button at the end of each row of DataGridView
and by clicking that I want to remove the desired row from the binding list which is data source of my grid.
但我似乎无法做到我在产品类中创建了一个按钮对象并使用唯一的 id 实例化它以从列表中删除该对象.但按钮未显示在行中.
But I can't seem to do it I have created a button object in product class and instantiated it with the unique id to remove that object from list. but button is not showing in the row.
表单中有TextBoxes,用户可以输入文本,当他们按下Add按钮时,一个新的product对象被提供的字段实例化,然后被添加到BindingList
.
There are TextBoxes in the form and users can enter text, and when they press Add button, a new object of product is instantiated with the provided fields and then it is added to the BindingList
.
最后这个列表绑定到 DataGridView
并且细节显示在网格中.(我已经完成了这部分).
Finally this list is bound to the DataGridView
and details are shown in the grid. (I have done this part).
最后点击保存按钮,列表被保存在数据库中.
and at last by clicking save button the list is saved in the DB.
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
到您的网格列.以下是使用按钮栏时您应该了解的一些常见任务:
To show a button on DataGridView
rows, you should add a DataGridViewButtonColumn
to columns of your grid. Here is some common tasks which you should know when using button column:
- 将按钮列添加到 DataGridView
- 在按钮上显示图像
- 设置按钮文本
- 处理按钮的点击事件
将按钮列添加到 DataGridView
要在网格的每一行显示一个按钮,您可以添加 DataGridViewButtonColumn
以编程方式或使用设计器的网格列:
To show a button on each row of your grid, you can add a DataGridViewButtonColumn
to columns of your grid programmatically or using designer:
var deleteButton=new DataGridViewButtonColumn();
deleteButton.Name="dataGridViewDeleteButton";
deleteButton.HeaderText="Delete";
deleteButton.Text="Delete";
deleteButton.UseColumnTextForButtonValue=true;
this.dataGridView1.Columns.Add(deleteButton);
在按钮上显示图像
如果您更喜欢在按钮上绘制图像,您应该在资源中拥有一个图像,然后处理 CellPainting
网格事件:
If you prefer to draw image on button, you should have an image in a resource and then handle CellPainting
event of your grid:
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;
}
}
设置按钮文字
您可以使用以下任一选项:
You can use either of these options:
您可以设置 DataGridViewButtonColumn
的 Text
属性,并将其 UseColumnTextForButtonValue
设置为 true
,这样文本将显示在该列的每个单元格上.
You can set Text
property of your DataGridViewButtonColumn
and also set its UseColumnTextForButtonValue
to true
, this way the text will display on each cells of that column.
deleteButton.Text="Delete";
deleteButton.UseColumnTextForButtonValue=true;
您也可以使用单元格的 Value
属性:
Also you can use Value
property of cell:
this.dataGridView1.Rows[1].Cells[0].Value = "Some Text";
作为另一种选择,您可以处理 CellFormatting
网格事件.当您想为按钮设置不同的文本时,这种方式可能很有用.
Also as another option, you can handle CellFormatting
event of your grid. This way may be useful when you want to set different texts for buttons.
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
事件网格.这两个事件都通过单击和按 键触发.
To hanlde clicks on button, you can handle CellClick
or CellContentClick
event of your grid. Both events fires by click and by pressing key.
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
,那么你就可以得到行后面的数据了:
You have e.RowIndex
, then you can get the data behind the row:
var data = (Product)dataGridView1.Rows[e.RowIndex].DataBoundItem;
// then you can get data.Id, data.Name, data.Price, ...
您需要将其转换为 recore 的数据类型,例如我们说 Product
.
You need to cast it to the data type of the recore, for example let's say Product
.
如果数据绑定已设置为使用 DataTable,则要转换的类型为 DataRowView
.
If the data binding has been setup to use a DataTable, the the type to cast is DataRowView
.
您也可以使用 dataGridView1.Rows[e.RowIndex].Cells[some cell index].Value
来获取特定单元格的值,但是 DataBoundItem
可以获取更多感觉.
You can also use dataGridView1.Rows[e.RowIndex].Cells[some cell index].Value
to get value of a specific cell, however DataBoundItem
makes more sense.
注意
- 正如 Ivan 在评论中提到的,当您使用
BindingList
时,您不会不需要将网格的数据源设置为 null 并在每次更改时返回绑定列表.BindingList
本身反映了对DataGridView
的更改.
- As mentioned by Ivan in comments, when you use
BindingList
you don't need to set datasource of grid to null and back to binding list with every change. TheBindingList
itself reflects changes to yourDataGridView
.
这篇关于带有按钮控件的 DataGridView - 删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!