0

在按钮单击时在 GridView winform 中添加新行。需要在网格中有单个按钮而不是 DataGridViewButtonColumn

4

2 回答 2

1

您可以使用添加它

GridView1.DataSource = 数据表;

然后通过这个添加新行

数据表.Rows.Add();

这将添加新行并将控件 ID 提供给 add()。

于 2012-10-20T10:42:09.597 回答
1
use this 

public partial class Form1 : Form
    {
        Button textBoxDgv1 = new Button();
        Label labelDgv1 = new Label();
the next is on the Form_Load event

private void Form1_Load(object sender, EventArgs e)
        {
            labelDgv1.Text = "Delete";
            labelDgv1.Height = 20;
            labelDgv1.AutoSize = false;
            labelDgv1.BorderStyle = BorderStyle.FixedSingle;
            labelDgv1.TextAlign = ContentAlignment.MiddleCenter;
            int Xdgv1 = this.dataGridView1.GetCellDisplayRectangle(2, -1, true).Location.X;
            labelDgv1.Width = this.dataGridView1.Columns[2].Width + Xdgv1;
            labelDgv1.Location = new Point(0, this.dataGridView1.Height - textBoxDgv1.Height);
            this.dataGridView1.Controls.Add(labelDgv1);

and one more section is in dataGridView1_CellPainting

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
       int sum = 0;
       for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
       {
           if (dataGridView1.Rows[i].Cells[3].Value != string.Empty)
           {
               sum += Convert.ToInt32(this.dataGridView1[3, i].Value);
           }
       }
           textBoxDgv1.Text = sum.ToString();
           int Xdgvx = this.dataGridView1.GetCellDisplayRectangle(2, -1, true).Location.X;
           labelDgv1.Width = this.dataGridView1.Columns[2].Width + Xdgvx;
           labelDgv1.Location = new Point(0, this.dataGridView1.Height - textBoxDgv1.Height);
           textBoxDgv1.Width = this.dataGridView1.Columns[3].Width;
           Xdgvx = this.dataGridView1.GetCellDisplayRectangle(3, -1, true).Location.X;
           textBoxDgv1.Location = new Point(Xdgvx, this.dataGridView1.Height - textBoxDgv1.Height);
}
于 2012-10-20T11:35:49.437 回答