2

刚刚处理最近需要帮助的事情,我再次卡在这里..我正在使用数据网格从数据库中检索值,我想使用类似的数据网格更新数据库,但不知何故我遇到了麻烦,谁能指导我如何做到这一点.. ...我的意思是当执行查询并检索选定的数据时,如果用户想要更新他/她可以在显示值的数据网格中执行的操作......

if (textBox1.Text != ""|| textBox1.Text==null)
        {
            textBox3.Enabled = false;
            dateTimePicker1.Enabled = false;
            dateTimePicker2.Enabled = false;
            String txt = textBox1.Text;
            dataGridView1.DataSource = null;
            dataGridView1.Rows.Clear();
            dataGridView1.Refresh();

            OleDbDataAdapter dAdapter = new OleDbDataAdapter("SELECT * FROM [BDetails] WHERE ([BranchCode] = '" + @txt + "')", connParam);
            OleDbCommandBuilder cBuilder = new OleDbCommandBuilder(dAdapter);


             dataTable = new DataTable();
            DataSet ds = new DataSet();

            dAdapter.Fill(dataTable);

            if (dataTable.Rows.Count > 0)
            {
                for (int i = 0; i < dataTable.Rows.Count; i++)
                {
                    dataGridView1.Rows.Add(dataTable.Rows[i][0], dataTable.Rows[i][1], dataTable.Rows[i][2], dataTable.Rows[i][3], dataTable.Rows[i][4], dataTable.Rows[i][5], dataTable.Rows[i][6], dataTable.Rows[i][7], dataTable.Rows[i][8], dataTable.Rows[i][11], dataTable.Rows[i][12]);

                }

            }// end inner if
            else
            {
                MessageBox.Show("No Record Found");
                textBox3.Enabled = true;
                dateTimePicker1.Enabled = true;
                dateTimePicker2.Enabled = true;
            }// end inner else


        }// end outer if
        else 
        {
            MessageBox.Show("Please Enter Branch Code");
            bookConn.Close();

        }// end outer else

我绝望的家伙帮帮我

private void button8_Click(object sender, EventArgs e) { ReadData(); // 保存数据();

    }// end function

    void ReadData()
    {
        this.ds = new DataSet();
      //  string connString = "CONNICTION STRING GOES HERE";
        dAdapter = new OleDbDataAdapter("select * from BDetails", connParam);
        this.dAdapter.Fill(this.ds,"[BDetails]");
        this.ds.AcceptChanges();
        //set the table as the datasource for the grid in order to show that data in the grid
        this.dataGridView1.DataSource = ds.DefaultViewManager;
    }// end function

    void SaveData()
    {
        DataSet changes = this.ds.GetChanges();
        if (changes != null)
        {
            //Data has changes. 
            //use update method in the adapter. it should update your datasource
            int updatedRows = this.dAdapter.Update(changes);
            this.ds.AcceptChanges();
        }
    }// end function
4

1 回答 1

5

嗯,阿比尔。确实有很多方法可以处理数据,而且在很多时候,开发人员偏爱以一种或另一种方式工作。从你的问题中,我可以看到使用 ADO.NET 可能是新的,所以我建议你阅读一些关于使用 .NET 中的数据(DataTables、DataSets、DataGrids、DataAdapters、Data Binding 等)

我认为(根据我的短暂经验)从数据源读取数据和向数据源写入数据的最简单方法是使用 DataAdapter 将数据读入数据集,然后将数据集设置为用户可以编辑的 gridview 的数据源。要写回更改,只需使用适配器中的更新方法。这是一个示例代码

DataSet ds;
OleDbDataAdapter dataAdapter;
void ReadData()
    {
        this.ds = new DataSet();
        string connString = "CONNICTION STRING GOES HERE";
        this.dataAdapter = new OleDbDataAdapter("QUERY GOES HERE", connString);
        this.dataAdapter.Fill(this.ds, "TABLE1");
        this.ds.AcceptChanges();
        //set the table as the datasource for the grid in order to show that data in the grid
        this.dataGridView1.DataSource = ds.DefaultViewManager;
    }

    void SaveData()
    {
        DataSet changes = this.ds.GetChanges();
        if (changes != null)
        {
            //Data has changes. 
            //use update method in the adapter. it should update your datasource
            int updatedRows = this.dataAdapter.Update(changes);
            this.ds.AcceptChanges();
        }
    }

请参阅以下内容,因为它提供了有关使用 DataGrid 控件的更长示例

http://www.codeproject.com/Articles/9986/Using-the-DataGrid-Control

有关 DataTable、DataSets 和 DataGrids 的一些介绍,请参见

http://www.codeproject.com/Articles/6179/A-Practical-Guide-to-NET-DataTables-DataSets-and-D

于 2013-06-27T21:01:37.063 回答