如何刷新数据网格?

时间:2011-10-13 07:24:11

标签: c#

我使用c#作为前端,ms访问作为后端

仅当组合框中的索引发生更改时才显示数据网格

 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        refershGridView(comboBox1.Text);
    }

但是当我对数据网格进行任何更新时,更新仅在我做出选定的索引更改事件后才会反映

我尝试了datagidview1.refresh()并且还隐式调用了我的referhGridView(comboBox1.Text)函数

但我的网格视图仅在我进行选定的索引更改时刷新

referhGridView(comboBox1.Text)的代码

private void refershGridView(string tableName)
    {

        saveBttnSwitch = 0;//for save button swicth 
        //setting back to intial user interface ..



        clearVisibilty();
        clearall();
        button1.Visible = true;
        button3.Visible = false;

        label11.Visible = false;
        try
        {
            OleDbConnection mycon = new OleDbConnection();

            mycon.ConnectionString = ConnString;

            //create the database query
            string query = null;
            if (tableName == "employee")
            {
                query = "SELECT fname,lname,ssn FROM employee";

                dataGridView1.Visible = true;
            }
            if (tableName == "project")
            {


                query = "SELECT pname,pnumber FROM project";

                dataGridView1.Visible = true;

            }



            //create an OleDbDataAdapter to execute the query
            OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, mycon);


            //create a command builder
            OleDbCommandBuilder cBuilder = new OleDbCommandBuilder(dAdapter);

            //create a DataTable to hold the query results
            DataTable dTable = new DataTable();

            //fill the DataTable
            try
            {
                dAdapter.Fill(dTable);
            }

            catch (OleDbException exp)
            {
                label11.Text = "file couldnt be found...kindly check Db file location ";
                label11.Visible = true;
                button1.Visible = false;

            }
            //  DataGridView dgView = new DataGridView();

            //BindingSource to sync DataTable and DataGridView
            BindingSource bSource = new BindingSource();

            //set the BindingSource DataSource
            bSource.DataSource = dTable;

            //set the DataGridView DataSource
            dataGridView1.DataSource = bSource;
            // dataGridView1.Dock = DockStyle.Fill;
            dataGridView1.AutoGenerateColumns = true;

            mycon.Close();
        }
        catch (System.Data.SqlClient.SqlException ex)
        {

            throw new InvalidOperationException("Data could not be read", ex);

        }

        this.button2.Visible = false;

    }

2 个答案:

答案 0 :(得分:0)

refershGridView(comboBox1.Text);是否重新填充DataGrid?

如果是这样,请从您希望重新填充数据的任何其他地方拨打电话。如果即使是组合的SelectedIndexChanged是您填充它的唯一地方,除非您更改选择,否则它不会刷新。

正如@Bryan建议的那样,如果我们看到一些代码就会更好。

答案 1 :(得分:0)

在填充DataTable并设置DataGridView数据源之前,尝试清除dTabledataGridView1.DataSource

dTable = new DataTable();
dataGridView1.DataSource = nothing;