谁能告诉我为什么这行代码对我不起作用:
DataTable dt = new DataTable();
dt = (DataTable)this.dataGridView_Clients.DataSource;
我想将 DataSource 对象显式转换为 DataTable,但它给了我这个异常:
An unhandled exception of type 'System.InvalidCastException' occurred in Volkswagen App.exe
Additional information: Unable to cast object of type 'System.Windows.Forms.BindingSource' to type 'System.Data.DataTable'
最佳答案
因为 DataGridView
DataSource
是类型 BindingSource
而不是 DataTable
(在你的例子中)。你可以试试这个:
dt = (DataTable)((BindingSource)this.dataGridView_Clients.DataSource).DataSource;
关于c# - 为什么这行代码不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15178885/