protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGrid();
        }
    }
    protected void DataGrid1_EditCommand(object source, DataGridCommandEventArgs e)
    {
        DataGrid1.EditItemIndex = (int)e.Item.ItemIndex;
        BindGrid();
    }
    protected void DataGrid1_UpdateCommand(object source, DataGridCommandEventArgs e)
    {
        string strupdate = "";
        strupdate += "学号 ='"+((TextBox)(e.Item.Cells[1].Controls[0])).Text+"'";
        strupdate += ",姓名 ='"+((TextBox)(e.Item.Cells[2].Controls[0])).Text+"'";
        strupdate += ",语文 ='"+((TextBox)(e.Item.Cells[3].Controls[0])).Text+"'";
        strupdate += ",数学 ='"+((TextBox)(e.Item.Cells[4].Controls[0])).Text+"'";
        strupdate += ",英语 ='"+((TextBox)(e.Item.Cells[5].Controls[0])).Text+"'";
        strupdate += ",政治 ='"+((TextBox)(e.Item.Cells[6].Controls[0])).Text+"'";
        string updata = "UPDATE grades SET " + strupdate + " where 学号='" +((TextBox)e.Item.Cells[1].Controls[0]).Text+"'";
        SqlCommand Mycommand = new SqlCommand(updata,myConnection);
        Mycommand.Connection.Open();
        try
        {
            Mycommand.ExecuteNonQuery();
            DataGrid1.EditItemIndex = -1;
        }
        catch
        {
            Response.Write("未能更新字段,请确保正确填了字段");
        }
        BindGrid();着里是绑定数据
     
    }
    protected void DataGrid1_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
        DataGrid1.CurrentPageIndex = e.NewPageIndex;
        BindGrid();
    }
    protected void DataGrid1_CancelCommand(object source, DataGridCommandEventArgs e)
    {
        DataGrid1.EditItemIndex = -1;
        BindGrid();
    }小弟不才,刚接触着方面的东西~~~希望各位帮我看哈~~~
 页面运行时数据能够正常显示,但更新的时候就会出错~~
 显示“ConnectionString 属性尚未初始化。” 指向这句Mycommand.Connection.Open();

解决方案 »

  1.   

    意思就是说ConnectionString 你还没有给它值,你这样不能跟数据库打上交道。
    应该要有server=.;database=数据库名;uid=sa;pwd=密码;之类的连接字符串。
      

  2.   

    SqlConnection myConnection = new SqlConnection("server=.;sa=sa;pwd=;database=northwindow")
    SqlCommand cmd = new SqlCommand(updata, myConnectionn);
    try
    {
    conn.Open();
    cmd.ExcuteNonQuery();
     DataGrid1.EditItemIndex = -1;}
    catch()
    {}
      

  3.   

    可 我前面有这样的语句列~~~
    public partial class _Default : System.Web.UI.Page 
    {
        SqlConnection myConnection = new SqlConnection();
        private void BindGrid()
        {
            string strconn = "server=localhost;uid=sa;pwd=19870109;database=students";
            myConnection.ConnectionString = strconn;
            string strsql = "select 学号,姓名,语文,数学,英语,政治 from grades";
            SqlDataAdapter da = new SqlDataAdapter(strsql,myConnection);
            DataSet ds = new DataSet();
            da.Fill(ds,"scores");
            ds.Tables["scores"].DefaultView.Sort="语文";
            DataGrid1.DataSource = ds.Tables["scores"].DefaultView;
            DataGrid1.DataBind();
      

  4.   

    你这个string strconn = "server=localhost;uid=sa;pwd=19870109;database=students";
    只是在BindGrid() 给myConnection 定义了 把这个拿到外面。public partial class _Default : System.Web.UI.Page 
    {
        SqlConnection myConnection = new SqlConnection();
        string strconn = "server=localhost;uid=sa;pwd=19870109;database=students";
        myConnection.ConnectionString = strconn;
      
        private void BindGrid()
        {
    ...
    ....
      

  5.   

    Mycommand.Connection.Open();
    你在更新的时候没有CONNECTION对象必须指定Mycommand的CONNECTION STRING。。然后在打开连接。
      

  6.   

    你只有在BindGrid中才初始化,updatecommand时并没有执行BindGrid
      

  7.   

    小弟也不才,
    你把SqlConnection myConnection = new SqlConnection();改为
    SqlConnection myConnection = new SqlConnection("server=localhost;uid=sa;pwd=19870109;database=students"),后面的一些语句省了,试一下
      

  8.   

    搞定了哦~~呵呵~~~
      
    SqlConnection myConnection = new SqlConnection("server=localhost;uid=sa;pwd=19870109;database=students"),我就这样改了下~~再次感谢~~