2009-05-21 10 views

답변

7

내가 BindingSource에 OA/승 그것을 할 방법을 잘 모르겠어요, 여기 하나의 방법으로 할 수 있습니다 :

var drv = bindingSoure1.Current as DataRowView; 
if (drv != null) 
    var row = drv.Row as MyRowType; 
1

당신은 직접 강력한 형식의 행으로 선택한 행을 캐스팅 할 수 있어야 DataGridView에 바인딩되었습니다.

23
DataRowView currentDataRowView = (DataRowView)dgv1.CurrentRow.DataBoundItem 
DataRow row = currentDataRowView.Row 
+0

흠 ... 선택한 행에는«DataBoundItem»이 null입니다. –

+2

@ Hi-Angel 데이터 바인딩을 사용하고 있지 않습니다. – stuartd

3

은 다음 특성을 얻기가 가능하다 : DataGridViewSelectedRowCollection :

this.dataGridView.SelectedRows 

한 타입의 집합을 구한다. 여기에는 DataGridViewRow 유형의 항목이 포함됩니다.

그런 사람은 다음과 같은 방법으로 그들 자신의 유형 bounditem 얻을 수 있습니다 :

DataGridViewSelectedRowCollection list = this.dataGridViewInventoryRecords.SelectedRows; 
MyType selectedItem = (MyType)list[0].DataBoundItem; //[0] ---> first item 
0

는이 작업을 시도합니다. 당신이 데이터베이스에 테이블이나보기로 DataGridView에 바인딩 한 경우 else return;

0

XtraMessageBox.Show(row["ID"].ToString()); } if (row != null) {

DataRow row = gridView1.GetDataRow(gridView1.FocusedRowHandle);

, 당신은 강력한 형식의 개체로에서 데이터를 얻을 수 있습니다.

이 대답은 디자인 타임에 DataSet을 사용하여 데이터베이스에 연결된 Windows 양식에 대한 것입니다. 예제 이름은 DataSet1이고 예제 테이블 이름은 Customer_Info입니다.

// cast the data bound item to DataRowView so you have 
// access to "Row", which 
// has the actual data for the row in typed fields. 
DataRowView drv = dgv.SelectedRows(0).DataBoundItem as DataRowView; 
// run the code and look at the debugger value of drv.Row -- 
// the type will be shown 
// which is the type created by the data binding, representing 
// your table or view 
//{YourDataSetName.YourTableOrViewType} tmpTableData = drv.Row as {YourDataSetName.YourTableOrViewType}; 
DataSet1.Customer_InfoRow tmpTableData = drv.Row as DataSet1.Customer_InfoRow; 

이 링크는 답변입니다. 내가 명쾌함과 위의 예를 더했으면 좋겠다. https://social.msdn.microsoft.com/Forums/windows/en-US/f252e395-58e6-4703-ba7b-0740efcbecf3/can-i-convert-the-selected-row-in-a-bound-datagridview-to-a-typed-datarow?forum=winformsdatacontrols

이 링크는 프로그래밍 방식으로 데이터 소스에 추가 된 데이터를 보여 주며 기존 데이터베이스에서 데이터를 가져 오지 않습니다. 이것은 대답의 일부분이되었습니다 : https://msdn.microsoft.com/en-us/library/4wszzzc7(v=vs.110).aspx

0

데이터 세트 내에서 tableadapter가 사용되지 않는 경우 datagridview에 강력하게 형식화 된 액세스를 수행 할 수 없습니다.

는 DataGridView를이 BindingSource에 통해 데이터 테이블에 바인딩 할 때, 강하게 변수를 입력 할 수있는 액세스 권한이 다음과 같이 수행합니다

가 새 프로젝트를 만듭니다.

DataSet1이라는 이름의 DataSet과 dt99라는 이름의 DataTable을 문자열 유형 인 DataColumn1 및 DataColumn2라는 열로 삽입하십시오.

enter image description here

기본 폼에 DataGridView를 추가하고

enter image description here

그래서 dt99BindingSource가 된 DataGridView와 데이터 테이블

enter image description here

를 연결하는 dt99에 바인딩 선택 변경에 대한 추가 및 이벤트 핸들러 DataGridView를, 그리고 코드

개인 무효 dataGridView1_SelectionChanged (개체 보낸 사람, EventArgs입니다 전자) {

ds1.dt99Row d= ((ds1.dt99Row)((DataRowView)dt99BindingSource.Current).Row); 

    Debug.WriteLine(d.DataColumn1 + " " + d.DataColumn2); 

}

지금 당신이 강력하게 입력 한 변수 (d.DataColumn1와 D의 다음 조각을 삽입 .DataColumn2)를 사용하여 DataGridview에서 선택된 셀에 액세스하십시오.

또 다른 흥미로운 기능은 데이터 세트에 삽입 된 데이터 테이블이 많은 도움이되는 공용 클래스 세트를 제공한다는 것입니다 데이터 테이블을 처리 할 때, 예를 들어

 private void Form1_Load(Object sender, EventArgs e) 
    { 
     ds1.dt99.Adddt99Row("K", "B"); 
     ds1.dt99.Adddt99Row("L", "D"); 
     ds1.dt99.Adddt99Row("M", "F"); 
     ds1.dt99.Adddt99Row("N", "H"); 

     ds1.dt99Row dr = ds1.dt99.Newdt99Row(); 
     dr.DataColumn1 = "X"; 
     dr.DataColumn2 = "Y"; 
     ds1.dt99.Adddt99Row(dr); 

    } 
관련 문제