Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Delete Row from Data Grid

Posted on 2010-09-20
7
Medium Priority
?
481 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 35

Accepted Solution

by:
YZlat earned 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Loops Section Overview

783 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