0

这就是我的网格视图的样子:

在此处输入图像描述

我需要删除选定的行,其中有一个隐藏列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 事件,所以我将删除代码放在那里,但是当我尝试删除时连接重置,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>

使用数据键(如果这是正确的方法,则不知道):

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 。这是正确的方法吗?

4

1 回答 1

1

You are deleting a row, not selecting it, so selectedindex won't have anything, unless you've selected a row previously. You should use the information in the GridViewDeleteEventArgs. In there you can find the rowindex, as well as the data for columns you've marked as key columns.

于 2013-06-07T02:11:42.533 回答