C# winfrom DataGridView 控件 新增一列添加删除按钮

方法一:

1,可以使用 DataGridView提供了一种列的类型,叫 DataGridViewButtonColumn,可以展示出一个按钮,虽然在UI界面中是一个button 的效果,但是并不是一个按钮,只是渲染出来的一个样式。所以并不能像button 一样去使用。

 2,这里可以设置样式,看下效果把!

3,如果想要点击删除按钮可以触发事件的话,则需要设置 DataGridVIew 的事件 CellContentClick

 private void uiDataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (uiDataGridView1.Columns[e.ColumnIndex].Name == "btnDel" && e.RowIndex >= 0)
            {
                MessageBox.Show("删除");
            }
        }

 方法二

1,首先新增列类型 DataGridViewTextBoxColumn

 

2,添加代码

 private void Form1_Load(object sender, EventArgs e)
        {
            int index = this.uiDataGridView1.Rows.Add();
            this.uiDataGridView1.Rows[index].Cells[0].Value = "0";
            this.uiDataGridView1.Rows[index].Cells[1].Value = "张之琅";
            this.uiDataGridView1.Rows[index].Cells[2].Value = "男";
            this.uiDataGridView1.Rows[index].Cells[3].Value = "18";
            this.uiDataGridView1.Rows[index].Cells[4].Value = "勤奋";
            Button btn = new Button();
            btn.Text = "删除";
            btn.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(80)))), ((int)(((byte)(160)))), ((int)(((byte)(255)))));
            btn.Font = new System.Drawing.Font("微软雅黑", 8F);
            this.uiDataGridView1.Controls.Add(btn);
            Rectangle rect = this.uiDataGridView1.GetCellDisplayRectangle(5, index, false);
            btn.Location = new Point(rect.Left, rect.Top);
            btn.Click += new EventHandler(btnDel_Click);

        }
        void btnDel_Click(object sender, EventArgs e)
        {
            if (uiDataGridView1.SelectedRows.Count < 1)
                return;

            uiDataGridView1.Rows.Remove(this.uiDataGridView1.SelectedRows[0]);
        }

最后看下效果把! 

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以通过以下代码删除 TableLayoutPanel 控件的一行: ```csharp private void DeleteRow(TableLayoutPanel panel, int rowIndex) { // 移除该行的所有控件 for (int i = 0; i < panel.ColumnCount; i++) { Control control = panel.GetControlFromPosition(i, rowIndex); panel.Controls.Remove(control); } // 移除该行 for (int i = rowIndex + 1; i < panel.RowCount; i++) { for (int j = 0; j < panel.ColumnCount; j++) { Control control = panel.GetControlFromPosition(j, i); panel.SetRow(control, i - 1); // 更新控件的行数 } } panel.RowStyles.RemoveAt(panel.RowCount - 1); panel.RowCount--; } ``` 同样的,你也可以通过以下代码删除 TableLayoutPanel 控件一列: ```csharp private void DeleteColumn(TableLayoutPanel panel, int columnIndex) { // 移除该列的所有控件 for (int i = 0; i < panel.RowCount; i++) { Control control = panel.GetControlFromPosition(columnIndex, i); panel.Controls.Remove(control); } // 移除该列 for (int i = columnIndex + 1; i < panel.ColumnCount; i++) { for (int j = 0; j < panel.RowCount; j++) { Control control = panel.GetControlFromPosition(i, j); panel.SetColumn(control, i - 1); // 更新控件的列数 } } panel.ColumnStyles.RemoveAt(panel.ColumnCount - 1); panel.ColumnCount--; } ``` 使用时,你可以调用这些方法并传入 TableLayoutPanel 控件和要删除的行或列的索引。例如,删除第 2 行: ```csharp DeleteRow(tableLayoutPanel1, 1); ``` 删除第 3 列: ```csharp DeleteColumn(tableLayoutPanel1, 2); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值