La Modification en ligne d'un GridView
J'ai un affichage de la grille que j'ai renseigné dans le code derrière. J'ai activé l'AutoGenerateEditButton sur ce GridView, mais maintenant ne peut pas travailler sur la façon d'activer la modification en ligne.
J'ai regardé pour les échantillons, et n'ai pas trouvé quelque chose d'utile. Ci-dessous est une copie de ma page ASPX et puis le code derrière.
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;
}
}
}
}
OriginalL'auteur David Longfellow | 2011-01-17
Vous devez vous connecter pour publier un commentaire.
La façon la plus simple pour activer l'édition est de donner le contrôle gridview un sqldatasource dont la mise à jour des paramètres et un updatecommand.
ici, c'est plus sur que:
http://csharpdotnetfreak.blogspot.com/2009/05/gridview-sqldatasource-insert-edit.html
OriginalL'auteur boruchsiper
Si vous n'êtes pas obligé d'utiliser un GridView, je les recommande vivement à l'aide d'une 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
Pour moi, la liste est plus souple et facile à mettre en œuvre.
OriginalL'auteur ncakmak