Import DataRow in DataTable
i try to Import selected Rows into a DataTable to send it to a function. I go to Button Clickevent an set following Code:
var dt = new DataTable();
foreach (var DataRow in dataGridView1.SelectedRow开发者_开发百科s)
dt.ImportRow(DataRow);
But there is a error, cannot convert from object to DataRow. Any suggestion to solve this problem?
You can't use ImportRow
because it requires a DataRow
. DataGridView.SelectedRows
will return a collection of DataGridViewRow
.
A DataGridViewRow
is not a DataRow
. However, you can use the DataBoundItem
property of DataGridViewRow
in order to get the actual DataRow
:
foreach (DataGridViewRow dataRow in dataGridView1.SelectedRows)
{
DataRow dataBoundItem = ((DataRowView)dataRow.DataBoundItem).Row;
dt.ImportRow(dataBoundItem);
}
DataTable.ImportRow takes a DataRow. Your datagridview1 however contains DataGridViewRows. You'll need to find a way to convert (example) between the two, or construct new DataRow's from your DataGridViewRow's data.
精彩评论