开发者

Inline Editing of a GridView

I have a grid view that I have populated in the code behind. I have enabled the AutoGenerateEditButton on this GridView but now cannot work out how to enable inline editing.

I have looked for samples, and haven't found anything useful. Below is a copy of my ASPX page and then the code behind.

ProjectDetails.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ProjectDetails.aspx.cs" Inherits="ProjectActions.ProjectDetails" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>      
         <asp:GridView ID="ProjectDetailsGrid" 
             runat="server" 
             AutoGenerateColumns="False" 
             AutoGenerateEditButton="True" CellPadding="3" 
             GridLines="Horizontal" BackColor="White" BorderColor="Black" 
             BorderStyle="Solid" BorderWidth="6px">
             <AlternatingRowStyle BackColor="#F7F7F7" />
            <Columns>
            <asp:BoundField HeaderText ="ID" DataField="ActionID" />
            <asp:BoundField HeaderText ="Summary" DataField="ActionSummary" />

            <asp:BoundField HeaderText ="Description" DataField="ActionDescription" />
            <asp:BoundField HeaderText ="Start Date" DataField="ActionStartDate" />
            <asp:BoundField HeaderText ="Target Close Date" DataField="ActionTargetCloseDate" />
            <asp:BoundField HeaderText ="Actual Close Date" DataField="ActionActualCloseDate" />
            <asp:BoundField HeaderText ="Status" DataField="ActionStatus" />
            <asp:BoundField HeaderText ="Owner" DataField="ActionOwner" />
            <asp:BoundField HeaderText ="Last Modified" DataField="ActionLastModified" />
            <asp:BoundField HeaderText ="Action Update" DataField="ActionUpdate" />
            </Columns>
             <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
             <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
             <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
             <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
             <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
             <SortedAscendingCellStyle BackColor="#F4F4FD" />
             <SortedAscendingHeaderStyle BackColor="#5A4C9D" />
             <SortedDescendingCellStyle BackColor="#D8D8F0" />
             <SortedDescendingHeaderStyle BackColor="#3E开发者_Python百科3277" />
         </asp:GridView>
    </div>
    </form>
    <asp:Label ID="ErrorMessage" runat="server" Text=""></asp:Label>
    <br />
    <br />
     <asp:Label ID="SQLStatement" runat="server" Text=""></asp:Label>
</body>
</html>

ProjectDetails.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Text;
using System.Data;

namespace ProjectActions
{
    public partial class ProjectDetails : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                BindProjectDetails();
            }
        }


        public void BindProjectDetails()
        {

            string QueryStringID = Request.QueryString["id"];

            if (QueryStringID == "")
            {
                QueryStringID = "0";
            }


            string mySQLstring = "Select ";
            mySQLstring = mySQLstring + "Actions.ActionID, ";
            mySQLstring = mySQLstring + "Actions.ActionSummary, ";
            mySQLstring = mySQLstring + "ActionDetails.ActionDescription, ";
            mySQLstring = mySQLstring + "ActionDetails.ActionStartDate, ";
            mySQLstring = mySQLstring + "ActionDetails.ActionTargetCloseDate, ";
            mySQLstring = mySQLstring + "ActionDetails.ActionActualCloseDate, ";
            mySQLstring = mySQLstring + "ActionDetails.ActionStatus, ";
            mySQLstring = mySQLstring + "ActionDetails.ActionOwner, ";
            mySQLstring = mySQLstring + "ActionDetails.ActionLastModified, ";
            mySQLstring = mySQLstring + "ActionDetails.ActionUpdate ";
            mySQLstring = mySQLstring + "FROM ActionDetails ";
            mySQLstring = mySQLstring + "INNER JOIN Actions ON ActionDetails.ActionID = Actions.ActionID ";
            mySQLstring = mySQLstring + "INNER JOIN Projects ON ActionDetails.ProjectID = Projects.ProjectID ";
            mySQLstring = mySQLstring + "AND ActionDetails.ProjectID = " + QueryStringID + " ";
            mySQLstring = mySQLstring + "ORDER BY ActionID ASC";

            string champeryConnection = WebConfigurationManager.ConnectionStrings["TheDatebase"].ConnectionString;

            //Error Handling for SQL Connection
            try
            {
                using (SqlConnection myConnection = new SqlConnection(champeryConnection))
                {

                    SqlCommand myCommand = new SqlCommand(mySQLstring, myConnection);
                    // opens the connection to the database
                    myConnection.Open();

                    DataTable projectData = new DataTable("ActionDetails");
                    SqlDataAdapter DataAdapter = new SqlDataAdapter();

                    DataAdapter.SelectCommand = myCommand;


                    DataAdapter.Fill(projectData);


                    ProjectDetailsGrid.DataSource = projectData;
                    ProjectDetailsGrid.DataBind();

                    if(ProjectDetailsGrid.Rows.Count == 0){
                        Response.Redirect("\\ProjectActionsDetails/AddProjectActionsDetails.aspx?id=" + Request.QueryString["id"] + "&name=" + Request.QueryString["name"] + "");
                    }

                }

            }
            //Write errors to Label 'Error'
            catch (Exception Err)
            {
                ErrorMessage.Text = Err.ToString();
                SQLStatement.Text = mySQLstring;
            }
        }
    }
}


The easiest way to enable editing is to give the gridview a sqldatasource that has update parameters and an updatecommand. here is more on that: http://csharpdotnetfreak.blogspot.com/2009/05/gridview-sqldatasource-insert-edit.html


If you are not obligated with using a GridView, I would strongly recommend using a ListView:

http://basgun.wordpress.com/2007/12/29/listview-control-in-aspnet-35-3/

http://www.codedigest.com/Articles/ASPNET/105_EditUpdateDelete_and_Insert_in_ListView_Control.aspx

To me, ListView is more flexible and easy to implement.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜