开发者

Import DataRow in DataTable

开发者 https://www.devze.com 2023-02-16 12:48 出处:网络
i try to Import selected Rows into a DataTable to send it to a function. I go to Button Clickevent an set following Code:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消