2012-07-30 4 views
0

내 애플리케이션에 두 개의 데이터 격자가 있습니다 (dataGridView1dataGridView2). 선택한 항목을 dataGridView1에서 dataGridView2으로 옮깁니다. 여기에 내가 현재이 일을하고 어떻게 : 그러나데이터 표를 데이터 표로 복사하지 않고 복사하십시오.

DataTable dt = new DataTable("dt"); 
DataTable id = new DataTable("id"); 
try 
{ 
    if (dataGridView1.Rows.Count > 0) 
    { 
     for (int i = 0; i < dataGridView1.Rows.Count - 0; i++) 
     { 
      if (dataGridView1.Rows[i].Cells[0].Value != null) 
      { 
       DataRow row; 
       DataRow idRow; 
       row = dt.NewRow(); 
       idRow = id.NewRow(); 
       idRow["id"] = dataGridView1.Rows[i].Cells[1].Value.ToString(); 
       row["id"] = dataGridView1.Rows[i].Cells[1].Value.ToString(); 
       row["Link Name"] = dataGridView1.Rows[i].Cells[2].Value.ToString(); 
       dt.Rows.Add(row); 
       id.Rows.Add(idRow); 
      } 
     } 
    dataGridView2.DataSource = dt; 
} 

, 나는 또한 dataGridView2에서 항목을 제거 할 수 있어야합니다. 현재 DataTable dtdataGridView2에 바인딩되어 있으므로 dataGridView2도 지워 항목을 추가 한 후에 지울 수 없습니다.

기본적으로 제 질문은 datagrid.DataSource을 사용하지 않고 데이터 표의 내용을 데이터 표에 추가하는 방법이 있습니까?

답변

1

dataGridView2.Rows.Add 기능을 사용하여 각 행을 수동으로 추가 할 수 있습니다. 또한 일반적으로 행의 데이터의 실제 소스를 보유하기 위해 dataGridView2.Row(n).Tag을 사용했습니다.

int n = dataGridView2.Rows.Add(); 
DataGridViewRow newRow = dataGridView2.Rows[n]; 
newRow.Cells[0].Value = "ABC"; 
newRow.Cells[1].Value = 123; 
newRow.Tag = row; 
관련 문제