나는 datagridview와 세 개의 텍스트 상자가 있습니다. 그리드 이벤트 SelectionChanged
가 발생 하면 그리드 에서 데이터를 가져와 속성에 씁니다. 값이있는 열을 클릭하지 않으면 문제가 발생합니다.
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");
}
이 문제를 해결하는 방법 :
스크린 샷의 텍스트 : 'System.Windows.Forms.DataGridView'유형의 개체를 'System.Windows.Forms.DataRowView'유형으로 캐스팅 할 수 없습니다.
is
운영자 참조 : https://msdn.microsoft.com/en-us/library/scekt9xw.aspx
DataRowView인지 확인하십시오.
if(BSource.Current is DataRowView)
{
//...do your cast here
또는 as
연산자를 사용할 수 있습니다 .
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다