Solved

gridview delete button

Posted on 2006-06-16
5
874 Views
Last Modified: 2010-05-19
I'm loading data from two different tables from separate databases
I then want to be able  to delete a row from the grid by clicking on a delete button
The delete button removes a row from db1.dbo.tblA X based on x.email

GridViewShowA.Visible = true;
String objConnection = ConfigurationManager.ConnectionStrings["MyConnection"].ToString();
String strSQL = "SELECT x.office as Office, x.email as Email, z.fname as 'First Name', z.lname as 'Last Name', ";
strSQL += "z.title as Title ";
strSQL += "FROM db1.dbo.tblA X ";
strSQL += "JOIN db2.dbo.tblA Z ";
strSQL += " ON x.email = z.email ";
strSQL += "WHERE x.office = '" + DropDownListoffice.SelectedValue.ToString() + "' ";
strSQL += "AND z.email = x.email";
SqlDataAdapter objAdapter = new SqlDataAdapter(strSQL, objConnection);
DataSet dataSet = new DataSet();
objAdapter.Fill(dataSet, "myData");
DataTable dataTable = dataSet.Tables[0];
GridViewShowA.DataSource = dataTable.DefaultView;
GridViewShowA.DataBind();


I know that I can add a delete button on the aspx file but I'd prefer to do this from the code behind file. So during the build of the gridview, can I construct a delete button? Something like:

Gridview.CreateButton("Delete")?

Then when the user clicks on the delete button for a given row they should be able to delete the row from the db1.dbo.tblA X based on x.email. Something like this?


gridview.deletebutton.onclick(DeleteRow(email));


protected void DeleteRow(email);
{
  delete from sql server where email = gridview.deletebutton.email
}

Any ideas?
0
Comment
Question by:fwsteal
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 1

Expert Comment

by:dprimeus
ID: 16923673
In my grids where I allow the user to delete a row I just add a column that is a buttonfield and the buttontype = "Image".  Then I put a nice little image of an X in the ImageUrl.  You can assign the column a CommandName ="Delete".  Then attach the grid to a procedure for the RowCommand event and in the procedure you can do your delete.  That is what I do anyway.  When the user clicks the button it fires the RowCommand event which calls the procedure you created.

Procedure example:

protected void grdBondList_RowCommand(object sender, GridViewCommandEventArgs e)

{
DO YOUR DELETE HERE!
}

Hope this helps.  If you need more detail let me know.
0
 
LVL 1

Accepted Solution

by:
dprimeus earned 500 total points
ID: 16923699
Almost forgot, you need to check the e object to see which button was pressed if you have say a edit button and a delete button.

protected void grdList_RowCommand(object sender, GridViewCommandEventArgs e)

{
  if (e.CommandName == "Delete")
  {
    "delete code"
  }
}
0
 

Author Comment

by:fwsteal
ID: 16934206
Here is my aspx code:

<asp:GridView ID="GridViewSA" runat="server" BorderColor="Silver" BorderStyle="Solid" BorderWidth="1px">
  <HeaderStyle BackColor="#E0E0E0" Font-Bold="True" />
  <AlternatingRowStyle BackColor="#FF8000" BorderStyle="Solid" />
  <Columns>
   <asp:ButtonField ButtonType="Button" CommandName="Delete" HeaderText="Remove" ShowHeader="True" Text="Remove" />
  </Columns>
</asp:GridView>

code behind:
protected void GridViewSA_RowCommand(object sender, GridViewCommandEventArgs e)

{
  if (e.CommandName == "Delete")
  {
    "delete code"
  }
}

Is this how it should appear?
0
 

Author Comment

by:fwsteal
ID: 16934229
Error:

The GridView 'GridViewSA' fired event RowDeleting which wasn't handled.
0
 

Author Comment

by:fwsteal
ID: 16934857
never mind got it
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Testing of the web services 1 78
How to use NFS (Network File System) in Asp.net mvc 5? 4 71
asp.net mvc5 6 33
Run software updates from the website 6 50
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question