且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

数据网格视图按钮重复

更新时间:2023-10-19 10:49:52

我不太确定我明白你的问题,但我相信你需要封装新列的创建到它自己的方法,只调用一次 - 在构造函数实例

例如:

 无效CreateDeleteColumn()
{
    bcol​​ =新DataGridViewButtonColumn();
    bcol​​.HeaderText =行动;
    bcol​​.Text =删除;
    bcol​​.Name =deleteUserButton;
    bcol​​.UseColumnTextForButtonValue = TRUE;    UsersView.Columns.Add(bcol);
}

这应该阻止它每次填充列表视图时添加一列。

希望这有助于和对不起,如果我误解了。

托尼

I have added button to datagrid view but when ever the function is called more than once then new button adds I need to stop this addition

  void AddtoGrid()
    {
        try
        {                
            table = new DataTable();
            bcol = new DataGridViewButtonColumn();
            bcol.HeaderText = "Action ";
            bcol.Text = "Delete";
            bcol.Name = "deleteUserButton";
            bcol.UseColumnTextForButtonValue = true;                

            table.Columns.Add("Name");
            table.Columns.Add("Type");
            table.Columns.Add("Status");
            table.Columns.Add("Date Created");
            table.Columns.Add("Action");
            for (int i = 0; i < userAction.UserName.ToArray().Length; i++)
            {
                row = table.NewRow();
                asc.Add(userAction.UserName[i]);
                row["Name"] = userAction.UserName[i];
                row["Type"] = userAction.UserType[i];
                row["Status"] = userAction.UserStatus[i];
                row["Date Created"] = userAction.DateCrea[i];
                row["Action"] = bcol.Text;
                table.Rows.Add(row);
            }

            UsersView.DataSource = table;
            UsersView.AllowUserToAddRows = false;//To remove extra row at the end
            UsersView.Columns.Add(bcol);
        }
        catch (Exception ca)
        {
            MessageBox.Show(ca.ToString());
        }
    }//End Function for Getting Present Users

I'm not quite sure I understand your question, though I believe that you need to encapsulate creation of the new column into it's own method and only call it once - in the constructor for instance.

For example:

void CreateDeleteColumn()
{            
    bcol = new DataGridViewButtonColumn();
    bcol.HeaderText = "Action ";
    bcol.Text = "Delete";
    bcol.Name = "deleteUserButton";
    bcol.UseColumnTextForButtonValue = true;

    UsersView.Columns.Add(bcol);
}

That should stop it adding a column every time you populate the list view.

Hope this helps and sorry if I misunderstood.

Tony