如何通过组合框切换不同表格而不添加额外列

huangapple go评论74阅读模式
英文:

How to switch different table throgh the combobox without adding additional column

问题

I have a database named appmondb and I want to use the combobox to switch tables. If I select the ids table first, it is no problem, but if I select the accounts table next, the fields of ids and accounts will appear at the same time.

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    currentdt.Clear();//clear table 
    dataGridView1.DataSource = null;

    MySqlDataAdapter adapter = new MySqlDataAdapter();
    MySqlCommand cmd;
    string sql = "SELECT * FROM " + comboBox1.Text;

    cmd = new MySqlCommand(sql, Connection.connMaster);

    adapter.SelectCommand = cmd;
    adapter.Fill(currentdt);

    dataGridView1.DataSource = currentdt;
    totalPage = calculateTotalPages(currentdt);//update totalPage
}

first I choose ids table

after that, I select accounts table

I have something to say that it should be .fill() or the wording of dataset and datatable

英文:

I have a database named appmondb and I want to use the combobox to switch tables. If I select the ids table first, it is no problem, but if I select the accounts table next, the fields of ids and accounts will appear at the same time.

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
        currentdt.Clear();//clear table 
        dataGridView1.DataSource = null;

        MySqlDataAdapter adapter = new MySqlDataAdapter();
        MySqlCommand cmd;
        string sql = "SELECT * FROM " + comboBox1.Text;

        cmd = new MySqlCommand(sql, Connection.connMaster);

        adapter.SelectCommand = cmd;
        adapter.Fill(currentdt);

        dataGridView1.DataSource = currentdt;
        totalPage = calculateTotalPages(currentdt);//update totalPage
}

first I choose ids table

after that, I select accounts table

I have something to say that it should be .fill() or the wording of dataset and datatable

答案1

得分: 1

Calling Clear on the DataTable removes all the rows but it doesn't remove the columns, so they are still there when you call Fill for the new query. You'll need to either create a new DataTable each time or else Clear the Columns collection as well.

I'm fairly certain that clearing the DataSource is not going to remove any columns from the grid either, so you're going to have to Clear the Columns collection of the grid too. Like so:

dataGridView1.DataSource = null;
dataGridView1.Columns.Clear();
currentdt.Clear();
currentdt.Columns.Clear();
英文:

Calling Clear on the DataTable removes all the rows but it doesn't remove the columns, so they are still there when you call Fill for the new query. You'll need to either create a new DataTable each time or else Clear the Columns collection as well.

I'm fairly certain that clearing the DataSource is not going to remove any columns from the grid either, so you're going to have to Clear the Columns collection of the grid too. Like so:

dataGridView1.DataSource = null;
dataGridView1.Columns.Clear();
currentdt.Clear();
currentdt.Columns.Clear();

huangapple
  • 本文由 发表于 2023年4月13日 15:52:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76002958.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定