我有一个DataGridView。我正在使用数据源绑定DataGridBiew,如下所示......
private void form_load(object sender, eventargs e)
{
var members = xyz.members(..
select new {
blah....
blahh...
});
memberbindingsource.datasource = members
membergridview1.datasource = memberbindingsource;
// at here i have added button column to membergridview 1
// like this....
DataGridViewButtonColumn column = new DataGridViewButtonColumn();
membergridview1.Columns.Add(column);
column.FlatStyle = FlatStyle.Standard;
column.DefaultCellStyle.BackColor = Color.Green;
column.Text = "ADD";
column.HeaderText = "Add";
column.UseColumnTextForButtonValue = true;
column.Name = "btnadd";
这在表单加载时工作正常但在某处我检查组合框中的条件,如此
if(combobox1.text == "abc")
{
then i have to loaded the grid view again
like this ...
var members = xyz.members(..
select new {
blah....
blahh...
});
memberbindingsource.datasource = members
membergridview1.datasource = memberbindingsource;
at here i have added button column to membergridview 1
like this....
DataGridViewButtonColumn column = new DataGridViewButtonColumn();
membergridview1.Columns.Add(column);
column.FlatStyle = FlatStyle.Standard;
column.DefaultCellStyle.BackColor = Color.Green;
column.Text = "ADD";
column.HeaderText = "Add";
column.UseColumnTextForButtonValue = true;
column.Name = "btnadd";
}
当第一次选择combobox1文本“abc”时,datagridview显示的按钮列很好......
当我检查第一个条件(如此combobox1.text == "abc"
)时,DataGridView显示详细信息,但如果再次检查相同条件,它将再次添加按钮列。
当我一次又一次地检查相同的条件时,我想只显示一个带有成员数据的按钮列。
如何防止这种情况 - 不要一次又一次地增加按钮列?
答案 0 :(得分:0)
这是因为每次加载页面时都会添加此列。因此,无论是新电话还是回拨电话,都会添加此列。 要正确完成此工作,请附上在以下条件
中添加列的代码段if (!IsPostBack)
{
//add your column add code snippet here
}
所以你的代码看起来像这样
if (!IsPostBack)
{
DataGridViewButtonColumn column = new DataGridViewButtonColumn();
membergridview1.Columns.Add(column);
column.FlatStyle = FlatStyle.Standard;
column.DefaultCellStyle.BackColor = Color.Green;
column.Text = "ADD";
column.HeaderText = "Add";
column.UseColumnTextForButtonValue = true;
column.Name = "btnadd";
}