gpt4 book ai didi

c# - GridView 的内联编辑

转载 作者:行者123 更新时间:2023-11-30 18:06:36 24 4
gpt4 key购买 nike

我在后面的代码中填充了一个 GridView 。我已在此 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;
}
}
}
}

最佳答案

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

关于c# - GridView 的内联编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4714634/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com