隐藏DataGridView中的行,绑定在我的项目中不起作用 [英] Hide Row in DataGridView with Binding not working in my project
本文介绍了隐藏DataGridView中的行,绑定在我的项目中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在项目中面临一个关于datagridview行隐藏的问题。我粘贴的代码在saperate测试项目中工作,但不在我的项目中。请帮帮我。
我正在测试的代码
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;不工作
CurrencyManager cm =(CurrencyManager)BindingContext [bindingSource1.DataSource];
cm.SuspendBinding();
// InVisible行
dataGridView1.Rows [ 2 ]。可见= false ;
dataGridView1.Rows [ 3 ]。可见= 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(); // 没有这一切所有行都没有更新
}
}
class CustomDataCollection:BindingList< CustomData>
{
public CustomDataCollection()
{
this .Add( new CustomData(){SrNo = 1 ,Name = A,Reading = 11 .11F});
此 .Add( new CustomData(){SrNo = 2 ,Name = B,Reading = 22 .11F});
此 .Add( new CustomData(){SrNo = 3 ,Name = C,Reading = 33 .11F});
此 .Add( new CustomData(){SrNo = 4 ,Name = D,Reading = 44 .11F});
此 .Add( new CustomData(){SrNo = 5 ,Name = E,Reading = 55 .11F});
此 .Add( new CustomData(){SrNo = 6 ,Name = F,Reading = 66 .11F});
此 .Add( new CustomData(){SrNo = 7 ,Name = G,Reading = 77 .11F});
}
}
类 CustomData:INotifyPropertyChanged
{
int srno;
public int SrNo
{
get { return srno; }
set {srno = value ; OnPropertyChanged( SrNo); }
}
string name;
public string 名称
{
get { return name; }
set {name = value ; OnPropertyChanged( 名称); }
}
float 阅读;
public float 阅读
{
get { return 阅读; }
set {reading = value ; OnPropertyChanged( 读); }
}
#region INotifyPropertyChanged会员
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,在form_load事件完成后为第2次。
public partial class frmTestGirdBinding:Form
{
CustomDataCollection cdata = new CustomDataCollection();
Random rnd = new Random();
public frmTestGirdBinding()
{
InitializeComponent();
this .dataGridView1.DataBindingComplete + = new System.Windows.Forms.DataGridViewBindingCompleteEventHandler(此跨度> .dataGridView1_DataBindingComplete);
}
private void frmTestGirdBinding_Load( object sender,EventArgs e)
{
BindingSource bindingSource1 = new BindingSource();
bindingSource1.DataSource = cdata;
dataGridView1.DataSource = bindingSource1;
}
private void button1_Click(< span class =code-keyword> object sender,EventArgs e)
{
for ( int i = 0 ; i < cdata.Count; i ++)
{
cdata [i] .Reading =( float )rnd.NextDouble();
}
dataGridView1.Refresh(); // 没有这一切所有行都没有更新
}
private void dataGridView1_DataBindingComplete( object sender,DataGridViewBindingCompleteEventArgs e)
{
// InVisible行
dataGridView1。行[ 2 ]。可见= false ;
dataGridView1.Rows [ 3 ]。可见= false ;
}
}
class CustomDataCollection:BindingList< customdata>
{
public CustomDataCollection()
{
this .Add( new CustomData(){SrNo = 1 ,Name = A,Reading = 11 .11F});
此 .Add( new CustomData(){SrNo = 2 ,Name = B,Reading = 22 .11F});
此 .Add( new CustomData(){SrNo = 3 ,Name = C,Reading = 33 .11F});
此 .Add( new CustomData(){SrNo = 4 ,Name = D,Reading = 44 .11F});
此 .Add( new CustomData(){SrNo = 5 ,Name = E,Reading = 55 .11F});
此 .Add( new CustomData(){SrNo = 6 ,Name = F,Reading = 66 .11F});
此 .Add( new CustomData(){SrNo = 7 ,Name = G,Reading = 77 .11F});
}
}
类 CustomData:INotifyPropertyChanged
{
int srno;
public int SrNo
{
get { return srno; }
set {srno = value ; OnPropertyChanged( SrNo); }
}
string name;
public string 名称
{
get { return name; }
set {name = value ; OnPropertyChanged( 名称); }
}
float 阅读;
public float 阅读
{
get { return 阅读; }
set {reading = value ; OnPropertyChanged( 读); }
}
#region INotifyPropertyChanged会员
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged( string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler!= null )
{
handler( this , new PropertyChangedEventArgs(propertyName));
}
}
#endregion
} < / customdata >
I'm facing one problem regarding datagridview row hiding in my project. The code I've pasted works in saperate test project but, not in my project. Please help me on this.
The code i'm testing
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
}
Please help me in this.
解决方案
Searching on this issue solve the problem by adding DataBindingComplete event and hide the specific row. DataBindingComplete is fired 2 times, 1 at the time of binding and 2nd after form_load event complete.
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 }</customdata>
这篇关于隐藏DataGridView中的行,绑定在我的项目中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文