I know this is supposed to be really simple but I'm stumped, despite all my reading:
I've got a gridview full of 'Categories' pulled rom my SQL db; each row has a (hidden) 'category_id' column.
My delete button should ensure that the appropriate record is marked for deletion by category_id.
I understand that somehow the point of a datagrid is that it is bound to my datatable - but how do I leverage that so that deleting a grid row (which might be row index 1) finds and deletes the correct record (which might be category_id 3125)? (All I need to do is flag the row as deleted, then call my SQLAdapter.Update, correct?)
My gut tells me I'm overthinking it - that I don't need to explicitly hunt down the id in the row I chose, that the gridview will automagically know which one. But how?
<asp:GridView id="grdCategories" runat="server" AutoGenerateColumns="False" onrowcommand="grdCategories_RowCommand" OnRowDataBound="grdCategories_RowDataBound"> <Columns> <asp:BoundField DataField="> <asp:BoundField DataField="english_text" HeaderText="English" /> <asp:TemplateField HeaderText="Delete Category"> <ItemTemplate> <asp:Button ID="lnkDeleteCat" CommandArgument='<%# Eval("category_id") %>' CommandName="Delete" Text="Delete" runat="server" OnClientClick="return confirm('Delete this category? All questions in this category will be moved to Archive.');" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>protected void grdCategories_RowCommand(Object sender, GridViewCommandEventArgs e) { if (e.CommandName.Equals("Delete")) { int category_id = Convert.ToInt32(e.CommandArgument); DataRow rowToDelete = FindRowWithID(dsCats.Tables[0],category_id); ??dsCats.Tables[0].Rows(???).Delete(); }protected DataRow FindRowWithID(DataTable dt, int id)
{
DataRow foundRow = dt.Rows.Find("primaryKeyValue");
if (foundRow != null)
{
return foundRow;
}
else
{
return null;
}
}
![]() |
0 |
![]() |
Set the following property (on the gridview) to the primary key of your datasource.DataKeyNames
C# <---> VB.Net Translator
![]() |
0 |
![]() |
Thanks.
Of course, my attempted code is still a mess. I've got to get that right too. I'll look up DataKeyNames.
[Update:] I'm just not getting it. I must be over thinking it. I simply want to have my DELETE button delete that row from my dataset.
Every article I read says that the way to delete a datarow from a dataset is trivial: myDataTable.myDataRow.Delete(n); but it doesn't tell you how to get n. And it doesn't tell you WHERE to put that: in the RowCommand, the RowDeleting or somewhere else!
![]() |
0 |
![]() |
Hi DaveC426913,
Try this:
if (e.CommandName.Equals("Delete"))
{
string category_id = e.CommandArgument;
for (int i = 0; i < dsCats.Tables[0].Rows.Count; i++)
{
if (dsCats.Tables[0].Rows[i]["category_id"].ToString() == category_id)
{
dsCats.Tables[0].Rows[i].Delete();
dsCats.Tables[0].AcceptChanges();
}
}
}Thanks,
Qin Dian Tang
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
![]() |
0 |
![]() |
If you are trying to use one of the built in commands it probably better to use the OnDeleting event, but the code does translate well from what Qin Dian Tang, suggests.
David Fowler
SDE, ASP.NET Team, Microsoft
![]() |
0 |
![]() |
Thanks, that's got it working. (One change: e.CommandArgument.ToString() )
OK, dsCats was null when I went to delete a row, so I've added dsCats = (DataSet)Session["FAQCategories"];
It's working, except that the row isn't being deleted from the db. I checked: before the Delete and there were 5 rows in the dataset, after the Delete there are only 4 rows. So it's doing its job correctly, but when it's getting to the database, it's not deleting it. No error. This may be one of those situations I read about where the database doesn't know that there's a record to delete.
protected void grdCategories_RowCommand(Object sender, GridViewCommandEventArgs e) { dsCats = (DataSet)Session["FAQCategories"]; if (e.CommandName.Equals("Delete")) { string category_id = e.CommandArgument.ToString(); for (int i = 0; i < dsCats.Tables[0].Rows.Count; i++) { if (dsCats.Tables[0].Rows[i]["category_id"].ToString() == category_id) { dsCats.Tables[0].Rows[i].Delete(); dsCats.Tables[0].AcceptChanges(); } } DBLayer.updateCategories(dsCats); Server.Transfer("manage_categories.aspx"); } }public static int updateCategories(DataSet ds) { string sqlstr = "select * from FAQCategories ORDER BY position_num"; SqlConnection conn = new SqlConnection(_cs); try { SqlDataAdapter sqlad = new SqlDataAdapter(sqlstr, conn); new SqlCommandBuilder(sqlad); sqlad.Update(ds, "FAQCategories"); } catch (Exception) { return 0; } finally { conn.Close(); } return 1; }
![]() |
0 |
![]() |
Hi DaveC426913,
Try to use DataTable name to do deleting, make sure you have set the DataTable name "FAQCategories" in Session["FAQCategories"]:
for (int i = 0; i < dsCats.Tables["FAQCategories"].Rows.Count; i++)
{
if (dsCats.Tables["FAQCategories"].Rows[i]["category_id"].ToString() == category_id)
{
dsCats.Tables["FAQCategories"].Rows[i].Delete();
dsCats.Tables["FAQCategories"].AcceptChanges();
}
}And open SqlConnection before updating:
SqlConnection conn = new SqlConnection(_cs);
conn.Open();Here is a good example of doing updating through SqlDataAdapter.Update(): http://support.microsoft.com/kb/308055
Thanks,
Qin Dian Tang
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
![]() |
0 |
![]() |