本帖最后由 Couquettish 于 2014-05-22 09:01:47 编辑

解决方案 »

  1.   

    调用datasetresource() 你把ds clear()了
      

  2.   

    把ds.clear();注释之后还是无法更新··
      

  3.   

    this.dataGridView1.DataSource = ds.Tables[0];
    后面加上
    this.dataGridView1.DataBind();
      

  4.   


    //datagridview1.datasource = ds.tables["ReportText_1"];大小写不对。
      

  5.   


    报错·············"System.Windows.forms.DataGridView" 不包含"DataBind"的定义····
      

  6.   


    把"ds.tables"改成"ds.Tables"?
    报错······················“System.Windows.Forms.DataGridView”并不包含“datasource”的定义··
      

  7.   


    报错·············"System.Windows.forms.DataGridView" 不包含"DataBind"的定义····

    不好意思,把DataGridView看成GridView了。
      

  8.   


    是不是改成这样?
    dataGridView1.DataSource = ds.Tables["ReportText_1"];
      

  9.   


    是不是改成这样?
    dataGridView1.DataSource = ds.Tables["ReportText_1"];[/quote还是无法更新··
      

  10.   

    在我本机上测试下面的代码可以更新,请试一下,保证ReportText_1表里有主键,否则不能自动生成更新查询,需要手动设置UpdateCommand:
    public DataSet ds = new DataSet();
    public SqlDataAdapter sdr = new SqlDataAdapter();
    string strSql = "select * from ReportText_1";
    public SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings[1].ConnectionString.ToString());

    private void button1_Click(object sender, EventArgs e)
    {
    //ds.Tables.Add("ReportText_1");
    SqlCommandBuilder scb = new SqlCommandBuilder(sdr);
    sdr.UpdateCommand = scb.GetUpdateCommand();
    //sdr.InsertCommand = scb.GetInsertCommand();

    //SetDataResource(); sdr.Update(ds, "ReportText_1");
    } public void SetDataResource()
    {
    conn.Open();
    sdr = new SqlDataAdapter(strSql, conn);

    ds.Clear();
    sdr.Fill(ds, "ReportText_1");
    dataGridView1.DataSource = ds.Tables["ReportText_1"];
    conn.Close(); this.dataGridView1.DataSource=null;
    this.dataGridView1.DataSource = ds.Tables["ReportText_1"];
    }
      

  11.   

    从错误信息看,你的程序里面好像没有有Form1_Load这个方法,如果没有,就把提示出错的代码的这行注释掉再看一下。
      

  12.   

    有这句吗:SqlCommandBuilder scb = new SqlCommandBuilder(sdr);
    你把你的代码贴的全一点,不然不好判断
      

  13.   

    麻烦了public DataSet ds = new DataSet();        public SqlDataAdapter sdr = new SqlDataAdapter();        string strSql = "select * from ReportText_1";        public SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings[1].ConnectionString.ToString());        private void button1_Click(object sender, EventArgs e)
            {
                //ds.Tables.Add("RportText_1");            SqlCommandBuilder scb = new SqlCommandBuilder(sdr);            //DataTable table = this.dataGridView1.DataSource as DataTable;            //conn.Open();
                
                sdr.UpdateCommand=scb.GetUpdateCommand();
                //datasetresource();                        sdr.Update(ds, "RportText_1");            //conn.Close();        }
            public void SetDataResource()
            {            conn.Open();            sdr = new SqlDataAdapter(strSql, conn);            ds.Clear();            sdr.Fill(ds, "ReportText_1");            dataGridView1.DataSource = ds.Tables["ReportText_1"];            conn.Close();            this.dataGridView1.DataSource = null;
                this.dataGridView1.DataSource = ds.Tables["ReportText_1"];        }        /*public void datasetresource()
            {            conn.Open();            sdr = new SqlDataAdapter(strSql, conn);            //ds.Clear();            sdr.Fill(ds, "ReportText_1");            dataGridView1.DataSource = ds.Tables["ReportText_1"];            conn.Close();            this.dataGridView1.DataSource = null;            this.dataGridView1.DataSource = ds.Tables[0];
                //this.dataGridView1.DataBind();        }*/
            public Form1()        {            InitializeComponent();        }
            private void Form1_load(object sender, EventArgs e)
            {//            datasetresource();        }
                   
            private void button1_Click_1(object sender, EventArgs e)
            {            OutPut Out = new OutPut();            Out.ShowDialog();        }        private void Form1_Load_1(object sender, EventArgs e)
            {            // TODO: 这行代码将数据加载到表“cpyDataSet.ReportText_1”中。您可以根据需要移动或移除它。            this.reportText_1TableAdapter1.Fill(this.cpyDataSet.ReportText_1);
            }        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
            {            dataGridView1.Columns[0].ReadOnly = true;        }        private void reportViewer1_Load(object sender, EventArgs e)
            {        }
      

  14.   

    你的代码的第30行sdr.Update(ds, "RportText_1");应该是ReportText_1吧?
      

  15.   

    ````````失误··  现在已经改了 但是依旧报错····需要初始化selectCommand
      

  16.   

    按照你的代码,在Button1被点击的时候(SqlCommandBuilder scb = new SqlCommandBuilder(sdr);),sdr可能是空的,andBuilder scb = new SqlCommandBuilder(sdr);,所以出错了。应该需要给sdr传入sql语句和conn连接(sdr = new SqlDataAdapter(strSql, conn);)。我看到你本来是在Form_Load事件里有SetDataResource();所以在你下面的代码里还是调用它。这样初始化的错误就解决了。