GridView的内联编辑

时间:2011-01-17 15:09:12

标签: c# .net asp.net

我有一个网格视图,我已经在后面的代码中填充了。我已在此GridView上启用了AutoGenerateEditButton,但现在无法确定如何启用内联编辑。

我找了样品,但没找到任何有用的东西。下面是我的ASPX页面的副本,然后是后面的代码。

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="#3E3277" />
         </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;
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

启用编辑的最简单方法是为gridview提供一个sqldatasource,它具有更新参数和updatecommand。 这里有更多: http://csharpdotnetfreak.blogspot.com/2009/05/gridview-sqldatasource-insert-edit.html

答案 1 :(得分:0)

如果您没有义务使用GridView,我强烈建议您使用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

对我来说,ListView更灵活,更易于实现。