我用DataGrid做更新、删除功能,为什么我在
private void DgWord_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DgWord.SelectedIndex = e.Item.ItemIndex;
TextBox tbWord = (TextBox)e.Item.Cells[0].Controls[1];
DropDownList ddlType = (DropDownList)e.Item.Cells[1].Controls[1];
}
取这些控件时不会出错,可把这些代码放在
private void DgWord_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
中就会出错呢,出错提示:指定的转换无效
不知这是为什么?

解决方案 »

  1.   

    DgWord_UpdateCommand和DgWord_EditCommand两个事件下的dategrid状态不同,应该是因为UpdateCommand下e.Item.Cells[0].Controls[1],[2]分别为TextBox,DropDownList。
    而EditCommand下e.Item.Cells[0].Controls[1],[2]可能是Label吧?
    所以转换时出现错误。string tbWord = e.Item.Cells[0].Controls[1].ToString();
    string ddlType = e.Item.Cells[1].Controls[1].ToString();
      

  2.   

    是因为在点编辑的时候,datagrid的行中没有TextBox 和DropDownList控件,所以转换就出错。
      

  3.   

    点编辑时有TextBox 和 DropDownList 控件
    DgWord_UpdateCommand和DgWord_EditCommand 这两个事件所对应的控件应该是相同的吧,因为是先执行DgWord_EditCommand,编辑后,再执行DgWord_UpdateCommand保存结果,都是在编辑状态吧
      

  4.   

    刚刚试过了,DgWord_UpdateCommand和DgWord_EditCommand 这两个事件所对应的控件确实不同。我是想实现这样的功能,在编辑时有一个下拉菜单,我希望下拉菜单选定的值为从数据库中选值的值,而不是默认的第一个值 ,请问怎么做,谢谢!