C# 如何刷新数据网格?

C# 如何刷新数据网格?,c#,C#,我使用c作为前端,ms access作为后端 仅当组合框中的索引更改时,我才显示datagrid private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { refershGridView(comboBox1.Text); } 但是,当我对数据网格进行任何更新时,更新仅在我执行选定的索引更改事件后才会反映 我尝试了datagidview1.refresh,还隐式调用了我的r

我使用c作为前端,ms access作为后端

仅当组合框中的索引更改时,我才显示datagrid

 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        refershGridView(comboBox1.Text);
    }
但是,当我对数据网格进行任何更新时,更新仅在我执行选定的索引更改事件后才会反映

我尝试了datagidview1.refresh,还隐式调用了我的refershGridViewcomboBox1.Text函数

但只有在更改选定索引时,我的网格视图才会刷新

refershGridViewcomboBox1.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;

    }
不引用GridViewCombobox1.Text;重新填充数据网格

如果是这样,请从您希望重新填充数据的任何其他地方调用它。如果组合中的SelectedIndexChanged是您填充它的唯一位置,则除非您更改选择,否则它不会刷新

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

在填充数据表和设置DataGridView数据源之前,尝试清除dTable和dataGridView1.DataSource

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

我可以知道您当前在refershGridView方法中的代码吗?如果再次选择相同的索引,它将刷新。我还尝试在我更新值的地方调用refreshGridViewcombo1.text,它在调试模式下进行引用,但在我执行时不进行调试,如果再次选择相同的索引,它将进行刷新。我还尝试在调试模式下更新值的地方调用refreshGridViewcombo1.text,但在没有调试的情况下执行时不调用。因为我没有发现代码中有任何问题,所以我尝试使用一个简单的表单,没有按钮可见性的网格和组合,当然还有其他东西,使用SQL Server。很好。你是说所有的场景在调试模式下都可以正常工作,不是吗?是的,当我调试它的时候它看起来很好,但当我执行它的时候就不行了。兄弟,除了这一点,我对问题没有任何线索,因为它可以正常工作。希望您没有执行任何多线程操作。我在多线程应用程序中看到过这种情况,但没有正确执行。希望你能找到解决办法。