I have a datagridview in which I want to copy some rows to a collection by a button event.
Here arises two questions.
-
If I click outside the grid, it shouldn’t fire it because it will throw an exception.
How to prevent it? -
Th second one seems to be simple? It is an empty datarow, then it should be prohibtted as well.
private void btnAdd_Click(object sender, EventArgs e) { try { if (// need condition here.) { DataRowView currentDataRowView = (DataRowView)DataGridView1.CurrentRow.DataBoundItem; DataRow row = currentDataRowView.Row; DataRowlist.Add(row); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
Thanks.
Simply check if there is a currentrow:
if (DataGridView1.CurrentRow != null)
{
…
}