DataSet ds=new DataSet();
dgProducts.DataSource=ds.Tables[ProductTableName];
dgProducts.DataBind();新建DataSet再绑定,还没加载数据呢

解决方案 »

  1.   

    Update要能过SqlCommand来更新数据库
    public void UpdateCommand(Object sender, DataGridCommandEventArgs e) 
    {
    // Retrieve the new text 
    int nColPositionIndex = 2; // 0-based position of the column
    int nColFromIndex = 3; // 0-based position of the column
    TextBox txtPosition = (TextBox) e.Item.Cells[nColPositionIndex].Controls[0];
            TextBox txtFrom = (TextBox) e.Item.Cells[nColFromIndex].Controls[0]; // Prepare the command text
    String strCmd = "UPDATE Employees SET title=@sPosition, country=@sCountry WHERE employeeid=@nEmpID";
    SqlConnection conn = new SqlConnection(txtConn.Text);
    SqlCommand cmd = new SqlCommand(strCmd, conn); SqlParameter p1 = new SqlParameter("@nEmpID", SqlDbType.Int);
    p1.Direction = ParameterDirection.Input;
    p1.Value = grid.DataKeys[e.Item.ItemIndex];
    cmd.Parameters.Add(p1); SqlParameter p2 = new SqlParameter("@sPosition", SqlDbType.NVarChar, 30);
    p2.Direction = ParameterDirection.Input;
    p2.Value = txtPosition.Text;
    cmd.Parameters.Add(p2); SqlParameter p3 = new SqlParameter("@sCountry", SqlDbType.NVarChar, 15);
    p3.Direction = ParameterDirection.Input;
    p3.Value = txtFrom.Text;
    cmd.Parameters.Add(p3); conn.Open();
    cmd.ExecuteNonQuery();
    conn.Close();
    // Reset the edit mode for the current item
    grid.EditItemIndex = -1; // Refresh the grid
    UpdateView();
    } private void UpdateView()
    {
    SqlConnection conn = new SqlConnection(txtConn.Text);
    SqlDataAdapter da = new SqlDataAdapter(txtCommand.Text, conn);

    DataSet ds = new DataSet();
    da.Fill(ds, "MyTable"); // Bind the data
    grid.DataSource = ds.Tables["MyTable"]; // Display the data
    grid.DataBind();
    }
      

  2.   

    将你的LoadGrid()函数改为下面的即可
    public void LoadGrid()
    {
        Connect();
    SqlDataAdapter adapter=new SqlDataAdapter(strSQLSelect,objConnection);
    DataSet ds=new DataSet();
    adapter.Fill(ds,"ProductTableName");
    Disconnect(); dgProducts.DataSource=
    ds.Tables["ProductTableName"].DefaultView;
    dgProducts.DataBind();
    }主要是dgProducts.DataSource=ds.Tables[ProductTableName]和adapter.Fill(ds,ProductTableName);
    ;的表达有问题
    改为如上即可 相应的UpdateProduct(int ProductID,decimal Price)中的也要更改