无法在WinForms中右侧添加DataGridViewButtonColumn

时间:2015-07-31 09:30:05

标签: c# winforms datagridview

我使用DataGridView构建了一个WinForms应用程序。我想在结尾(右侧)添加EditDelete的按钮列。

我有这样的代码用于数据绑定:

public FrmMain()
{
    InitializeComponent();
    var editColumn = new DataGridViewButtonColumn  //Adding Edit column
    {
        Text = "Edit",
        UseColumnTextForButtonValue = true,
        Name = "Edit",
        DataPropertyName = "Edit"
    };
    dataGridView1.Columns.Add(editColumn);
    var delColumn = new DataGridViewButtonColumn  //Adding Delete Column
    {
        Text = "Delete",
        UseColumnTextForButtonValue = true,
        Name = "Delete",
        DataPropertyName = "Delete"
    };
    dataGridView1.Columns.Add(delColumn);
    DisplayData(); //Method to bind data in gridview
}

这将在第0列和第0列添加按钮,就像附加的图像一样。如何在状态列之后添加按钮?

enter image description here

5 个答案:

答案 0 :(得分:2)

N.B。我用C#编程,而不是VB编程,所以可能会有一些我不知道的细微差别。

您在绑定数据之前添加了按钮列,因此它们被添加到空列集合中。因此,它们成为第0列和第1列。然后绑定数据,并在两个按钮列之后添加这些新列。

您可以先尝试绑定数据,然后添加按钮列,此时它们应显示在最后。或者在您拥有列时添加列,然后在绑定数据后修改列显示顺序。

答案 1 :(得分:0)

您可以尝试使用displayIndex属性:

delColumn.displayIndex = yourIndex;

或尝试使用Insert功能。

**编辑:**

试试这样:

public FrmMain()
{
    InitializeComponent();
    var editColumn = new DataGridViewButtonColumn  //Adding Edit column
    {
        Text = "Edit",
        UseColumnTextForButtonValue = true,
        Name = "Edit",
        DataPropertyName = "Edit"
    };
    dataGridView1.Columns.Add(editColumn);
    var delColumn = new DataGridViewButtonColumn  //Adding Delete Column
    {
        Text = "Delete",
        UseColumnTextForButtonValue = true,
        Name = "Delete",
        DataPropertyName = "Delete"
    };
    dataGridView1.Columns.Add(delColumn);

    // Set after adding to collection
    editColumn.DisplayIndex = 3;
    delColumn.DisplayIndex = 4;

    DisplayData(); //Method to bind data in gridview
}

答案 2 :(得分:0)

我为你创造了这样的东西。因为我无法看到图像,但可以理解你的问题: 的修改

private void Form1_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("name");
        for (int j = 0; j < 10; j++)
        {
            dt.Rows.Add("");
        }
        for (int j = 0; j < 10; j++)
        {
            dt.Columns.Add("");
        }
        this.dataGridView1.DataSource = dt;
        this.dataGridView1.Columns[0].Width = 100;

        Button edit = new Button();
        edit.Text = "Edit";
        Button delete = new Button();
        delete.Text = "Delete";
        this.dataGridView1.Controls.Add(edit);
        //set its location and size to fit the cell
        edit.Location = this.dataGridView1.GetCellDisplayRectangle(3, 0, true).Location;
        edit.Size = this.dataGridView1.GetCellDisplayRectangle(4, 0, true).Size;

        this.dataGridView1.Controls.Add(delete);
        //set its location and size to fit the cell
        delete.Location = this.dataGridView1.GetCellDisplayRectangle(2, 3, true).Location;
        delete.Size = this.dataGridView1.GetCellDisplayRectangle(0, 1, true).Size;
    }

edit delete button image

答案 3 :(得分:0)

这是为所有ID添加的内容。:

private void Form1_Load(object sender, EventArgs e)
        {
            int j, i;
            DataTable dt = new DataTable();
            dt.Columns.Add("name");
            for ( j = 0; j < 10; j++)
            {
                dt.Rows.Add("");
            }
            for ( i = 0; i < 10; i++)
            {
                dt.Columns.Add("");
            }
            this.dataGridView1.DataSource = dt;
            this.dataGridView1.Columns[0].Width = 100;


            for (int k = 0; k < dt.Rows.Count; k++ )
            {
                Button edit = new Button();
                edit.Text = "Edit";
                Button delete = new Button();
                delete.Text = "Delete";
                this.dataGridView1.Controls.Add(edit);
                this.dataGridView1.Controls.Add(delete);                      
                //set its location and size to fit the cell
                edit.Location = this.dataGridView1.GetCellDisplayRectangle(3, k, true).Location;
                edit.Size = this.dataGridView1.GetCellDisplayRectangle(0, 1, true).Size;

                //set its location and size to fit the cell
                delete.Location = this.dataGridView1.GetCellDisplayRectangle(4, k, true).Location;
                delete.Size = this.dataGridView1.GetCellDisplayRectangle(0, 1, true).Size;
            }
        }

答案 4 :(得分:0)

我刚遇到同样的问题,偶然发现了这个问题。当其他答案奏效时,我受到启发,试图采用更简单的方法。

您可以使用DataBindingComplete方法知道绑定数据的时间,然后添加列(在末尾有效插入)。然而,道路上的一个小小的颠簸是这样的:

  

当数据源的内容发生变化时,会引发此事件   当DataSource,DataMember或BindingContext的值时   财产变化。

换句话说,事件不止一次被触发,即使只是dataGrid.DataSource = mySource;。为了解决这个问题,我添加了一个检查,以查看我要添加的列是否已存在。如果没有,我添加我的按钮列:

private void myGridView_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
    if (!myGridView.Columns.Contains("Remove"))
    {
        DataGridViewButtonColumn removeColumn = new DataGridViewButtonColumn();
        removeColumn.Name = "Remove";
        removeColumn.HeaderText = "Remove";
        removeColumn.DataPropertyName = "Remove";
        removeColumn.UseColumnTextForButtonValue = true;
        myGridView.Columns.Add(removeColumn);
    }
}

在我的情况下,我只想添加一个按钮,以便删除&#39;这将删除该行。希望您能看到这个概念,并可以调整此解决方案以满足您的需求。确保设置按钮列的Name属性,否则.Contains()将返回false。

我想注意,这也会导致列索引的混乱行为,因此当您在CellClickCellContentClick中查找按钮时,您可能需要检查列名称,而不是列索引。 See this question for more details.