Solved

Delete Row from Data Grid

Posted on 2010-09-20
7
468 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

623 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