0

This is how my gridview looks like:

enter image description here

I need to delete the selected row where there is a hidden column activityID and taskID which I set visible to false because I need their value to delete it from the database. I have to delete from database using QuestionNo , activityID and taskID .

ActivityID and TaskID is hidden , it actually looks like this :

QuestionNo,ActivtiyID,TaskID,QuestionContent then delete.

Code :

    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();
    }`

From what i know once delete button is pressed , it fires OnRowDeleting event so i put my delete codes in there , however i connection reset when i try to delete , the values of q , a , t is null , what gone wrong here? , The delete button is not working.. Help please , thanks . i am using EF to do this btw...

Here is the 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>

With data keys ( duno if this is the correct way ) :

aspx :

 DataKeyNames="QuestionNo,ActivityID,TaskID"

code behind :

  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(); */
    }

I am just trying to retrieve the values so i'll comment out the delete codes .. i check the values by debugging and returns 0 . is this the right way to do it?

2

1 Answer 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.

Sign up to request clarification or add additional context in comments.

5 Comments

hi there , thanks for the reply . what do u mean by information in gridviewdeleteeventargs? .. and how can i retrieve info from there?
It's the second parameter in gvQuestion_RowDeleting1. You use it like any variable.
oh , sorry that i am new , i have updated my codes but i try to delete , the whole connection crashes.
this that the correct way to delete : int q = Convert.ToInt32(gvQuestion.Rows[e.RowIndex].Cells[0].ToString()); ?? because i need to get the value first before i could delete in database
the 3 statement returns Null and btw i converted boundfield to template field

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.