数据网格视图按钮重复 [英] Datagrid View Button repeat
本文介绍了数据网格视图按钮重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我添加按钮,DataGrid的看法,但曾经当函数被调用一次,然后新的按钮将我需要停止这种增加更多
无效AddtoGrid()
{
尝试
{
表=新的DataTable();
bcol =新DataGridViewButtonColumn();
bcol.HeaderText =行动;
bcol.Text =删除;
bcol.Name =deleteUserButton;
bcol.UseColumnTextForButtonValue = TRUE; table.Columns.Add(姓名);
table.Columns.Add(类型);
table.Columns.Add(状态);
table.Columns.Add(创建日期);
table.Columns.Add(行动);
的for(int i = 0; I< userAction.UserName.ToArray()长;我++)
{
行= table.NewRow();
asc.Add(userAction.UserName [I]);
行[名称] = userAction.UserName [I]
行[型] = userAction.UserType [I]
行[状态] = userAction.UserStatus [I]
行[创建日期] = userAction.DateCrea [I]
行[行动] = bcol.Text;
table.Rows.Add(行);
} UsersView.DataSource =表;
UsersView.AllowUserToAddRows = FALSE; //要在末尾删除多余的行
UsersView.Columns.Add(bcol);
}
赶上(例外CA)
{
MessageBox.Show(ca.ToString());
}
} //结束函数获取present用户
解决方案
我不太确定我明白你的问题,但我相信你需要封装新列的创建到它自己的方法,只调用一次 - 在构造函数实例
例如:
无效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
这篇关于数据网格视图按钮重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文