在System.Web.UI.DataGrid组件中,添加了一个模板列,加入一个文本框,一个按钮列(编辑,更新,取消)请教如何实现可编辑列?即单击编辑按钮进入编辑状态,可编辑列显示文本框等待输入,按钮文字变成"更新",单击后提交服务器执行更新操作.请高手指点

解决方案 »

  1.   

    http://www.ccw.com.cn/applic/prog/htm2003/20030828_13IYP.asp
      

  2.   

    在模板列中,写一个<edititemtemplate>,在这里可以写编辑的文本框之类的
      

  3.   

    <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="&lt;img border=0 src=images/ok.gif&gt;" HeaderText="编辑" CancelText="&lt;img border=0 src=images/Cancel.gif&gt;" EditText="&lt;img border=0 src=control/image/folder.ico&gt;">
    </asp:EditCommandColumn>//声明一编辑列
    <asp:TemplateColumn HeaderText="名称">
    //正常显示列
    <ItemTemplate><%#DataBind.Eval(Container.DataItem,"ColumnsName")%></ItemTemplate>
    //触发编辑事件时显示的列:
    <EditItemTemplate>
    <asp:TextBox Runat="server" ID="txtNewName" CssClass="ECFtxt" Text='<%#DataBind.Eval(Container.DataItem,"ColumnsName")%>'></asp:TextBox>
    </EditItemTemplate>
    </asp:TemplateColumn>
    在后台声明DataGrid编辑事件:
    private void dgEdu_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    dgEdu.EditItemIndex = (int)e.Item.ItemIndex;
    dgEdu.DataSource = DataSets;
    dgEdu.DataBind();
    }
      

  4.   

    还是去孟子老大的网站很好的 http://dotnet.aspx.cc/
      

  5.   

    给你一个例子,改一下就可以了
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;namespace WebApplicationCSharp
    {
    /// <summary>
    /// VC69 的摘要描述。
    /// </summary>
    public class VC69 : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.DataGrid StudentDataGrid;
    protected System.Web.UI.WebControls.Label Message;

    private void Page_Load(object sender, System.EventArgs e)
    {
    if (!IsPostBack)
    BindGridToSource();
    } #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 此呼叫为 ASP.NET Web Form 设计工具的必要项。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 此为设计工具支援所必需的方法 - 请勿使用程式码编辑器修改
    /// 这个方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.StudentDataGrid.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.StudentDataGrid_CancelCommand);
    this.StudentDataGrid.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.StudentDataGrid_EditCommand);
    this.StudentDataGrid.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.StudentDataGrid_UpdateCommand);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion private void StudentDataGrid_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    StudentDataGrid.EditItemIndex = e.Item.ItemIndex;
    BindGridToSource();
    } private void StudentDataGrid_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    StudentDataGrid.EditItemIndex = -1;
    BindGridToSource();
    Message.Text = "";
    } private void StudentDataGrid_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    SqlConnection MyConnection = new SqlConnection("server=(local)\\NetSDK;database=北风贸易;Trusted_Connection=yes");
    // 建立 UPDATE 命令字符串
    string UpdateCmd = "UPDATE 学生 SET 学生姓名 = @SName, 性别 = @Gender, 家长姓名 = @PName," +
    "身高 = @Height, 体重 = @Weight, 血型 = @BloodType WHERE 身份证号码 = @uid";

    SqlCommand MyCommand = new SqlCommand(UpdateCmd, MyConnection);

    // 设定 UPDATE 命令字串中各个参数的数据类型与长度
    MyCommand.Parameters.Add(new SqlParameter("@uid", SqlDbType.NVarChar, 18));
    MyCommand.Parameters.Add(new SqlParameter("@SName", SqlDbType.NVarChar, 12));
    MyCommand.Parameters.Add(new SqlParameter("@Gender", SqlDbType.Bit));
    MyCommand.Parameters.Add(new SqlParameter("@PName", SqlDbType.NVarChar, 12));
    MyCommand.Parameters.Add(new SqlParameter("@Height", SqlDbType.Real));
    MyCommand.Parameters.Add(new SqlParameter("@Weight", SqlDbType.Real));
    MyCommand.Parameters.Add(new SqlParameter("@BloodType", SqlDbType.NVarChar, 3));

    // 取得主索引键之值(此处为身份证号码)
    MyCommand.Parameters["@uid"].Value = StudentDataGrid.DataKeys[e.Item.ItemIndex];

    string[] Cols = new string[] {"@uid", "@SName", "@Gender", "@PName", "@Height", "@Weight", "@BloodType"};

    // 取得 TableCell 物件的数目,也就是每一数据列之储存格的数目(等於 DataGrid 伺服器控制项中数据行的数目)
    int NumCols = e.Item.Cells.Count;

    // 略过第一栏与第二栏。因为第一栏是按钮,第二栏的身份证号码则已在之前取得。
    for (int i = 2; i<= NumCols - 1; i++)
    {
    TextBox CurrentTextBox = (TextBox)e.Item.Cells[i].Controls[0];
    string ColValue = CurrentTextBox.Text;

    // 检查字段(储存格)的内容是否为 Null 值
    if (ColValue == "")
    {
    Message.Text = "错误: 每一个字段都必须输入数据不允许 Null 值!";
    return;
    }

    // 将各字段(储存格)的数据指派给 UPDATE 命令中的参数
    if (i == 3)
    {
    // 第四个字段(储存格)是「性别」,因此必须将 True 转换成 1,
    // 并将 False 转换成 0,然後再指派给参数 @Gender 。
    if (CurrentTextBox.Text == "True")
    MyCommand.Parameters["@Gender"].Value = 1;
    else
    MyCommand.Parameters["@Gender"].Value = 0;
    }
    else
    MyCommand.Parameters[Cols[i - 1]].Value = ColValue;
    } // 开启连接
    MyCommand.Connection.Open();

    try
    {
    // 呼叫 ExecuteNonQuery() 方法以便针对数据来源执行 UPDATE 命令
    MyCommand.ExecuteNonQuery();
    Message.Text = "<b>已更新数据纪录</b><br>";

    // 完成更新作业后使数据行跳出编辑模式
    StudentDataGrid.EditItemIndex = -1;
    }
    catch (SqlException Exp)
    {
    if (Exp.Number == 2627)
    Message.Text = "错误: 具有相同主索引键的数据纪录已经存在。";
    else
    Message.Text = "错误: 无法更新数据纪录,请确定各字段是否都已正确输入。";
    }

    // 关闭连接
    MyCommand.Connection.Close();
    BindGridToSource();
    } private void BindGridToSource()
    {
    SqlConnection MyConnection = new SqlConnection("server=(local)\\NetSDK;database=北风贸易;Trusted_Connection=yes");
    SqlDataAdapter MyCommand = new SqlDataAdapter("SELECT 身份证号码,学生姓名,性别,家长姓名,身高,体重,血型 FROM 学生", MyConnection);
    DataSet ds = new DataSet();
    MyCommand.Fill(ds, "学生");
    StudentDataGrid.DataSource = ds.Tables["学生"].DefaultView;
    StudentDataGrid.DataBind();
    }

    }
    }
      

  6.   

    前台代码:
    <%@ Page language="c#" Codebehind="VC69.aspx.cs" AutoEventWireup="false" Inherits="WebApplicationCSharp.VC69" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>编辑数据列使用示范</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <form id="VC69" method="post" runat="server">
    <asp:DataGrid id="StudentDataGrid" style="Z-INDEX: 101; LEFT: 10px; POSITION: absolute; TOP: 106px" runat="server" DataKeyField="身份证号码" Width="483px" Font-Size="9pt" BorderColor="#8080FF" BackColor="#FFFFCC">
    <HeaderStyle HorizontalAlign="Center" ForeColor="#990000" BackColor="#CCFF33"></HeaderStyle>
    <Columns>
    <asp:EditCommandColumn ButtonType="PushButton" UpdateText="更新" HeaderText="异动按钮" CancelText="取消" EditText="编辑">
    <ItemStyle Wrap="False"></ItemStyle>
    </asp:EditCommandColumn>
    </Columns>
    </asp:DataGrid>
    <asp:Label id="Message" style="Z-INDEX: 102; LEFT: 10px; POSITION: absolute; TOP: 78px" runat="server" ForeColor="Red"></asp:Label><IMG style="Z-INDEX: 103; LEFT: 8px; POSITION: absolute; TOP: 8px" alt="" src="Images/Title86.jpg">
    </form>
    </body>
    </HTML>