我在datagrid中添加了控件自带的delete按钮,在delete_command中的代码不知道怎么写了
private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string key=this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString();    //e.item.itemidex能够获取到值,但是到DataGrid1.DataKeys提示索引超出范围???
string deleteCmd = "DELETE from Employee where empid= '"+key+"'";

ConDB mycon=new ConDB();   //这几行没有问题
mycon.Execute(deleteCmd);
mycon.Free();
}求高手告诉告诉我该怎么写?越详细越好,就当我是个笨蛋

解决方案 »

  1.   

    private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    string key=e.item.cells[].tostring()
    //e.item.cells[].tostring()  对应你表里的关键词
    string deleteCmd = "DELETE from Employee where empid= '"+key+"'";

    ConDB mycon=new ConDB();   //这几行没有问题
    mycon.Execute(deleteCmd);//运行删除命令
    mycon.Free();//释放资源
    }
      

  2.   

    string key=this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString(); 
    有可能没有DataKeys使用
    string key=this.DataGrid1[dataGrid1.CurrentRowIndex,dataGrid1.CurrentCell.ColumnNumber].ToString();