DataGridView的用按钮控制 - 删除行 [英] DataGridView with Button Control - Remove Row
问题描述
我想在 DataGridView的
的每一行的结束,并通过单击我想从它的数据源绑定列表中删除所需的行删除按钮我网格。
但我似乎无法做到这一点我已经创造了产品档次的一个按钮对象,并使用唯一ID初始化它从列表中删除该对象。但按钮没有显示在行中。
有在表格文本框,并且用户可以输入文本,并且当它们preSS添加按钮,产品的新对象被实例化与所提供的字段,然后将其添加到的BindingList
。
最后这个名单被绑定到 DataGridView的
和细节都显示在网格中。 (我做了这部分)。
,最后点击保存按钮列表被保存在数据库中。
公共类产品{
公共字符串品牌{搞定;组; }
公众诠释productSku,则{搞定;组; }
公众诠释数量{搞定;组; } 公共产品(串品牌,诠释productSku,则,诠释数量){
this.Brand =品牌;
this.ProductPrice = productSku,则;
this.Quantity =数量;
}
}公共部分类的MainForm:表格{
.....
的BindingList<产品与GT; lProd =新的BindingList<产品及GT;();
私人无效btnAddProduct_Click(对象发件人,EventArgs的发送){
串品牌= txtProBrand.Text;
INT价格= Convert.ToInt32(txtPrice.Text);
INT数量= Convert.ToInt32(txtQuantity.Text); 产品pro =新产品(品牌,价格,数量);
lProd.Add(PRO);
dataGridView1.DataSource = NULL;
dataGridView1.DataSource = lProd;
}
.....
}
要显示在 DataGridView的
行的按钮,您应该添加<一个href=\"https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewbuttoncolumn(v=vs.110).aspx\"相对=nofollow> DataGridViewButtonColumn
您的网格列。下面是你应该使用按钮栏时,知道一些常见的任务:
- 按钮列添加到DataGridView的
- 在按钮显示图像
- 按钮的设置文本
- 拉手点击按钮的事件
按钮列添加到DataGridView的
要显示在您的网格每行一个按钮,就可以添加<一个href=\"https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewbuttoncolumn(v=vs.110).aspx\"相对=nofollow> DataGridViewButtonColumn
编程网格的列或使用设计师:
VAR deleteButton =新DataGridViewButtonColumn();
deleteButton.Name =dataGridViewDeleteButton;
deleteButton.HeaderText =删除;
deleteButton.Text =删除;
deleteButton.UseColumnTextForButtonValue = TRUE;
this.dataGridView1.Columns.Add(deleteButton);
在按钮显示图片
如果您preFER借鉴按钮图像,你应该在资源有一个图像,然后处理<一个href=\"https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellpainting(v=vs.110).aspx\"相对=nofollow> CellPainting
网格的事件:
无效dataGridView1_CellPainting(对象发件人,DataGridViewCellPaintingEventArgs E)
{
如果(e.RowIndex == dataGridView1.NewRowIndex || e.RowIndex℃,)
返回; 如果(e.ColumnIndex == dataGridView1.Columns [dataGridViewDeleteButton。指数)
{
VAR图像= Properties.Resources.DeleteImage; //一个图像
e.Paint(e.CellBounds,DataGridViewPaintParts.All);
变种X = e.CellBounds.Left +(e.CellBounds.Width - image.Width)/ 2;
变种Y = e.CellBounds.Top +(e.CellBounds.Height - image.Height)/ 2;
e.Graphics.DrawImage(图象,新的点(X,Y)); e.Handled =真实的;
}
}
设置按钮的文本
您可以使用这些选项:
您可以设置文本
你的 DataGridViewButtonColumn
,并设置其 UseColumnTextForButtonValue 物业code>到
真正
,这样的文字会显示在该列的每个细胞。
deleteButton.Text =删除;
deleteButton.UseColumnTextForButtonValue = TRUE;
您也可以使用细胞值
属性:
this.dataGridView1.Rows [1] .Cells [0] .value的=一些文本;
另外,作为另一种选择,你可以处理<一个href=\"https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellformatting(v=vs.110).aspx\"相对=nofollow> CellFormatting
网格的事件。当你想设置的按钮不同的文本通过这种方式可能是有用的。
无效dataGridView1_CellFormatting(对象发件人,DataGridViewCellFormattingEventArgs E)
{
//如果这是标题行或新行,什么都不做
如果(e.RowIndex℃的|| e.RowIndex == this.dataGridView1.NewRowIndex)
返回; //如果格式化所需的列,设置的值
如果(e.ColumnIndex = this.dataGridView1.Columns [dataGridViewDeleteButton。指数)
{
e.Value =删除;
}
}
手柄点击按钮的事件
要hanlde按钮点击,您可以处理<一个href=\"https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellclick(v=vs.110).aspx\"相对=nofollow> CellClick
或<一个href=\"https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellcontentclick(v=vs.110).aspx\"相对=nofollow> CellContentClick
网格的事件。通过点击由pressing <大骨节病>空间骨节病>键这两个事件触发。
无效dataGridView_CellClick(对象发件人,DataGridViewCellEventArgs E)
{
//如果点击是在新的行或标题行
如果(e.RowIndex == dataGridView1.NewRowIndex || e.RowIndex℃,)
返回; //检查是否点击打开特定列
如果(e.ColumnIndex == dataGridView1.Columns [dataGridViewDeleteButton。指数)
{
//这里放一些逻辑,例如从你的绑定列表中删除一行。
yourBindingList.RemoveAt(e.RowIndex);
}
}
注意
- 正如伊万在评论中提到的,当你使用
的BindingList
您不需要定格的数据源为null,回到与每一个变化绑定列表。在的BindingList
本身就体现改变你的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.
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.
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
.
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;
}
.....
}
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:
- Add Button Column to DataGridView
- Show Image on Button
- Set Text of Button
- Handle Click Event of Button
Add Button Column to DataGridView
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);
Show Image on Button
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;
}
}
Set Text of Button
You can use either of these options:
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;
Also you can use Value
property of cell:
this.dataGridView1.Rows[1].Cells[0].Value = "Some Text";
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";
}
}
Handle Click Event of Button
To hanlde clicks on button, you can handle CellClick
or CellContentClick
event of your grid. Both events fires by click and by pressing Space 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);
}
}
Note
- 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的用按钮控制 - 删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!