将按钮插入特定的数据网格视图列

时间:2018-09-08 08:12:01

标签: c# winforms datagridview

我可以将按钮放在想要的行的任何地方吗?例如,我想将按钮放在row [2]中,这样该按钮将出现在该行的每一列中。

我试图做出类似这样的操作以使右边的按钮变成可能,但失败了:

DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
        btn.HeaderText = "Delete";
        btn.Text = "   ";
        btn.UseColumnTextForButtonValue = true;
        tbl_incomingrawmaterialform.ColumnCount = 5; 

        tbl_incomingrawmaterialform.Columns[0].HeaderText = "Header1";
        tbl_incomingrawmaterialform.Columns[1].HeaderText = "Header2";
        tbl_incomingrawmaterialform.Columns[2].HeaderText = "Header3";
        tbl_incomingrawmaterialform.Columns[3].HeaderText = "Header4";
        tbl_incomingrawmaterialform.Columns.Add(btn);

这是用于循环数据:

tbl_incomingrawmaterialform.Rows[i].Cells[0].Value = (i + 1);
            tbl_incomingrawmaterialform.Rows[i].Cells[1].Value = Value1;
            tbl_incomingrawmaterialform.Rows[i].Cells[2].Value = Value2;
            tbl_incomingrawmaterialform.Rows[i].Cells[3].Value = Value3;
            DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
            tbl_incomingrawmaterialform.Rows.Add(btn);

            i++;

但这不起作用。我以为如果我清空一列,该按钮会自动出现。对不起,我还是初学者。

1 个答案:

答案 0 :(得分:2)

您应该使用gridView.Columns.Insert()而不是gridView.Rows.Add()。请看一下:

        int columnIndex = 4;

        if (tbl_incomingrawmaterialform.Columns["Delete"] == null)
        {
            tbl_incomingrawmaterialform.Columns.Insert(columnIndex, btn);
        }

编码愉快!