Solved

gridview delete button

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

810 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