0

I have a grid view that looks like this for example :

在此处输入图像描述

我需要删除选定的行,其中有一个隐藏列 activityID 和 taskID 我将其设置为 false,因为我需要它们的值才能从数据库中删除它。

所以这是我的代码:

 protected void gvQuestion_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "delete")
        {
            Model.question del = new Model.question(); // Entitiy CRUD
            del.ActivityID = Convert.ToInt32(gvQuestion.Rows[0].Cells[2].ToString()); // Value of ActivityID column in GV
            del.TaskID = Convert.ToInt32(gvQuestion.Rows[0].Cells[3].ToString()); // Value of TaskID column in GV
            daoQuestion.Delete(del);

        }

        daoQuestion.Save(); 
    }

    protected void gvQuestion_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {
        e.ExceptionHandled = true;
    }

我按照这里的指南:http: //www.codeproject.com/Articles/111037/Delete-Data-in-GridView-Using-Template-Button

但是我使用实体框架删除,但是我得到的错误System.FormatException: Input string was not in a correct format.,错误在于删除语句。我搜索了错误,它说它可能是一个空值。我该如何解决这个问题?

顺便说一句,我也从数据库中填充了这个网格视图。

4

4 回答 4

2
var itemToDel = context.Questions.FirstOrDefault(q=>q.ActivityID = aid && q.TaskID == tid);

if(itemToDel !=null){
    context.Questions.DeleteObject(itemToDel);
    context.SaveChanges();
}

要从当前删除行中获取值,请尝试以下操作

int index = Convert.ToInt32(e.CommandArgument);

GridViewRow row = gvQuestion.Rows[index];

var aid = Convert.ToInt32(row.Cells[2].Text); 
var tid  = Convert.ToInt32(row.Cells[3].Text);
于 2013-06-05T03:12:18.567 回答
0

我认为你的转换是罪魁祸首,也就是说,你的单元格的值被转换为Int32,如果它是空的,它会给出错误"Input String was not in correct format"。因此,以下这一项可能会出错:

del.ActivityID = Convert.ToInt32(gvQuestion.Rows[0].Cells[2].Text); 
del.TaskID = Convert.ToInt32(gvQuestion.Rows[0].Cells[3].Text);

那么,为什么不使用 TryParse 并首先检查是否有值,例如:

 if (e.CommandName == "delete")
     {
        int ActID, TskID;
        Int32.TryParse(gvQuestion.Rows[0].Cells[2].Text, out ActID);
        Int32.TryParse(gvQuestion.Rows[0].Cells[3].Text, out TskID);
        // Try to Debug here to show the value of ActID and TskID if you have the right values.
        if (ActID>0 && TskID > 0)
        {
        Model.question del = new Model.question(); // Entitiy CRUD
        del.ActivityID = ActID; // Value of ActivityID column in GV
        del.TaskID = TskID; // Value of TaskID column in GV
        daoQuestion.Delete(del);

        }

    }

也尝试DataKeys而不是Rows[].Cells[]喜欢:

 gvQuestion.DataKeys(e.EditIndex).Item("ActivityID");
 gvQuestion.DataKeys(e.EditIndex).Item("TaskID");

或者:

 gvQuestion.DataKeys[0].Values[2];
 gvQuestion.DataKeys[0].Values[3];

请参阅此处此处的示例链接以供参考

于 2013-06-05T03:17:01.850 回答
0

检查DataKeyNames="Id"Gridview 中的设置为问题 ID 并删除使用以下代码,

        protected void grid_RowCommand(object sender, GridViewCommandEventArgs e)
        {
              if (e.CommandName.ToUpper().ToString() == "DELETE")
              {
                 ViewState["id"] = e.CommandArgument.ToString().Trim();
                 Model.question del = new Model.question();
                del.ActivityID = Convert.ToInt32(gvQuesViewState["id"]).ToString());             
                del.TaskID = Convert.ToInt32(ViewState["id"]).ToString());             
                daoQuestion.Delete(del);
              }
             daoQuestion.Save(); 
        }
于 2013-06-05T04:59:14.367 回答
0

我认为您需要先查询模型,如下所示。我对 linq 不太熟悉,如果有错误,请更正语法。

Model.question del = new Model.question();

var item = from d in del
           where d.ActivityID == Convert.ToInt32(gvQuestion.Rows[0].Cells[2].ToString());
           and d.TaskID == Convert.ToInt32(gvQuestion.Rows[0].Cells[3].ToString());
           select d;

daoQuestion.Delete(item);
于 2013-06-05T03:04:53.910 回答