注意你的Page_Load事件:
if(!IsPostBack)
{
  //绑定数据
}

解决方案 »

  1.   

    这个 
     我可能与你碰到的问题不同
      
     以前  我在dream....中出现过几次这个问题 但是 我在   .net 中没有碰到过。
    也许是楼上那位大哥所说吧
      

  2.   

    一个对DataGrid进行编辑,排序,更新等操作得例子<%@ 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;
        }
        
        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>
      

  3.   

    <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" DataKeyField="au_id" OnItemCommand="DataGrid_ItemCommand" OnEditCommand="DataGrid_Edit" OnUpdateCommand="DataGrid_Update" OnCancelCommand="DataGrid_Cancel" OnDeleteCommand="DataGrid_Delete" AllowPaging="true" PageSize="6" OnPageIndexChanged="DataGrid_Page" ForeColor="Black" BackColor="White" CellPadding="3" GridLines="None" CellSpacing="1" width="80%">
                <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" Text="Add new item" Font-Size="smaller"></asp:LinkButton>
            <br />
            <br />
            <asp:Label id="Message" runat="server" width="80%" forecolor="red" enableviewstate="false"></asp:Label>
        </form>
    </body>
    </html>
      

  4.   

    这边有个例子,你可以参考一下:
    http://chs.gotdotnet.com/quickstart/util/srcview.aspx?path=/quickstart/aspplus/samples/webforms/data/datagrid6.src&file=CS\datagrid6.aspx&font=3