Datagrid视图按钮重复

时间:2010-11-12 17:35:36

标签: c# datagridview button repeat

  

完全重复Datagrid View Button repeat

我已经向datagrid视图添加了按钮,但是当多次调用该函数时,新按钮添加我需要停止此添加

  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");


            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];
                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

1 个答案:

答案 0 :(得分:1)

将方法分成两部分:

1。)设置网格结构 2.)添加新行

public void SetupDataGridView()
{
            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");

            UsersView.DataSource = table;
            UsersView.AllowUserToAddRows = false;//To remove extra row at the end
            UsersView.Columns.Add(bcol);
}

public void PopulateDataGridView()
{

            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];
                table.Rows.Add(row);
            }

}

这仍然是一种次优的方法,但这是任何人都可以为有你技能的人做的。

您缺乏编程和面向对象编程的基本知识。得到一本书(例如克里斯出售'关于Windows窗体编程的书)阅读它,然后回来。你会从中受益!