Solved

gridview delete button

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

759 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

20 Experts available now in Live!

Get 1:1 Help Now