当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - How to remove a dynamically created button in gridview

问题描述:

I'm trying to find a way to remove an item that has been dynamically added when I change my drop down list selected user. The way this works is I add an edit and delete button dynamically when I select a user. I need to have the existing one be removed when I change users. Here is the code I'm using to build the two buttons. How do I remove them? Do I wipe out the columns and then add the columns again?

try

{

dgvBG.DataSource = this.bgDataTableAdapter.GetData(cbPatientID.SelectedItem.ToString());

try

{

Image editBGImg = Properties.Resources.editor;

Image delBGImg = Properties.Resources.del;

DataGridViewImageColumn btnBGEdit = new DataGridViewImageColumn();

DataGridViewImageColumn btnBGDel = new DataGridViewImageColumn();

btnBGEdit.HeaderText = "Edit";

btnBGDel.HeaderText = "Delete";

btnBGEdit.Image = editBGImg;

btnBGDel.Image = delBGImg;

btnBGEdit.Width = 35;

btnBGDel.Width = 42;

dgvBG.Columns.Add(btnBGEdit);

dgvBG.Columns.Add(btnBGDel);

网友答案:

Please try this code. It may be better to hide them than remove them.

for (int i = dgvBG.Columns.Count - 1; i >= 0; i--)
{
    var col = dgvBG.Columns[i];

    if (col.HeaderText == "Edit" || col.HeaderText == "Delete" )
    {
        dgvBG.Columns.Remove(col);
        // col.Visible = false;
    }
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: