【问题标题】:Datagrid View Button repeatDatagridview 按钮重复
【发布时间】:2010-11-12 17:01:52
【问题描述】:

我已经在数据网格视图中添加了按钮,但是当函数被多次调用时,新按钮添加我需要停止这个添加

  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

【问题讨论】:

    标签: c# datagridview button


    【解决方案1】:

    我不太确定我是否理解您的问题,但我相信您需要将新列的创建封装到它自己的方法中并且只调用一次 - 例如在构造函数中。

    例如:

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

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

    希望这对我有所帮助,如果我误解了,我们深表歉意。

    托尼

    【讨论】:

      猜你喜欢
      • 2015-04-06
      • 1970-01-01
      • 1970-01-01
      • 2017-01-17
      • 2021-07-26
      • 2019-07-15
      • 2019-08-08
      • 1970-01-01
      相关资源
      最近更新 更多