DataGridView添加行
大家好,帮一下我 ,我想在DataGridView里添加新的一行,用代码添加,不知该怎么弄,列是我自定义的列 给断示例代码最好 比如说我有3列 colOne,colTwo,colThree,当我添加行的时候,要在所对应的单元格里分别填上1,2,3这3个数字,该怎么做呀 大家见笑,我新人来着
------解决方案--------------------winform?
1、给项目增加一个类,比如Class1,包含三个属性:ColOne, ColTwo, ColThree,并实现INotifyPropertyChanged
class Class1 : System.ComponentModel.INotifyPropertyChanged
{
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
public Class1()
{
}
public Class1(int a, int b, int c)
{
colOne = a;
colTwo = b;
colThree = c;
}
private int colOne;
public int ColOne
{
get { return colOne; }
set
{
colOne = value;
OnPropertyChanged( "ColOne ");
}
}
private int colTwo;
public int ColTwo
{
get { return colTwo; }
set
{
colTwo = value;
OnPropertyChanged( "ColTwo ");
}
}
private int colThree;
public int ColThree
{
get { return colThree; }
set
{
colThree = value;
OnPropertyChanged( "ColThree ");
}
}
private void OnPropertyChanged(string strProp)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(strProp));
}
}
}
2、在Form上拖一个DataGridView和BindingSource
DataGridView的DataSource指定为bindingSource1,也就是刚拖进来的BindingSource实例。
bindingSource1的DataSource(通过属性面板):‘添加项目数据源’-‘对象’,指定为Class1。
3、按钮点击事件:
private void button1_Click(object sender, EventArgs e)
{
bindingSource1.Add(new Class1(1, 2, 3));
}