Link to home
Start Free TrialLog in
Avatar of fwsteal
fwsteal

asked on

gridview delete button

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?
Avatar of dprimeus
dprimeus
Flag of United States of America image

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.
ASKER CERTIFIED SOLUTION
Avatar of dprimeus
dprimeus
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of fwsteal
fwsteal

ASKER

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?
Avatar of fwsteal

ASKER

Error:

The GridView 'GridViewSA' fired event RowDeleting which wasn't handled.
Avatar of fwsteal

ASKER

never mind got it