我用VS 2005 开发WinForm 程序,基本上都采用拖拽的开发模式
即从数据源中拖拽DataTable到界面中,VS就会在界面上自动产生DataGridView ,下方产生 BindingSource 和 TableAdapter然后,我在DataGridView 中增加一个与数据源无关的CheckBox列,用来选择记录,并对选中记录操作,但是不成功,以删除功能为例,我写的删除代码如下:
foreach (DataGridViewRow row in this.tblMtrlDetailDataGridView.Rows)
{
DataGridViewCheckBoxCell checkBox = (DataGridViewCheckBoxCell)row.Cells["Selected"]; if ((bool)checkBox.FormattedValue)
{
this.tblMtrlDetailBindingSource.RemoveAt(row.Index);
}
}当 tblMtrlDetailDataGridView 中记录全部打勾时,删除总是留有一条记录,再选中该记录点【删除】,才能删掉
我猜想问题可能出在 this.tblMtrlDetailBindingSource.RemoveAt(row.Index);
DataGridView 和 BindingSource 的索引对应关系
请大侠们帮忙!!!
另外,如果点击 DataGridView 的列头重新排序后,对 BindingSource 有没有影响?
即从数据源中拖拽DataTable到界面中,VS就会在界面上自动产生DataGridView ,下方产生 BindingSource 和 TableAdapter然后,我在DataGridView 中增加一个与数据源无关的CheckBox列,用来选择记录,并对选中记录操作,但是不成功,以删除功能为例,我写的删除代码如下:
foreach (DataGridViewRow row in this.tblMtrlDetailDataGridView.Rows)
{
DataGridViewCheckBoxCell checkBox = (DataGridViewCheckBoxCell)row.Cells["Selected"]; if ((bool)checkBox.FormattedValue)
{
this.tblMtrlDetailBindingSource.RemoveAt(row.Index);
}
}当 tblMtrlDetailDataGridView 中记录全部打勾时,删除总是留有一条记录,再选中该记录点【删除】,才能删掉
我猜想问题可能出在 this.tblMtrlDetailBindingSource.RemoveAt(row.Index);
DataGridView 和 BindingSource 的索引对应关系
请大侠们帮忙!!!
另外,如果点击 DataGridView 的列头重新排序后,对 BindingSource 有没有影响?
解决方案 »
- vs2005加载OFFICE
- .net疑问
- 工具栏按钮在运行时和设计时的大小一致,是怎么回事?
- 【奇怪】怀疑是编码的问题~
- 奇怪的问题,为什么在WF中,一个控件加上可托拽处理后,该控件的click事件就时有时无呢?请高手们!
- WebBrowser.Navigate加载pdf文件后,程序退出报内存错误
- 菜鸟求教:C#中如何调用IE打开*.htm文件!
- -------------求用FLASH2004和VB建立的聊天室(程序或教程)---------------------------
- 自动升级客户端
- 问几个比较简单 但涉及了一些底层运行机制 (主要是编译器修改了我的代码 但我无法理解某些行为)
- 【讨论】经典的,用于学习的开源项目有哪几个?
- 我用WebService封装了访问oracle的服务,为什么其他机器调用这个服务的时候还说没装oracle的客户端
反正listview里是这样的你改成
for(int i=this.tblMtrlDetailDataGridView.Rows.Count-1;i>-1;i--)
{
DataGridViewCheckBoxCell checkBox = (DataGridViewCheckBoxCell)row[i].Cells["Selected"];
if ((bool)checkBox.FormattedValue)
{
this.tblMtrlDetailBindingSource.RemoveAt(i);
}
}试试不知道我有没有写错……
for (int i = this.tblMtrlDetailDataGridView.Rows.Count - 1; i > -1; i--)
{
DataGridViewCheckBoxCell checkBox = (DataGridViewCheckBoxCell)this.tblMtrlDetailDataGridView.Rows[i].Cells["Selected"]; if ((bool)checkBox.FormattedValue)
{
this.tblMtrlDetailBindingSource.RemoveAt(i);
}
} 另外,点击 DataGridView 的列头重新排序后,BindingSource 中的数据也跟着排序,能按照选择的正确定位