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.Conne ctionStrin gs["MyConn ection"].T oString();
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.Selecte dValue.ToS tring() + "' ";
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("Del ete")?
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.oncl ick(Delete Row(email) );
protected void DeleteRow(email);
{
delete from sql server where email = gridview.deletebutton.emai l
}
Any ideas?
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.Conne
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.Selecte
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("Del
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.oncl
protected void DeleteRow(email);
{
delete from sql server where email = gridview.deletebutton.emai
}
Any ideas?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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(obje ct sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
"delete code"
}
}
Is this how it should appear?
<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(obje
{
if (e.CommandName == "Delete")
{
"delete code"
}
}
Is this how it should appear?
ASKER
Error:
The GridView 'GridViewSA' fired event RowDeleting which wasn't handled.
The GridView 'GridViewSA' fired event RowDeleting which wasn't handled.
ASKER
never mind got it
Procedure example:
protected void grdBondList_RowCommand(obj
{
DO YOUR DELETE HERE!
}
Hope this helps. If you need more detail let me know.