This is how my gridview looks like:

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?
QuestionNo,ActivtiyID,TaskIDas DataKey of Gridview. Check MSDN to know how to add multipleDataKeytoGridView. Check How to useRowDeletingevent of grid view using DataKey. Check Delete records in Gridview with multi-datakeynames in GridView. Hope you understand and works for you.