我似乎不太明白这一点。我使用的是VS2013中的数据网格视图。我有一个简单的脚本来拉取表格和更新网格视图。它似乎工作得很好,除了datagridview似乎是附加了列,而不是用新的列替换它们。
我已经环顾了几个小时,尝试了很多治疗方法。也许你能帮上忙?
以下是我到目前为止使用的代码:
public CentralStation(MySqlConnection _myConnection)
{
InitializeComponent();
myConnection = _myConnection;
myAdapter = new MySqlDataAdapter();
myCommand = new MySqlCommand(" ", myConnection);
myDataTable = new DataTable();
myBinder = new BindingSource();
PopulateTableSelection();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
myCommand.CommandText = "SELECT * FROM tcpro." + this.tableMenuList.Text + ";";
myAdapter.SelectCommand = myCommand;
myDataTable.Clear();
myAdapter.Fill(myDataTable);
myBinder.DataSource = null;
myBinder.DataSource = myDataTable;
dataGridView1.DataSource = myBinder;
myAdapter.Update(myDataTable);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}发布于 2015-06-03 04:17:21
myCommand.CommandText = "SELECT * FROM tcpro." + this.tableMenuList.Text + ";";myCommand从不同的数据库表中选择数据,对吗?这些表具有不同的列
myAdapter填充相同的表
myAdapter.Fill(myDataTable);数据适配器不清除行,为此需要myDataTable.Clear();
所以数据适配器不会清除列,您也需要清除它们:
myDataTable.Clear();
myDataTable.Columns.Clear();这不是datagridview故障
https://stackoverflow.com/questions/30604936
复制相似问题