使用网格视图按钮删除数据库数据但返回空值

时间:2013-06-07 01:58:49

标签: c# asp.net gridview

这就是我的gridview的样子:

enter image description here

我需要删除存在隐藏列activityID and taskID的选定行,我将其设置为false,因为我需要将它们的值从数据库中删除。 我必须使用QuestionNo , activityID and taskID从数据库中删除。

ActivityID and TaskID is hidden,实际上看起来像这样:

  

QuestionNo,ActivtiyID,TaskID,QuestionContent然后删除。

代码:

    protected void gvQuestion_RowCommand(object sender, GridViewCommandEventArgs e)
    {     


    }

    protected void gvQuestion_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {

    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {


    }

    protected void gvQuestion_RowDeleting1(object sender, GridViewDeleteEventArgs e)
    {
        gvQuestion.DeleteRow(e.RowIndex);

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

        int q = Convert.ToInt32(gvQuestion.Rows[e.RowIndex].Cells[0].ToString());
        int a = Convert.ToInt32(gvQuestion.Rows[e.RowIndex].Cells[1].ToString()); // Hidden Column
        int t = Convert.ToInt32(gvQuestion.Rows[e.RowIndex].Cells[2].ToString()); // Hidden Column

        del.QuestionNo = q; 
        del.ActivityID = a;// Value of ActivityID column in GV
        del.TaskID = t; // Value of TaskID column in GV


        daoQuestion.Delete(del);
        daoQuestion.Save();
    }`

据我所知,一旦按下删除按钮,就会触发OnRowDeleting事件,所以我把删除代码放在那里,但是当我尝试删除i连接重置时,q,a,t的值为null,出了什么问题这里? ,删除按钮不起作用..请帮助,谢谢。我正在使用EF来做这件事...

这是aspx:

 <asp:GridView ID="gvQuestion" runat="server"   
                AutoGenerateColumns="False" 
            onselectedindexchanged="gvQuestion_SelectedIndexChanged" 
            onrowcommand="gvQuestion_RowCommand" onrowdeleted="gvQuestion_RowDeleted" onrowdeleting="gvQuestion_RowDeleting1" 
             >
             <Columns>
                    <asp:BoundField DataField="QuestionNo" HeaderText="QuestionNo" 
                        InsertVisible="False" ReadOnly="True" SortExpression="QuestionNo" />
                    <asp:BoundField DataField="ActivityID" HeaderText="ActivityID" 
                        SortExpression="ActivityID" Visible="False" />
                    <asp:BoundField DataField="TaskID" HeaderText="TaskID" 
                        SortExpression="TaskID" Visible="False" />
                        <asp:BoundField DataField="joined" HeaderText="QuestionContent" 
                        SortExpression="joined" >
                    <ControlStyle Width="150px" />
                    </asp:BoundField>
                    <asp:TemplateField>
                        <ItemTemplate>
                        <span onclick="return confirm('Are you sure to Delete the record?')">
                            <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Delete" 
                                onclick="LinkButton1_Click" CommandArgument="Delete">Delete</asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
      </asp:GridView>

使用数据键(duno,如果这是正确的方法):

aspx:

 DataKeyNames="QuestionNo,ActivityID,TaskID"
代码背后的代码:

  protected void gvQuestion_RowDeleting1(object sender, GridViewDeleteEventArgs e)
    {

        GridView gvQuestion = (GridView)sender;
        int row = e.RowIndex;

        int q = Convert.ToInt32(gvQuestion.DataKeys[row].Value[0].ToString());
        int a= Convert.ToInt32(gvQuestion.DataKeys[row].Value[1].ToString());
        int t = Convert.ToInt32(gvQuestion.DataKeys[row].Value[2].ToString()); // I assume that value 0 , 1 , 2 is according to the columns , not really sure on this.


 /*    Model.question del = new Model.question();
        del.QuestionNo = q; 
       del.ActivityID = a;// Value of ActivityID column in GV
        del.TaskID = t; // Value of TaskID column in GV


        daoQuestion.Delete(del);
      daoQuestion.Save();
      gvQuestion.DataBind(); */
    }

我只是想检索值,所以我会注释掉删除代码..我通过调试检查值并返回0。这是正确的方法吗?

1 个答案:

答案 0 :(得分:1)

您正在删除一行,而不是选择它,因此selectedindex将没有任何内容,除非您之前选择了一行。您应该使用GridViewDeleteEventArgs中的信息。在那里,您可以找到rowindex,以及您标记为关键列的列的数据。