c#使用带有链接按钮的网格视图从数据库中删除记录

时间:2013-06-05 02:46:27

标签: c# database gridview

我有一个网格视图,例如:

enter image description here

我需要删除存在隐藏列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.的错误,错误在于delete语句。我搜索了错误,它说它可能是一个空值。我该如何解决这个问题?

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

4 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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);

答案 2 :(得分: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];

请参阅示例链接herehere以供参考

答案 3 :(得分:0)

检查Gridview中的DataKeyNames="Id"是否设置为问题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(); 
        }