紧急求助!!!!!!!!!c#中的ado.net重大的一个bug?????? dataGrid1.DataSource = this.dataSet1.Tables["Test1"].DefaultView; 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 DefaultView不好用,根本不能实现dataSet11.Tables["TEST1"].Rows.InsertAt(dr1,idx)之后,插入的row显示在defaultview的最后面!!而用上面这样的方式自建的dataview则插入的row显示在dataview的那个row《当前位置》!不信的话,可以自己试试看看!! private void Form1_Load(object sender, System.EventArgs e) { dataSet = new DataSet(); dsCommand.SelectCommand = new SqlCommand( "select * from maotintest", connection ); dsCommand.Fill( dataSet ); this.dataGrid1.DataSource = this.dataSet.Tables[0].DefaultView; } private void button1_Click(object sender, System.EventArgs e) { row = this.dataSet.Tables[0].NewRow(); row["id"] = 7; row["name"] = "test7"; int count = this.dataSet.Tables[0].Rows.Count; this.dataSet.Tables[0].Rows.InsertAt( row, count+1 ); DataViewManager dvm2 = new DataViewManager( dataSet ); DataView dv2 = dvm2.CreateDataView(dataSet.Tables[0]); dataGrid1.DataSource = dv2; } 你到底要得是什么效果?~~没弄懂~~ 我得方法可以插在最下边,要当前位置直接 this.dataSet.Tables[0].Rows.add this.dataSet.Tables[0].Rows.add不好意思,这只能插在最后,呵呵~~ private void Form1_Load(object sender, System.EventArgs e) { dataSet = new DataSet(); dsCommand.SelectCommand = new SqlCommand( "select * from maotintest", connection ); dsCommand.Fill( dataSet ); this.dataGrid1.DataSource = this.dataSet.Tables[0].DefaultView; } private void button1_Click(object sender, System.EventArgs e) { row = this.dataSet.Tables[0].NewRow(); row["id"] = 7; row["name"] = "test7"; int count = this.dataGrid1.CurrentRowIndex; this.dataSet.Tables[0].Rows.InsertAt( row, count ); DataViewManager dvm2 = new DataViewManager( dataSet ); DataView dv2 = dvm2.CreateDataView(dataSet.Tables[0]); dataGrid1.DataSource = dv2; } }这样就可以了,你想插在那都可以,呵呵~~ 但是这样的话,当在dataGrid1的那个insertat进去的row上面进行手动修改数据之后,当焦点离开那个insertat进去的row就会出现异常??? maotin(liu) 的方法可以啊,什么异常说说 呵呵,好像是有异常:An unhandled exception of type 'System.NullReferenceException' occurred in system.windows.forms.dllAdditional information: Object reference not set to an instance of an object. 是不是Dataset需要AcceptChanges一下?关注 private void button1_Click(object sender, System.EventArgs e) { try { row = this.dataSet.Tables[0].NewRow(); row["id"] = 7; row["name"] = "test7"; int count = this.dataGrid1.CurrentRowIndex; this.dataSet.Tables[0].Rows.InsertAt( row, count ); dataSet.AcceptChanges(); DataViewManager dvm2 = new DataViewManager( dataSet ); // DataView dv2 = dvm2.CreateDataView(dataSet.Tables[0]); dataGrid1.DataSource = dvm2.CreateDataView( dataSet.Tables[0] ); dataGrid1.Refresh(); } catch( Exception ex ) { throw ex; } } To tanjch(大成):呵呵,我综合了大家的意见,现在应该没问提了,我试过了~~ 还是有问题,不信,你编辑其他的row,然后离开那个编辑的row,看看会莫名其妙的多次一个编辑的row来,datagrid的bug ,bug ,bug!!!!! 还是有问题,不信,你编辑其他的row,然后离开那个编辑的row,用上下移动到别的row上,看看会莫名其妙的多出一个编辑的row来,而且还会出现异常!! private void Form1_Load(object sender, System.EventArgs e) { this.dataGrid1.Refresh(); dataSet = new DataSet(); dsCommand.SelectCommand = new SqlCommand( "select * from maotintest", connection ); dsCommand.Fill( dataSet ); this.dataGrid1.DataSource = this.dataSet.Tables[0].DefaultView; } private void button1_Click(object sender, System.EventArgs e) { try { row = this.dataSet.Tables[0].NewRow(); row["id"] = 7; row["name"] = "test7"; int count = this.dataGrid1.CurrentRowIndex; this.dataSet.Tables[0].Rows.InsertAt( row, count+1); dataSet.AcceptChanges(); DataViewManager dvm2 = new DataViewManager( dataSet ); DataView dv2 = dvm2.CreateDataView(dataSet.Tables[0]); dataGrid1.DataSource = dv2; } catch( Exception ex ) { throw ex; } finally { this.dataGrid1.Refresh(); } } private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e) { dataSet.AcceptChanges(); this.dataGrid1.Refresh(); } 这回肯定没问题了,加一个dataGrid1_CurrentCellChanged事件。 你这样dataSet.AcceptChanges(),还能把那个dataset保存到数据库里面吗,不能吧!! 我已经解决的差不多了,虽然还不确定问题的根本,但好像是DataView和DataTable的不密切造成的 我发现DataTable中的DefaultView即使使用InsertAt方法 新的DataRow还是在所有Row的后面,和Add的一样 但不会出错 所以我在DataTable的RowChange事件里面 重新创建视图,竟然没有出错 你试试 然后再联络我至于问题的根本嘛 还要在研究研究 呵呵 我也觉得.net里的数据绑定有一些“小”bug当然它的功能还是很强大的 http://expert.csdn.net/Expert/topic/1371/1371696.xml?temp=.212063 关于EntityFramework4.5实现动态表的问题 c# 串口程序在其它的电脑上接收不到数据 事件异步调用的问题 小弟求段生成xml的代码 wince下的form怎么让它在wince中全屏(C#.2005开发的),有经验的进来指点下 用C#写的c/s架构 C# 和 VB.net 相比性能速度差多大阿? c#,控件treeview,请求帮忙!!!up有分 关于图像处理的问题 问个C# 的最基础问题,进者就有分,在线等 各位高手进来看看 簡單問題 。
dataSet11.Tables["TEST1"].Rows.InsertAt(dr1,idx)之后,插入的row显示在
defaultview的最后面!!
而用上面这样的方式自建的dataview则插入的row显示在dataview的那个row《当前位置》!
不信的话,可以自己试试看看!!
{
dataSet = new DataSet();
dsCommand.SelectCommand = new SqlCommand( "select * from maotintest", connection );
dsCommand.Fill( dataSet ); this.dataGrid1.DataSource = this.dataSet.Tables[0].DefaultView;
} private void button1_Click(object sender, System.EventArgs e)
{
row = this.dataSet.Tables[0].NewRow();
row["id"] = 7;
row["name"] = "test7";
int count = this.dataSet.Tables[0].Rows.Count;
this.dataSet.Tables[0].Rows.InsertAt( row, count+1 );
DataViewManager dvm2 = new DataViewManager( dataSet );
DataView dv2 = dvm2.CreateDataView(dataSet.Tables[0]);
dataGrid1.DataSource = dv2;
}
没弄懂~~ 我得方法可以插在最下边,
要当前位置直接 this.dataSet.Tables[0].Rows.add
不好意思,这只能插在最后,呵呵~~
{
dataSet = new DataSet();
dsCommand.SelectCommand = new SqlCommand( "select * from maotintest", connection );
dsCommand.Fill( dataSet ); this.dataGrid1.DataSource = this.dataSet.Tables[0].DefaultView;
} private void button1_Click(object sender, System.EventArgs e)
{
row = this.dataSet.Tables[0].NewRow();
row["id"] = 7;
row["name"] = "test7";
int count = this.dataGrid1.CurrentRowIndex;
this.dataSet.Tables[0].Rows.InsertAt( row, count );
DataViewManager dvm2 = new DataViewManager( dataSet );
DataView dv2 = dvm2.CreateDataView(dataSet.Tables[0]);
dataGrid1.DataSource = dv2; }
}这样就可以了,你想插在那都可以,呵呵~~
当焦点离开那个insertat进去的row就会出现异常???
An unhandled exception of type 'System.NullReferenceException' occurred in system.windows.forms.dllAdditional information: Object reference not set to an instance of an object.
{
try
{
row = this.dataSet.Tables[0].NewRow();
row["id"] = 7;
row["name"] = "test7";
int count = this.dataGrid1.CurrentRowIndex;
this.dataSet.Tables[0].Rows.InsertAt( row, count );
dataSet.AcceptChanges();
DataViewManager dvm2 = new DataViewManager( dataSet );
// DataView dv2 = dvm2.CreateDataView(dataSet.Tables[0]);
dataGrid1.DataSource = dvm2.CreateDataView( dataSet.Tables[0] );
dataGrid1.Refresh();
}
catch( Exception ex )
{
throw ex;
}
}
呵呵,我综合了大家的意见,现在应该没问提了,我试过了~~
编辑的row来,datagrid的bug ,bug ,bug!!!!!
{
this.dataGrid1.Refresh();
dataSet = new DataSet();
dsCommand.SelectCommand = new SqlCommand( "select * from maotintest", connection );
dsCommand.Fill( dataSet ); this.dataGrid1.DataSource = this.dataSet.Tables[0].DefaultView;
} private void button1_Click(object sender, System.EventArgs e)
{
try
{
row = this.dataSet.Tables[0].NewRow();
row["id"] = 7;
row["name"] = "test7";
int count = this.dataGrid1.CurrentRowIndex;
this.dataSet.Tables[0].Rows.InsertAt( row, count+1);
dataSet.AcceptChanges();
DataViewManager dvm2 = new DataViewManager( dataSet );
DataView dv2 = dvm2.CreateDataView(dataSet.Tables[0]);
dataGrid1.DataSource = dv2;
}
catch( Exception ex )
{
throw ex;
}
finally
{
this.dataGrid1.Refresh(); }
} private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)
{
dataSet.AcceptChanges();
this.dataGrid1.Refresh();
}
所以我在DataTable的RowChange事件里面 重新创建视图,竟然没有出错
你试试 然后再联络我至于问题的根本嘛 还要在研究研究 呵呵
当然它的功能还是很强大的