2012-04-23 2 views
1

그래서 데이터베이스의 데이터로 채워지는 datagridview가 있습니다. 이 시점에서 사용자는 데이터 클릭 뷰 내의 하나 이상의 행을 마우스 클릭/키 누름으로 선택했거나 선택하지 않을 수 있습니다.DataGridView 다중 행 선택, 특정 열 데이터 가져 오기

필자는 (선택시) 새로운 데이터 세트, 데이터 테이블을 생성하고 상기 데이터 그릿뷰로부터의 일부 데이터를 갖는 행을 추가 할 필요가있다.

예를 들어 테이블에 이름 만 있으면 안됩니다. I는 추가 처리를 위해 다른 방법으로 전달할 수 있도록 새로운 데이터 세트에 그 이름을 추가 숀 래리 위에 선택을 끌어 사용자 클릭하면

Joe 
Sean 
Larry 
Chris 

.

여기에 내가있는 곳이 있습니다. 내가 하나가 열 선언을 누락되거나 세트에 테이블을 추가 해요 마치

Input array is longer than the number of columns in this table. 

:

'Get index of current row 
    Dim currentMouseRow As New Integer 
    currentMouseRow = dataGridView_UnAssodevices.HitTest(e.X, e.Y).RowIndex 


    'grab cell data of selected rows 
    Dim ds As New DataSet 
    Dim dt As New DataTable 
    For Each row As DataGridViewRow In dataGridView_UnAssodevices.SelectedRows 
     Dim dr As New DataGridViewRow 
     Dim data As New DataGridViewTextBoxCell 
     data.Value = row.Cells(0).Value 
     dr.Cells.Add(data) 
     dt.Rows.Add(dr) 
     MessageBox.Show(data.ToString) 
    Next 





    'Add contextmenu if right clicked 
    If e.Button = MouseButtons.Right Then 
     Dim m As New ContextMenu() 
     If currentMouseRow >= 0 Then 
      dataGridView_UnAssodevices.Rows(currentMouseRow).Selected = True 
      m.MenuItems.Add(New MenuItem("View Full Device Info")) 
      m.MenuItems.Add(New MenuItem("Associate Device(s)")) 
     End If 
     m.Show(dataGridView_UnAssodevices, New Point(e.X, e.Y)) 
    End If 

그러나 나는이 오류가 계속?

답변

0

DataTable에 대한 DataColumn을 만들지 않은 것 같습니다. 보자.

'Get index of current row 
Dim currentMouseRow As New Integer 
currentMouseRow = dataGridView_UnAssodevices.HitTest(e.X, e.Y).RowIndex 


'grab cell data of selected rows 
Dim ds As New DataSet 
Dim dt As New DataTable 

'Create a Data Column for your DataTable; Or you can write a loop to create the datacolumn based on the cell in your DataGridViewRow 
dt.Columns.add("Col1") 

For Each row As DataGridViewRow In dataGridView_UnAssodevices.SelectedRows 
    Dim dr As New DataGridViewRow 
    Dim data As New DataGridViewTextBoxCell 
    data.Value = row.Cells(0).Value 
    dr.Cells.Add(data) 
    dt.Rows.Add(dr) 
    MessageBox.Show(data.ToString) 
Next 
+0

내가 간과했다고 생각할 수 없다, 고마워! – Ealianis