2017-12-07 12 views
0

메신저가 15 개 대신 3 개의 열을 목록 상자에 가져 오려고 할 때 어떻게해야합니까?특정 열을 DataBoxView에서 목록 상자로 가져 오는 방법

이것은 모든 열을 가져 오는 코드입니다.

private void testlist() 
    { 
     string[,] Datavalue = new string[dataGridView1.Rows.Count, dataGridView1.Columns.Count]; 
     foreach (DataGridViewRow row in dataGridView1.Rows) 
     { 
      foreach (DataGridViewColumn col in dataGridView1.Columns) 
      { 
       Datavalue[row.Index, col.Index] = dataGridView1.Rows[row.Index].Cells[col.Index].Value.ToString(); 
      } 
     } 
     int i = 1; 
     string strval = ""; 
     foreach (string ss in Datavalue) 
     { 
      strval += ss + " "; 
      if (i == 15) 
      { 
       listBox1.Items.Add(strval); 
       strval = ""; 
       i = 0; 
      } 
      i++; 
     } 
    } 
+0

내가 2 세를 원하는 15 열 – Luka

답변

0
private void testlist() 
{ 
    string[,] Datavalue = new string[dataGridView1.Rows.Count, 3]; 
    foreach (DataGridViewRow row in dataGridView1.Rows) 
    { 
     Datavalue[row.Index, 0] = 
      dataGridView1.Rows[row.Index].Cells[1].Value.ToString(); 
     Datavalue[row.Index, 1] = 
      dataGridView1.Rows[row.Index].Cells[2].Value.ToString(); 
     Datavalue[row.Index, 2] = 
      dataGridView1.Rows[row.Index].Cells[14].Value.ToString(); 
    } 
    int i = 1; 
    string strval = ""; 
    foreach (string ss in Datavalue) 
    { 
     strval += ss + " "; 
     if (i == 3) 
     { 
      listBox1.Items.Add(strval); 
      strval = ""; 
      i = 0; 
     } 
     i++; 
    } 
} 
+0

내가 예외 '인덱스가 범위를 벗어났습니다이 응답을 주셔서 감사합니다. 음수가 아니어야하며 콜렉션의 크기보다 작아야합니다. 매개 변수 이름 : index ' – Luka

+0

죄송합니다. 초기 답변에서 한 번 오류가 발생했습니다. 인덱스를 업데이트했습니다. 다시 시도 하시겠습니까? – weew

+0

완벽하게 답변 해 주셔서 감사합니다 :) – Luka