Reputation: 5250
I have datagridview and three textbox. When grid event SelectionChanged
is caused i get data from grid and write in properties. The problem arises when you do not click on the column has the value.
private void _artistGridSelectionChanged(object sender, EventArgs e)
{
int rowIndex = ArtistListGrid.CurrentCell.RowIndex;
int cellIndex = ArtistListGrid.CurrentCell.ColumnIndex;
this.ID = ((DataRowView)BSource.Current).Row.Field<Int32>("ID");
this.Name = ((DataRowView)BSource.Current).Row.Field<string>("Firstname");
this.Lastname = ((DataRowView)BSource.Current).Row.Field<string>("Lastname");
this.Nickname = ((DataRowView)BSource.Current).Row.Field<string>("Nickname");
}
How to fix this:
Text from the screenshot: Unable to cast object of type 'System.Windows.Forms.DataGridView' to type 'System.Windows.Forms.DataRowView'.
Upvotes: 0
Views: 3046
Reputation: 8180
see the is
operator: https://msdn.microsoft.com/en-us/library/scekt9xw.aspx
Check if it is a DataRowView:
if(BSource.Current is DataRowView)
{
//...do your cast here
Alternatively, you may use the as
operator:
https://msdn.microsoft.com/en-us/library/cscsdfbt.aspx
Upvotes: 1