创建DataGridViewButtonCell以删除DataGridView的项目?

时间:2016-09-17 05:34:03

标签: c# datagridview

我尝试在DataGridView中添加按钮以从中删除项目。要做到这一点,我尝试使用DataGridViewButtonCell,因为我看到它可以包含图像。我希望我的按钮留在DataGridView的第一列,但我有一个IList我用DataSource而我不知道如何添加在这种情况下,GridView的那个按钮。

我怎么能这样做?

这是我的代码:

private void setDataSource(){
   grid.DataSource = ivDAO.findAllItemVenda(venda);
   grid.ClearSelection();
   defineGrid();
}  

private void defineGrid() {
            //header 
            grid.Columns["produto"].HeaderText = "Produto";
            grid.Columns["valorUn"].HeaderText = "Unit.R$";
            grid.Columns["quantidade"].HeaderText = "Qtd.";
            grid.Columns["total"].HeaderText = "Total R$";

            //hide
            grid.Columns["id"].Visible = false;
            grid.Columns["venda"].Visible = false;

            //width
            grid.Columns["produto"].Width = 235;
            grid.Columns["valorUn"].Width = 80;
            grid.Columns["quantidade"].Width = 50;
            grid.Columns["total"].Width = 80;

            //align
            grid.Columns["valorUn"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            grid.Columns["quantidade"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            grid.Columns["total"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;

            //add button
            DataGridViewButtonCell btnDelete = new DataGridViewButtonCell();
            btnDelete.Value = "delete";
            grid.Rows.Add(btnDelete);
        }

我想要这个。

enter image description here

1 个答案:

答案 0 :(得分:0)

完成。最后我做了我想做的事。

我做了。

private void setDataSource(){
   //added column with image before set datasource
   if (grid.Columns["Del"] == null) {
                    var col = new DataGridViewImageColumn();
                    col.HeaderText = "";
                    col.Name = "Del";
                    col.Image = TerminalControleDeVendas.Properties.Resources.trash;
                    grid.Columns.Add(col);
                }
   grid.DataSource = ivDAO.findAllItemVenda(venda);
   grid.ClearSelection();
   defineGrid();
}  


private void defineGrid() {
            //header 
            grid.Columns["produto"].HeaderText = "Produto";
            grid.Columns["valorUn"].HeaderText = "Unit.R$";
            grid.Columns["quantidade"].HeaderText = "Qtd.";
            grid.Columns["total"].HeaderText = "Total R$";

            //hide
            grid.Columns["id"].Visible = false;
            grid.Columns["venda"].Visible = false;

            //width
            grid.Columns["Del"].Width = 30;
            grid.Columns["produto"].Width = 220;
            grid.Columns["valorUn"].Width = 80;
            grid.Columns["quantidade"].Width = 50;
            grid.Columns["total"].Width = 80;

            //align
            grid.Columns["valorUn"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            grid.Columns["quantidade"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            grid.Columns["total"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;

        }

最后我添加了删除项目

的处理程序
private void grid_CellContentClick(object sender, DataGridViewCellEventArgs e) {
            if (this.grid.Columns[e.ColumnIndex].Name == "Del") {
                DialogResult confirma = MessageBox.Show("Deseja realmente excluir o item ?", "Excluir", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (confirma == DialogResult.Yes) {
                    ItemVenda iv = (ItemVenda)grid.CurrentRow.DataBoundItem;
                    ivDAO.delete(iv);                        
                }
            }            
        }

结果是

enter image description here