Click here to Skip to main content
15,885,546 members
Articles / Web Development / ASP.NET

Edit Individual GridView Cells in ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.88/5 (84 votes)
14 Nov 2009CPOL5 min read 1.4M   26.3K   305  
Edit individual GridView cells without putting the entire row into edit mode.Examples using the SqlDataSource and ObjectDataSource controls are included.
#region Directives

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;

#endregion

/// <summary>
/// Task Data Access Layer
/// </summary>
public class TaskDataAccess
{
    #region Constructors
    
    public TaskDataAccess()
	{
    }

    #endregion

    #region GetTasks

    public static List<Task> GetTasks(string sortExpression)
    {
        string sql = @"SELECT Id, Description, AssignedTo, Status FROM Tasks";
        using (SqlConnection myConnection = 
            new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
        {
            SqlCommand myCommand = new SqlCommand(sql, myConnection);
            myConnection.Open();
            SqlDataReader reader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);

            List<Task> results = new List<Task>();
            while (reader.Read())
            {
                Task task = new Task();

                task.Id = int.Parse(reader["Id"].ToString());
                task.Description = reader["Description"].ToString();
                task.AssignedTo = reader["AssignedTo"].ToString();
                task.Status = reader["Status"].ToString();

                results.Add(task);             
            }

            // Sort the collection
            switch (sortExpression)
            {
                case ("Id"):
                    results.Sort();
                    break;
                case ("Description"):
                    results.Sort(Task.DescriptionComparison);
                    break;
                case ("AssignedTo"):
                    results.Sort(Task.AssignedToComparison);
                    break;
                case ("Status"):
                    results.Sort(Task.StatusComparison);
                    break;
            }   

            reader.Close();
            myConnection.Close();
            return results;
        }
    }

    #endregion

    #region UpdateTask

    public static void UpdateTask(int id, string description, string assignedTo, string status)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("UPDATE [Tasks] SET [Description] = '");
        sb.Append(description);
        sb.Append("', [AssignedTo] = '");
        sb.Append(assignedTo);
        sb.Append("', [Status] = '");
        sb.Append(status);
        sb.Append("' WHERE [Id] = ");
        sb.Append(id);
        string sql = sb.ToString();

        using (SqlConnection myConnection =
            new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
        {
            SqlCommand myCommand = new SqlCommand(sql, myConnection);
            myConnection.Open();
            myCommand.ExecuteNonQuery();
            myConnection.Close();
        }
    }

    #endregion
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer
Ireland Ireland
I have been designing and developing business solutions for the aviation, financial services, healthcare and telecommunications industries since 1999. My experience covers a wide range of technologies and I have delivered a variety of web and mobile based solutions.

Comments and Discussions