Solved

Delete Row from Data Grid

Posted on 2010-09-20
7
448 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
DLL in ASP.NET 20 41
ASP.NET Web Form Variables being reset 2 18
Re-position the objects 7 50
Jquery autocomplete 10 26
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
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.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

705 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now