如果不行的话,应该用什么控件来实现啊

解决方案 »

  1.   

    支持编辑模式啊/
    在grid_EditCommand事件里些上:grid.EditItemIndex = e.NewPageIndex;
    OK了。
      

  2.   

    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=hm;database=北风贸易;User Id=sa;");
    // 建立 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=hm;database=北风贸易;User Id=sa;");
    SqlDataAdapter MyCommand = new SqlDataAdapter("SELECT 身份证号码,学生姓名,性别,家长姓名,身高,体重,血型 FROM 学生", MyConnection);
    DataSet ds = new DataSet();
    MyCommand.Fill(ds, "学生");
    StudentDataGrid.DataSource = ds.Tables["学生"].DefaultView;
    StudentDataGrid.DataBind();
    }

    }
    }
      

  3.   

    <%@ Page language="c#" Codebehind="更新.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>
      

  4.   

    e.NewPageIndex为什么这个找不到?
    说没有定义
      

  5.   

    谁来教下我啊
    zhenjiaobing的代码里我运行有错,
    21aspnet是一个示例,看不明白啊
      

  6.   

    在数据源中 写以下代码 你就可以进行更新 删除 操作
     <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:connstring %>"
                SelectCommand="SELECT * FROM [message] ORDER BY [time] DESC"
                 updatecommand="Update [message] SET title=@title, name=@name, content=@content, ipaddress=@ipaddress, time=@time WHERE (id = @id)"
                deletecommand="Delete from [message] where id = @id"
                ></asp:SqlDataSource>
      

  7.   

    guo46941598,请问你那段代码写在哪里啊
    是写在DataGrid的数据源中吗?