Solved

Delete Row from Data Grid

Posted on 2010-09-20
7
455 Views
Last Modified: 2012-06-22
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

0
Comment
Question by:kirkheaton25
7 Comments
 
LVL 35

Expert Comment

by:YZlat
ID: 33717414
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

0
 
LVL 5

Expert Comment

by:FredTang
ID: 33717428
I think the button type is error,please remove the button type.
<asp:ButtonColumn   HeaderText="Delete" Text= "Del"     CommandName= "Delete" ></asp:ButtonColumn>
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 33717462
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.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 35

Expert Comment

by:YZlat
ID: 33717493
0
 
LVL 35

Accepted Solution

by:
YZlat earned 500 total points
ID: 33717504
0
 
LVL 1

Author Comment

by:kirkheaton25
ID: 33717912
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
      }

0
 
LVL 1

Author Closing Comment

by:kirkheaton25
ID: 33717933
Thanks for your help. I've now got it working.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

829 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