Monday, April 23, 2012

DataGridViewRow to DataRow - object reference not set to an instance

What I'm doing (or trying to do) is getting a row from DataGridView and convert it to DataRow, so I can put that DataRow into DataTable. So I searched for examples on the Web,
and people claimed they work just fine, but my doesn't.
This is my code:



DataGridViewRowCollection rows = myDataGridView.Rows;
foreach (DataGridViewRow row in rows)
{
/*(1)*/ DataRow myRow = (row.DataBoundItem as DataRowView).Row;
myDataTable.Rows.Add(myRow);
}


but I get "Object reference not set to an instance of an object." on line (1).





No comments:

Post a Comment