0

我尝试了一些代码,但没有。

Me.DataGridView1.Refresh()

为什么?我的 datagridview 自动连接到数据库,而不是手动使用命令进行连接。

4

1 回答 1

0

试试下面的代码......我想你除了下面的代码......

//Text box Change Event

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
                GetData("select * from Sample where num =" + textBox1.Text);
            else
                GetData("select * from Sample");
        }

//Data Bind Event by using BindingSource

        private void GetData(string selectCommand)
    {
        try
        {
            String connectionString ="Your Connection String";
            OleDbDataAdapter dataAdapter = new OleDbDataAdapter(selectCommand, connectionString);
            OleDbCommandBuilder commandBuilder = new OleDbCommandBuilder(dataAdapter);
            DataTable table = new DataTable();
            table.Locale = System.Globalization.CultureInfo.InvariantCulture;
            dataAdapter.Fill(table);
            //BindingSource - Binding
            sampleBindingSource.DataSource= table;
            dataGridView1.AutoResizeColumns( 
                DataGridViewAutoSizeColumnsMode.AllCells );
        }
        catch (SqlException)
        {
            MessageBox.Show("Error Occured");
        }
    }
于 2013-01-23T05:18:55.330 回答