如题。就是那种点[编辑],数据列表相应字段变成文本框,出现[更新][取消]的更新。我在DataGrid里加[编辑]链接老是提示错误。

解决方案 »

  1.   

    //点击编辑触发的事件
    private void DataGrid2_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    DataGrid2.EditItemIndex = e.Item.ItemIndex;
    DataGrid2.DataBind();
    this.SetBind1(); }
      

  2.   

    Dim key As String = DataGrid1.DataKeys(e.Item.ItemIndex).ToString()        Dim spbh1, lbbh1, sql, spmc1, gg1, sm1, bzq1 As String
            Dim bj1 As Decimal        Dim tb As TextBox
            tb = CType(e.Item.Cells(1).Controls(0), TextBox)
            spbh1 = tb.Text
            tb = CType(e.Item.Cells(2).Controls(0), TextBox)
            lbbh1 = tb.Text
            tb = CType(e.Item.Cells(3).Controls(0), TextBox)
            spmc1 = tb.Text
            tb = CType(e.Item.Cells(4).Controls(0), TextBox)
            gg1 = tb.Text
            tb = CType(e.Item.Cells(5).Controls(0), TextBox)
            bj1 = tb.Text
            tb = CType(e.Item.Cells(6).Controls(0), TextBox)
            bzq1 = tb.Text
            tb = CType(e.Item.Cells(7).Controls(0), TextBox)
            sm1 = tb.Text
            'TextBox1.Text = spbh1 & lbbh1
            Sqlupdate.Cancel()        Sqlupdate.Connection.Open()
            Sqlupdate.CommandText = "update tt_spzc set spbh='" & spbh1 & "',lbbh='" & lbbh1 & "',spmc='" & spmc1 & "',gg='" & gg1 & "',bj=" & bj1 & ",bzq=" & bzq1 & ",sm='" & sm1 & "' where spbh='" & key & "'"        Sqlupdate.ExecuteNonQuery()        DataGrid1.EditItemIndex = -1
            SqlDataAdapter1.Fill(MyDataSet1)
            DataGrid1.DataBind()本人也是刚学,做了一个更新的,欢迎大家来点评,我觉得是笨方法!
      

  3.   

    增删改完整例子摘自  Web Matrix<%@ Page Language="C#" %>
    <%@ import Namespace="System.Data" %>
    <%@ import Namespace="System.Data.SqlClient" %>
    <script runat="server">    // TODO: update the ConnectionString and Command values for your application
        
        string ConnectionString = "server=(local);database=pubs;trusted_connection=true";
        string SelectCommand = "SELECT au_id, au_lname, au_fname from Authors";
        
        bool isEditing = false;
        
        void Page_Load(object sender, EventArgs e) {
        
            if (!Page.IsPostBack) {
        
                // Databind the data grid on the first request only
                // (on postback, bind only in editing, paging and sorting commands)
        
                BindGrid();
            }
        }
        
        // ---------------------------------------------------------------
        //
        // DataGrid Commands: Page, Sort, Edit, Update, Cancel, Delete
        //
        
        void DataGrid_ItemCommand(object sender, DataGridCommandEventArgs e) {
        
            // this event fires prior to all of the other commands
            // use it to provide a more graceful transition out of edit mode
        
            CheckIsEditing(e.CommandName);
        }
        
        void CheckIsEditing(string commandName) {
        
            if (DataGrid1.EditItemIndex != -1) {
        
                // we are currently editing a row
                if (commandName != "Cancel" && commandName != "Update") {
        
                    // user's edit changes (if any) will not be committed
                    Message.Text = "Your changes have not been saved yet.  Please press update to save your changes, or cancel to discard your changes, before selecting another item.";
                    isEditing = true;
                }
            }
        }
        
        void DataGrid_Edit(object sender, DataGridCommandEventArgs e) {
        
            // turn on editing for the selected row
        
            if (!isEditing) {
                DataGrid1.EditItemIndex = e.Item.ItemIndex;
                BindGrid();
            }
        }
        
        void DataGrid_Update(object sender, DataGridCommandEventArgs e) {
        
            // update the database with the new values
        
            // get the edit text boxes
            string id = ((TextBox)e.Item.Cells[2].Controls[0]).Text;
            string lname = ((TextBox)e.Item.Cells[3].Controls[0]).Text;
            string fname = ((TextBox)e.Item.Cells[4].Controls[0]).Text;
        
            // TODO: update the Command value for your application
            SqlConnection myConnection = new SqlConnection(ConnectionString);
            SqlCommand UpdateCommand = new SqlCommand();
            UpdateCommand.Connection = myConnection;
        
            if (AddingNew)
                UpdateCommand.CommandText = "INSERT INTO authors(au_id, au_lname, au_fname, contract) VALUES (@au_id, @au_lname, @au_fname, 0)";
            else
                UpdateCommand.CommandText = "UPDATE authors SET au_lname = @au_lname, au_fname = @au_fname WHERE au_id = @au_id";
        
            UpdateCommand.Parameters.Add("@au_id", SqlDbType.VarChar, 11).Value = id;
            UpdateCommand.Parameters.Add("@au_lname", SqlDbType.VarChar, 40).Value = lname;
            UpdateCommand.Parameters.Add("@au_fname", SqlDbType.VarChar, 20).Value = fname;
        
            // execute the command
            try {
                myConnection.Open();
                UpdateCommand.ExecuteNonQuery();
            }
            catch (Exception ex) {
                Message.Text = ex.ToString();
            }
            finally {
                myConnection.Close();
            }
        
            // Resort the grid for new records
            if (AddingNew) {
        
                DataGrid1.CurrentPageIndex = 0;
                AddingNew = false;
            }
        
            // rebind the grid
            DataGrid1.EditItemIndex = -1;
            BindGrid();
        }
        
        void DataGrid_Cancel(object sender, DataGridCommandEventArgs e) {
        
            // cancel editing
        
            DataGrid1.EditItemIndex = -1;
            BindGrid();
        
            AddingNew = false;
        }
      

  4.   

    接上
        void DataGrid_Delete(object sender, DataGridCommandEventArgs e) {
        
            // delete the selected row
        
            if (!isEditing) {
        
                // the key value for this row is in the DataKeys collection
                string keyValue = (string)DataGrid1.DataKeys[e.Item.ItemIndex];
        
                // TODO: update the Command value for your application
                SqlConnection myConnection = new SqlConnection(ConnectionString);
                SqlCommand DeleteCommand = new SqlCommand("DELETE from authors where au_id='" + keyValue + "'", myConnection);
        
                // execute the command
                myConnection.Open();
                DeleteCommand.ExecuteNonQuery();
                myConnection.Close();
        
                // rebind the grid
                DataGrid1.CurrentPageIndex = 0;
                DataGrid1.EditItemIndex = -1;
                BindGrid();
            }
        }
        
        void DataGrid_Page(object sender, DataGridPageChangedEventArgs e) {
        
            // display a new page of data
        
            if (!isEditing) {
                DataGrid1.EditItemIndex = -1;
                DataGrid1.CurrentPageIndex = e.NewPageIndex;
                BindGrid();
            }
        }
        
        void AddNew_Click(Object sender, EventArgs e) {
        
            // add a new row to the end of the data, and set editing mode 'on'
        
            CheckIsEditing("");
        
            if (!isEditing) {
        
                // set the flag so we know to do an insert at Update time
                AddingNew = true;
        
                // add new row to the end of the dataset after binding
        
                // first get the data
                SqlConnection myConnection = new SqlConnection(ConnectionString);
                SqlDataAdapter myCommand = new SqlDataAdapter(SelectCommand, myConnection);
        
                DataSet ds = new DataSet();
                myCommand.Fill(ds);
        
                // add a new blank row to the end of the data
                object[] rowValues = { "", "", "" };
                ds.Tables[0].Rows.Add(rowValues);
        
                // figure out the EditItemIndex, last record on last page
                int recordCount = ds.Tables[0].Rows.Count;
                if (recordCount > 1)
                    recordCount--;
                DataGrid1.CurrentPageIndex = recordCount/DataGrid1.PageSize;
                DataGrid1.EditItemIndex = recordCount%DataGrid1.PageSize;
        
                // databind
                DataGrid1.DataSource = ds;
                DataGrid1.DataBind();
            }
        }
        
        // ---------------------------------------------------------------
        //
        // Helpers Methods:
        //
        
        // property to keep track of whether we are adding a new record,
        // and save it in viewstate between postbacks
        
        protected bool AddingNew {
        
            get {
                object o = ViewState["AddingNew"];
                return (o == null) ? false : (bool)o;
            }
            set {
                ViewState["AddingNew"] = value;
            }
        }
        
        void BindGrid() {
        
            SqlConnection myConnection = new SqlConnection(ConnectionString);
            SqlDataAdapter myCommand = new SqlDataAdapter(SelectCommand, myConnection);
        
            DataSet ds = new DataSet();
            myCommand.Fill(ds);
        
            DataGrid1.DataSource = ds;
            DataGrid1.DataBind();
        }</script>
    <html>
    <head>
    </head>
    <body style="FONT-FAMILY: arial">
        <h2>Editable Data Grid 
        </h2>
        <hr size="1" />
        <form runat="server">
            <asp:datagrid id="DataGrid1" runat="server" width="80%" CellSpacing="1" GridLines="None" CellPadding="3" BackColor="White" ForeColor="Black" OnPageIndexChanged="DataGrid_Page" PageSize="6" AllowPaging="true" OnDeleteCommand="DataGrid_Delete" OnCancelCommand="DataGrid_Cancel" OnUpdateCommand="DataGrid_Update" OnEditCommand="DataGrid_Edit" OnItemCommand="DataGrid_ItemCommand" DataKeyField="au_id">
                <HeaderStyle font-bold="True" forecolor="white" backcolor="#4A3C8C"></HeaderStyle>
                <PagerStyle horizontalalign="Right" backcolor="#C6C3C6" mode="NumericPages" font-size="smaller"></PagerStyle>
                <ItemStyle backcolor="#DEDFDE"></ItemStyle>
                <FooterStyle backcolor="#C6C3C6"></FooterStyle>
                <Columns>
                    <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" CancelText="Cancel" EditText="Edit" ItemStyle-Font-Size="smaller" ItemStyle-Width="10%"></asp:EditCommandColumn>
                    <asp:ButtonColumn Text="Delete" CommandName="Delete" ItemStyle-Font-Size="smaller" ItemStyle-Width="10%"></asp:ButtonColumn>
                </Columns>
            </asp:datagrid>
            <br />
            <asp:LinkButton id="LinkButton1" onclick="AddNew_Click" runat="server" Font-Size="smaller" Text="Add new item"></asp:LinkButton>
            <br />
            <br />
            <asp:Label id="Message" runat="server" width="80%" forecolor="red" enableviewstate="false"></asp:Label>
        </form>
    </body>
    </html>
      

  5.   

    楼上的出错,ResumeManage是我的表名。
    异常详细信息: System.Data.SqlClient.SqlException: 对象名 'ResumeManage' 无效。
      

  6.   

    类型“DataGridLinkButton”的控件“dgFunctionManage__ctl2__ctl0”必须放在具有 runat=server 的窗体标记内。 
    这个错误如何解决。