Link to home
Start Free TrialLog in
Avatar of kirkheaton25
kirkheaton25

asked on

Delete Row from Data Grid

I need to be able to delete rows from a data grid, however if I add any columns and buttons to the form it throws up errors. (Sample code and errors attached.)

Thanks.
<%@ Page Language="C#" src="SearchUser.aspx.cs" AutoEventWireup="true" Inherits="Article15.SDisplay" ContentType="text/html" ResponseEncoding="iso-8859-1" %>
.
.
.
 <form id="Form1" method="post" runat="server">
      
		 <asp:GridView ID="GridView1" runat="server" BackColor="#dddddd"
            BorderColor="black" BorderStyle="Solid" Width="1200px" BorderWidth="5px" DataKeyNames="uid"
             EmptyDataText="Empty..." Font-Names="Verdana"
            Font-Size="Small" GridLines="None">
	        </asp:GridView>
	</form>
.
.
.
	
namespace Article15
{
  using System;
  using System.Collections;
  using System.ComponentModel;
  using System.Data;
  using System.Data.SqlClient;  
  using System.Web;
  using System.Web.SessionState;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Web.UI.HtmlControls;
  using System.Web.UI.WebControls.WebParts;
  using System.Configuration;


  public class SDisplay : System.Web.UI.Page
  {
	
	protected void Page_Load(Object s, EventArgs e)
	{	

		SQLFunction();
  	}
	public void SQLFunction() 
	{
		SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["CONNECTSTR"].ConnectionString);
		myConnection.Open();
		SqlCommand oCom = new SqlCommand();
		oCom.Connection = myConnection;
		oCom.CommandText = "dbo247722972.sp_UserSearch";
		oCom.CommandType = CommandType.StoredProcedure;
		oCom.Parameters.Add(
		new SqlParameter("@surname", "Smith"));

    		SqlDataAdapter adapter = new SqlDataAdapter(oCom);
            	DataSet ds = new DataSet();

            	adapter.Fill(ds, "sqltable");

            	GridView1.DataSource = ds;
            	GridView1.DataBind();      
            	myConnection.Close();

	}
	
	}
}

Open in new window

Parser Error Message: System.Web.UI.WebControls.DataControlFieldCollection must have items of type 'System.Web.UI.WebControls.DataControlField'. 'asp:ButtonColumn' is of type 'System.Web.UI.WebControls.ButtonColumn'.

Source Error: 


Line 27: 			<columns>
Line 28: 			<asp:ButtonColumn Text="Delete" CommandName="Delete"  ButtonType="pushButton" />
Line 29: 			</columns>
Line 30: 	        </asp:GridView>

Open in new window

Avatar of YZlat
YZlat
Flag of United States of America image

use this
<asp:GridView ID="GridView3" runat="server" BackColor="#dddddd"
            BorderColor="black" BorderStyle="Solid" Width="1200px" BorderWidth="5px" DataKeyNames="uid"
             EmptyDataText="Empty..." Font-Names="Verdana"
            Font-Size="Small" GridLines="None">
            <Columns>
            <asp:CommandField ShowEditButton="True" ShowDeleteButton="True" />
            </Columns>

Open in new window

Avatar of FredTang
FredTang

I think the button type is error,please remove the button type.
<asp:ButtonColumn   HeaderText="Delete" Text= "Del"     CommandName= "Delete" ></asp:ButtonColumn>
you should add ButtonField like this:
 <asp:ButtonField Text="Delete" CommandName="Delete" ButtonType="Button" />

You can also set GV property AutoGenerateDeleteButton="true if you don't want to do above.
ASKER CERTIFIED SOLUTION
Avatar of YZlat
YZlat
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 kirkheaton25

ASKER

Thanks, I added:
<asp:commandfield showdeletebutton="true"
            deletetext="Remove"..
and added the OnRowDeleting="Documents_Delete" to the grid but I am now getting "fired event RowDeleting which wasn't handled." errors whenever I click on a Remove link.

The code behind is below.

      protected void Documents_Delete(object sender, GridViewDeleteEventArgs e)
      {
       // Do something
      }

Thanks for your help. I've now got it working.