Solved

gridview delete button

Posted on 2006-06-16
5
864 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

920 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now