请教一个datagrid删错行的问题

mlxsx 2007-07-18 06:49:52
请教一个问题,datagrid控件,为什么我先点了第一行(数据行)的编辑之后,再点击第二行的删除,实际操作之后删除确是第一行处于编辑状态的数据了,我的编辑,删除起一个修改字段的作用,不是立即对数据库进行操作的,而且EditItemIndex的值是"0",删除按钮传回的e.Item.ItemIndex值为"1"!
 如果我先选一个项删除,后选一个项编辑,两条记录的对应操作均可以执行!
 但是删除作用确是对第一行进行的操作!
 
 前辈,如果您熟悉这个原理,可以达到这个目的的,劳驾您帮我看看我的问题出在哪里,谢谢了!

    //编辑datagrid选项,可选行下的可选列
private void Basedatagrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
Basedatagrid1.EditItemIndex = e.Item.ItemIndex;
e.Item.Cells[9].Text="U";
this.HidMessageIdOne.Value =this.HidMessageIdOne.Value+e.Item.ItemIndex.ToString()+",";
this.dt=BuildTBlCommand(Basedatagrid1,"Text");
Basedatagrid1.DataSource=this.dt ;
Basedatagrid1.DataBind();
}
//删除datagrid指定选项
private void Basedatagrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
e.Item.Cells[9].Text="D";
//Basedatagrid1.EditItemIndex=e.Item.ItemIndex;
this.HidMessageIdList.Value =this.HidMessageIdList.Value+e.Item.ItemIndex.ToString()+",";
this.dt=BuildTBlCommand(Basedatagrid1,"Text");
Basedatagrid1.DataSource=this.dt ;
Basedatagrid1.DataBind();
}
...全文
129 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
mlxsx 2007-07-19
  • 打赏
  • 举报
回复
to liufuyahong
 我无法直接从datagird.datasource中读取dt(未修改和已经修改过的),解决绑定为空的现象的函数代码!
//根据datagrid的datatable选项生成dt
private DataTable BuildTBlCommand(BaseDataGrid bd,string readtype)
{
this.dt=new DataTable();
dt.Columns.Add("CategoryID");
dt.Columns.Add("CategoryType");
dt.Columns.Add("CategoryName");
dt.Columns.Add("MaxNumForAll");
dt.Columns.Add("MaxNumForPerContact");
dt.Columns.Add("MaxNumForPubContact");
dt.Columns.Add("LastUpdate");
dt.Columns.Add("OprationType");

StoreIndexForDel(this.HidMessageIdList.Value);
StoreIndexForUpd(this.HidMessageIdOne.Value);

for(int i=0;i<bd.Items.Count;i++)
{
DataRow row1=this.dt.NewRow();
if (bd.EditItemIndex==i && readtype=="TextBox")
//if (readtype=="TextBox")
{
row1["CategoryID"]=bd.Items[i].Cells[0].Text;
row1["CategoryType"]=bd.Items[i].Cells[8].Text;
row1["CategoryName"]=((TextBox)bd.Items[i].Cells[1].Controls[0]).Text;
row1["MaxNumForAll"]=((TextBox)bd.Items[i].Cells[2].Controls[0]).Text;
row1["MaxNumForPerContact"]=((TextBox)bd.Items[i].Cells[3].Controls[0]).Text;
row1["MaxNumForPubContact"]=((TextBox)bd.Items[i].Cells[4].Controls[0]).Text;
row1["LastUpdate"]=bd.Items[i].Cells[5].Text;
//修正"U"操作字段
if (this.updStore.Length>0)
{
for(int j=0;j<this.updStore.Length-1;j++)
{
if (Convert.ToInt16(this.updStore[j])==i)
{
row1["OprationType"]="U";
break;
}
}
}
else
{
row1["OprationType"]="";
}
this.dt.Rows.Add(row1);
}
else
{
row1["CategoryID"]=bd.Items[i].Cells[0].Text;
row1["CategoryType"]=bd.Items[i].Cells[8].Text;
row1["CategoryName"]=bd.Items[i].Cells[1].Text;
row1["MaxNumForAll"]=bd.Items[i].Cells[2].Text;
row1["MaxNumForPerContact"]=bd.Items[i].Cells[3].Text;
row1["MaxNumForPubContact"]=bd.Items[i].Cells[4].Text;
row1["LastUpdate"]=bd.Items[i].Cells[5].Text;
if (this.delStore.Length>0)
{
for(int j=0;j<this.delStore.Length-1;j++)
{
if (Convert.ToInt16(this.delStore[j])==i)
{
row1["OprationType"]="D";
break;
}
}
}
else
{
row1["OprationType"]=bd.Items[i].Cells[9].Text;
}
this.dt.Rows.Add(row1);
}
}
return this.dt;
}
mlxsx 2007-07-19
  • 打赏
  • 举报
回复
to amandag,vainnetwork

我点了编辑如代码所写,Basedatagrid1.EditItemIndex = e.Item.ItemIndex;做了如下设置,然后页面是textbox的显示模式,保留有原来的值,而且我当初也怀疑过可能默认有更改,所以我尝试特意在删除的操作代码 Basedatagrid1.EditItemIndex=e.Item.ItemIndex;又设置了当前值,或者读取原有的设定值,依然是同样的问题

to liufuyahong

datagird控件我是刚用,不是相当熟悉,在此控件上我绑定了2个按钮列,对显示出来的每一行数据进行,编辑,或删除操作,因为实际跟数据库相关联的代码我是批量处理,所以,它(按钮列)实际的操作只是更改当前行的一个列值,我无法取到datagird绑定的datatable(直接采用读取datagrid.datasource属性),所以采用BuildTBlCommand函数(利用datagrid.Items[i].Cells[0])每行每列的读形成一个datatable;

  HidMessageIdList.Value 这个text的值主要是保存,datagrid对应项的记录的操作符号,比如是对数据库删除,还是修改记录的作用!批操作的一个信息存储!
  
  读完我进行了重新绑定,要不然,datagrid的显示就是空的了!难道问题出在重新绑定上,我先按照这个思路好好看看,设定的编辑值是否保留!
liufuyahong 2007-07-18
  • 打赏
  • 举报
回复
你的BuildTBlCommand是根据Basedatagrid1的哪一个属性来执行操作的?
问题应该是出在这个函数。
还有HidMessageIdList.Value起什么作用,他是否与BuildTBlCommand有关?
vainnetwork 2007-07-18
  • 打赏
  • 举报
回复
从实际情况来看,当你点编辑了以后,除非返回非编辑状态,否则不能操作其它行的把
amandag 2007-07-18
  • 打赏
  • 举报
回复
从实际情况来看,当你点编辑了以后,除非返回非编辑状态,否则不能删除其他记录才合乎常理

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧