gpt4 book ai didi

c# - 在 DataGridView 中隐藏行,绑定(bind)在我的项目中不起作用

转载 作者:行者123 更新时间:2023-11-30 15:31:55 25 4
gpt4 key购买 nike

我在项目中遇到了一个关于 DataGridView 行隐藏的问题。我粘贴的代码在单独的测试项目中有效,但在我的项目中无效。请帮我解决这个问题。

我正在测试的代码

public partial class frmTestGirdBinding : Form
{
CustomDataCollection cdata = new CustomDataCollection();
Random rnd = new Random();
public frmTestGirdBinding()
{
InitializeComponent();
}

private void frmTestGirdBinding_Load(object sender, EventArgs e)
{
BindingSource bindingSource1 = new BindingSource();
bindingSource1.DataSource = cdata;
dataGridView1.DataSource = bindingSource1;
//bindingSource1.Filter = "Srno = 3"; not working


CurrencyManager cm = (CurrencyManager)BindingContext[bindingSource1.DataSource];
cm.SuspendBinding();
//InVisible the rows
dataGridView1.Rows[2].Visible = false;
dataGridView1.Rows[3].Visible = false;
cm.ResumeBinding();
}

private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < cdata.Count; i++)
{
cdata[i].Reading = (float)rnd.NextDouble();
}
dataGridView1.Refresh(); //without this all rows are not updating
}
}

class CustomDataCollection : BindingList<CustomData>
{
public CustomDataCollection()
{
this.Add(new CustomData() { SrNo = 1, Name = "A", Reading = 11.11F });
this.Add(new CustomData() { SrNo = 2, Name = "B", Reading = 22.11F });
this.Add(new CustomData() { SrNo = 3, Name = "C", Reading = 33.11F });
this.Add(new CustomData() { SrNo = 4, Name = "D", Reading = 44.11F });
this.Add(new CustomData() { SrNo = 5, Name = "E", Reading = 55.11F });
this.Add(new CustomData() { SrNo = 6, Name = "F", Reading = 66.11F });
this.Add(new CustomData() { SrNo = 7, Name = "G", Reading = 77.11F });
}
}
class CustomData : INotifyPropertyChanged
{
int srno;

public int SrNo
{
get { return srno; }
set { srno = value; OnPropertyChanged("SrNo"); }
}

string name;

public string Name
{
get { return name; }
set { name = value; OnPropertyChanged("Name"); }
}

float reading;

public float Reading
{
get { return reading; }
set { reading = value; OnPropertyChanged("Reading"); }
}
#region INotifyPropertyChanged Members

public event PropertyChangedEventHandler PropertyChanged;

private void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;

if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion

}

最佳答案

搜索此问题通过添加 DataBindingComplete 事件并隐藏特定行来解决问题。 DataBindingComplete 被触发 2 次,1 次在绑定(bind)时,2 次在 form_load 事件完成后。

public partial class frmTestGirdBinding : Form
{
CustomDataCollection cdata = new CustomDataCollection();
Random rnd = new Random();
public frmTestGirdBinding()
{
InitializeComponent();
this.dataGridView1.DataBindingComplete += new System.Windows.Forms.DataGridViewBindingCompleteEventHandler(this.dataGridView1_DataBindingComplete);
}

private void frmTestGirdBinding_Load(object sender, EventArgs e)
{
BindingSource bindingSource1 = new BindingSource();
bindingSource1.DataSource = cdata;
dataGridView1.DataSource = bindingSource1;

}

private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < cdata.Count; i++)
{
cdata[i].Reading = (float)rnd.NextDouble();
}
dataGridView1.Refresh(); //without this all rows are not updating
}

private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
//InVisible the rows
dataGridView1.Rows[2].Visible = false;
dataGridView1.Rows[3].Visible = false;
}
}

class CustomDataCollection : BindingList<CustomData>
{
public CustomDataCollection()
{
this.Add(new CustomData() { SrNo = 1, Name = "A", Reading = 11.11F });
this.Add(new CustomData() { SrNo = 2, Name = "B", Reading = 22.11F });
this.Add(new CustomData() { SrNo = 3, Name = "C", Reading = 33.11F });
this.Add(new CustomData() { SrNo = 4, Name = "D", Reading = 44.11F });
this.Add(new CustomData() { SrNo = 5, Name = "E", Reading = 55.11F });
this.Add(new CustomData() { SrNo = 6, Name = "F", Reading = 66.11F });
this.Add(new CustomData() { SrNo = 7, Name = "G", Reading = 77.11F });
}
}
class CustomData : INotifyPropertyChanged
{
int srno;

public int SrNo
{
get { return srno; }
set { srno = value; OnPropertyChanged("SrNo"); }
}

string name;

public string Name
{
get { return name; }
set { name = value; OnPropertyChanged("Name"); }
}

float reading;

public float Reading
{
get { return reading; }
set { reading = value; OnPropertyChanged("Reading"); }
}
#region INotifyPropertyChanged Members

public event PropertyChangedEventHandler PropertyChanged;

private void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;

if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion

}

关于c# - 在 DataGridView 中隐藏行,绑定(bind)在我的项目中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19922151/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com