对于DataSet中的问题真是郁闷啊

我将一个DataSet放到一个Session中,将Session中的一个表绑定到一个DataGrid中,在DataGrid中我加入了模板列。
DataSet表的示例:
字段1字段2字段3删除标记
ID1姓名1地址1已经删除
ID2姓名2地址2
ID3姓名3地址3
当用户通过DataGrid模板列删除第一条记录后,此时数据并未保存到数据库中,只是在第一条记录中做了一个删除标记。但在DataGrid中用户看到的第一条记录已经没有了,模板列绑定的第一条记录实际上是DataSet中的第二条记录,当用户再次执行删除DataGrid中的第一条记录时,通过参数获得的仍然是已经删除的第一条记录的rownumber,所以执行后不会在DataGrid中删除。

具体代码如下:
None.gif namespace  Human.WebUI.Persons.modules
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
using System;
InBlock.gif    
using System.Data;
InBlock.gif    
using System.Drawing;
InBlock.gif    
using System.Web;
InBlock.gif    
using System.Web.UI;
InBlock.gif    
using System.Web.UI.WebControls;
InBlock.gif    
using System.Web.UI.HtmlControls;
InBlock.gif
InBlock.gif    
using Human.Common.Data;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
///        ExperienceListModule 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class ExperienceListModule : ModuleBase
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected System.Web.UI.WebControls.DataGrid ExperienceDataGrid;
InBlock.gif
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            InitializeDataGrid();
InBlock.gif            ExperienceDataGrid.DataSource
=Person.Tables[PersonData.EXPERIENCE_ITEM_TABLE].DefaultView;
InBlock.gif            ExperienceDataGrid.DataBind();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void InitializeDataGrid()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ExperienceDataGrid.AutoGenerateColumns
=false;
InBlock.gif
InBlock.gif            TemplateColumn tcStartYear
=new TemplateColumn();
InBlock.gif            tcStartYear.HeaderTemplate
=new DataGridTempleteColumn(ListItemType.Header,"开始时间");
InBlock.gif            tcStartYear.ItemTemplate
=new DataGridTempleteColumn(ListItemType.Item,"StartYear");
InBlock.gif            ExperienceDataGrid.Columns.Add(tcStartYear);
InBlock.gif
InBlock.gif            TemplateColumn tcEndYear
=new TemplateColumn();
InBlock.gif            tcEndYear.HeaderTemplate
=new DataGridTempleteColumn(ListItemType.Header,"结束时间");
InBlock.gif            tcEndYear.ItemTemplate
=new DataGridTempleteColumn(ListItemType.Item,"EndYear");
InBlock.gif            ExperienceDataGrid.Columns.Add(tcEndYear);
InBlock.gif
InBlock.gif            BoundColumn CorporationCol
=new BoundColumn();
InBlock.gif            CorporationCol.HeaderText
="单位名称";
InBlock.gif            CorporationCol.DataField
=PersonData.EXPERIENCE_CORPORATION_FIELD;
InBlock.gif            ExperienceDataGrid.Columns.Add(CorporationCol);
InBlock.gif
InBlock.gif            BoundColumn DepartmentCol
=new BoundColumn();
InBlock.gif            DepartmentCol.HeaderText
="部门名称";
InBlock.gif            DepartmentCol.DataField
=PersonData.EXPERIENCE_DEPARTMENT_FIELD;
InBlock.gif            ExperienceDataGrid.Columns.Add(DepartmentCol);
InBlock.gif
InBlock.gif            TemplateColumn tcEdit
=new TemplateColumn();
InBlock.gif            tcEdit.HeaderTemplate
=new DataGridTempleteColumn(ListItemType.Header,"修改");
InBlock.gif            tcEdit.ItemTemplate
=new DataGridTempleteColumn(ListItemType.Item,"Edit");
InBlock.gif            
InBlock.gif            ExperienceDataGrid.Columns.Add(tcEdit);
InBlock.gif
InBlock.gif            ButtonColumn tcDelete
=new ButtonColumn();
InBlock.gif            tcDelete.Text
="删除";
InBlock.gif            tcDelete.ButtonType
=ButtonColumnType.LinkButton;
InBlock.gif            tcDelete.HeaderText
="删除";
InBlock.gif            tcDelete.CommandName
="Delete";
InBlock.gif            ExperienceDataGrid.Columns.Add(tcDelete);
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
InBlock.gif        
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif            
base.OnInit(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
///        设计器支持所需的方法 - 不要使用代码编辑器
InBlock.gif        
///        修改此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.ExperienceDataGrid.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.ExperienceDataGrid_DeleteCommand);
InBlock.gif            
this.ExperienceDataGrid.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.ExperienceDataGrid_ItemDataBound);
InBlock.gif            
this.ExperienceDataGrid.SelectedIndexChanged += new System.EventHandler(this.ExperienceDataGrid_SelectedIndexChanged);
InBlock.gif            
this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
private void ExperienceDataGrid_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (((LinkButton)e.CommandSource).CommandName == "Delete")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    DataTable ExperienceTable
=Person.Tables[PersonData.EXPERIENCE_ITEM_TABLE];
InBlock.gif                    
DataRow row=ExperienceTable.Rows[e.Item.ItemIndex];
InBlock.gif                    row.Delete();

ExpandedSubBlockEnd.gif
                }

InBlock.gif                
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
string s=ex.Message.ToString();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void ExperienceDataGrid_SelectedIndexChanged(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif        
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void ExperienceDataGrid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if((e.Item.ItemType!= ListItemType.Header) & ( e.Item.ItemType != ListItemType.Footer))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                LinkButton oDeleteButton  
= (LinkButton)e.Item.Cells[5].Controls[0];
InBlock.gif
InBlock.gif                oDeleteButton.Attributes.Add(
"onclick","Javascript:return confirm('您是否确认要删除此记录?');");
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif    
class DataGridTempleteColumn:ITemplate
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        ListItemType templateType;
InBlock.gif        
string columnName;
InBlock.gif   
InBlock.gif        
public DataGridTempleteColumn(ListItemType type, string colname)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            templateType 
= type;
InBlock.gif            columnName 
= colname;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void InstantiateIn(System.Web.UI.Control container)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Literal lc 
= new Literal();
InBlock.gif            
switch(templateType)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
case ListItemType.Header:
InBlock.gif                    lc.Text 
= columnName;
InBlock.gif                    container.Controls.Add(lc);
InBlock.gif                    
break;
InBlock.gif                
case ListItemType.Item:
InBlock.gif                    lc.DataBinding
+=new EventHandler(TemplateControl_DataBinding);
InBlock.gif                    container.Controls.Add(lc);
InBlock.gif                    
break;
InBlock.gif                
case ListItemType.EditItem:
InBlock.gif                    TextBox tb 
= new TextBox();
InBlock.gif                    tb.Text 
= "";
InBlock.gif                    container.Controls.Add(tb);
InBlock.gif                    
break;
InBlock.gif                
case ListItemType.Footer:
InBlock.gif                    lc.Text 
= "<I>" + columnName + "</I>";
InBlock.gif                    container.Controls.Add(lc);
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif    
InBlock.gif        
private void TemplateControl_DataBinding(object sender,
InBlock.gif            System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Literal lc;
InBlock.gif            lc 
= (Literal) sender;
InBlock.gif            DataGridItem container
=(DataGridItem)lc.NamingContainer;
InBlock.gif            
if(columnName=="StartYear")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                lc.Text
+=DataBinder.Eval(container.DataItem, "StartYear");
InBlock.gif                lc.Text
+="";
InBlock.gif                lc.Text
+=DataBinder.Eval(container.DataItem, "StartMonth");
InBlock.gif                lc.Text
+="";
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if(columnName=="EndYear")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(Convert.ToString(DataBinder.Eval(container.DataItem, "EndYear"))!="")
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    lc.Text
+=DataBinder.Eval(container.DataItem, "EndYear");
InBlock.gif                    lc.Text
+="";
InBlock.gif                    lc.Text
+=DataBinder.Eval(container.DataItem, "EndMonth");
InBlock.gif                    lc.Text
+="";
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    lc.Text
+="至今";
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
if(columnName=="Edit")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                lc.Text
+="<a href=# οnclick='EditExperience("+container.DataSetIndex+")'>修改</a>";
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

问题终于解决了,由于我是使用Row.Delete()函数,数据并未从数据集中删除,只是做了一个删除标记,所以在提取数据时,需要计算DataGrid中要删除行中数据在DataSet中具体是对应的那条。函数如下:

None.gif    private   int  GetFactRowItem(DataTable table, int  ItemIndex)
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif {
InBlock.gif   
int FactRowItem=0;
InBlock.gif   
int UnDeleteRowCount=-1;
InBlock.gif   
while(UnDeleteRowCount<ItemIndex)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
if(table.Rows[FactRowItem].RowState==DataRowState.Deleted)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     FactRowItem
++;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     FactRowItem
++;
InBlock.gif     UnDeleteRowCount
++;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif   }

InBlock.gif   
return FactRowItem-1;
ExpandedBlockEnd.gif  }
 

转载于:https://www.cnblogs.com/landina/archive/2005/12/24/304064.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值