2012-08-15 85 views
0

我想從第一個datagridview複製所有行到第二個,但我不想從第一個datagridview的空行,我需要重新格式化第二個datagridview的佈局。這使我瘋狂 - 需要幫助。選擇所有datagridview行到另一個gridview

private void btnConvert_Click(object sender, EventArgs e) 
    { 
    dgvData.SelectAll(); 

    DataTable dt = new DataTable(); 
     DataColumn dc; 
     DataRow dr; 

     //build cols 
     dc = new DataColumn(); 
     dc.DataType = System.Type.GetType("System.String"); 
     dc.ColumnName = "DS"; 
     dc.Unique = false; 
     dt.Columns.Add(dc); 

     dc = new DataColumn(); 
     dc.DataType = System.Type.GetType("System.String"); 
     dc.ColumnName = "GLAccount"; 
     dc.Unique = false; 
     dt.Columns.Add(dc); 

     dc = new DataColumn(); 
     dc.DataType = System.Type.GetType("System.String"); 
     dc.ColumnName = "SubAccount"; 
     dc.Unique = false; 
     dt.Columns.Add(dc); 

     dc = new DataColumn(); 
     //dc.DataType = System.Type.GetType("System.DateTime"); 
     dc.DataType = System.Type.GetType("System.String"); 
     dc.ColumnName = "Date"; 
     dc.Unique = false; 
     dt.Columns.Add(dc); 

     dc = new DataColumn(); 
     dc.DataType = System.Type.GetType("System.String"); 
     dc.ColumnName = "Description"; 
     dc.Unique = false; 
     dt.Columns.Add(dc); 

     dc = new DataColumn(); 
     dc.DataType = System.Type.GetType("System.Decimal"); 
     dc.ColumnName = "Amount"; 
     dc.Unique = false; 
     dt.Columns.Add(dc); 

     //build rows 
     foreach (DataGridViewRow row in this.dgvData.SelectedRows) 
     { 
      //how do i prevent copying over the empty row which happens to be the last row in the first datagridview? 

     int i; 
     i = dgvData.SelectedCells[0].RowIndex; //should this start with the first row? 
     int month = 12; 
     DateTime y = DateTime.Now; 
     int year = y.Year; 

     for (int ir = 1; ir <= month; ir++) 
     { 
      dr = dt.NewRow(); 
      dr["DS"] = dgvData.Rows[i].Cells[0].Value.ToString(); 
      dr["GLAccount"] = dgvData.Rows[i].Cells[1].Value.ToString(); 
      dr["SubAccount"] = dgvData.Rows[i].Cells[2].Value.ToString(); 

      if (ir == 7) 
      { 
       dr["Date"] = "1/01/" + (year + 1).ToString(); 
      } 
      else if (ir == 8) 
      { 
       dr["Date"] = "2/01/" + (year + 1).ToString(); 
      } 
      else if (ir == 9) 
      { 
       dr["Date"] = "3/01/" + (year + 1).ToString(); 
      } 
      else if (ir == 10) 
      { 
       dr["Date"] = "4/01/" + (year + 1).ToString(); 
      } 
      else if (ir == 11) 
      { 
       dr["Date"] = "5/01/" + (year + 1).ToString(); 
      } 
      else if (ir == 12) 
      { 
       dr["Date"] = "6/01/" + (year + 1).ToString(); 
      } 
      else if (ir == 1) 
      { 
       dr["Date"] = "7/01/" + year.ToString(); 
      } 
      else if (ir == 2) 
      { 
       dr["Date"] = "8/01/" + year.ToString(); 
      } 
      else if (ir == 3) 
      { 
       dr["Date"] = "9/01/" + year.ToString(); 
      } 
      else if (ir == 4) 
      { 
       dr["Date"] = "10/01/" + year.ToString(); 
      } 
      else if (ir == 5) 
      { 
       dr["Date"] = "11/01/" + year.ToString(); 
      } 
      else 
      { 
       dr["Date"] = "12/01/" + year.ToString(); 
      } 

      dr["Description"] = dgvData.Rows[i].Cells[3].Value.ToString(); 

      if (ir == 7) 
      { 
       if (dgvData.Rows[i].Cells[10].Value == null || dgvData.Rows[i].Cells[10].Value == "" || dgvData.Rows[i].Cells[10].Value == DBNull.Value) 
       { 
        dr["Amount"] = Convert.ToDecimal(0); 
       } 
       else 
       { 
        dr["Amount"] = Convert.ToDecimal(dgvData.Rows[i].Cells[10].Value); 
       } 
      } 
      else if (ir == 8) 
      { 
       if (dgvData.Rows[i].Cells[11].Value == null || dgvData.Rows[i].Cells[11].Value == "" || dgvData.Rows[i].Cells[11].Value == DBNull.Value) 
       { 
        dr["Amount"] = Convert.ToDecimal(0); 
       } 
       else 
       { 
        dr["Amount"] = Convert.ToDecimal(dgvData.Rows[i].Cells[11].Value); 
       } 
      } 
      else if (ir == 9) 
      { 
       if (dgvData.Rows[i].Cells[12].Value == null || dgvData.Rows[i].Cells[12].Value == "" || dgvData.Rows[i].Cells[12].Value == DBNull.Value) 
       { 
        dr["Amount"] = Convert.ToDecimal(0); 
       } 
       else 
       { 
        dr["Amount"] = Convert.ToDecimal(dgvData.Rows[i].Cells[12].Value); 
       } 
      } 
      else if (ir == 10) 
      { 
       if (dgvData.Rows[i].Cells[13].Value == null || dgvData.Rows[i].Cells[13].Value == "" || dgvData.Rows[i].Cells[13].Value == DBNull.Value) 
       { 
        dr["Amount"] = Convert.ToDecimal(0); 
       } 
       else 
       { 
        dr["Amount"] = Convert.ToDecimal(dgvData.Rows[i].Cells[13].Value); 
       } 
      } 
      else if (ir == 11) 
      { 
       if (dgvData.Rows[i].Cells[14].Value == null || dgvData.Rows[i].Cells[14].Value == "" || dgvData.Rows[i].Cells[14].Value == DBNull.Value) 
       { 
        dr["Amount"] = Convert.ToDecimal(0); 
       } 
       else 
       { 
        dr["Amount"] = Convert.ToDecimal(dgvData.Rows[i].Cells[14].Value); 
       } 
      } 
      else if (ir == 12) 
      { 
       if (dgvData.Rows[i].Cells[15].Value == null || dgvData.Rows[i].Cells[15].Value == "" || dgvData.Rows[i].Cells[15].Value == DBNull.Value) 
       { 
        dr["Amount"] = Convert.ToDecimal(0); 
       } 
       else 
       { 
        dr["Amount"] = Convert.ToDecimal(dgvData.Rows[i].Cells[15].Value); 
       } 
      } 
      else if (ir == 1) 
      { 
       if (dgvData.Rows[i].Cells[4].Value == null || dgvData.Rows[i].Cells[4].Value == "" || dgvData.Rows[i].Cells[4].Value == DBNull.Value) 
       { 
        dr["Amount"] = Convert.ToDecimal(0); 
       } 
       else 
       { 
        dr["Amount"] = Convert.ToDecimal(dgvData.Rows[i].Cells[4].Value); 
       } 
      } 
      else if (ir == 2) 
      { 
       if (dgvData.Rows[i].Cells[5].Value == null || dgvData.Rows[i].Cells[5].Value == "" || dgvData.Rows[i].Cells[5].Value == DBNull.Value) 
       { 
        dr["Amount"] = Convert.ToDecimal(0); 
       } 
       else 
       { 
        dr["Amount"] = Convert.ToDecimal(dgvData.Rows[i].Cells[5].Value); 
       } 
      } 
      else if (ir == 3) 
      { 
       if (dgvData.Rows[i].Cells[6].Value == null || dgvData.Rows[i].Cells[6].Value == "" || dgvData.Rows[i].Cells[6].Value == DBNull.Value) 
       { 
        dr["Amount"] = Convert.ToDecimal(0); 
       } 
       else 
       { 
        dr["Amount"] = Convert.ToDecimal(dgvData.Rows[i].Cells[6].Value); 
       } 
      } 
      else if (ir == 4) 
      { 
       if (dgvData.Rows[i].Cells[7].Value == null || dgvData.Rows[i].Cells[7].Value == "" || dgvData.Rows[i].Cells[7].Value == DBNull.Value) 
       { 
        dr["Amount"] = Convert.ToDecimal(0); 
       } 
       else 
       { 
        dr["Amount"] = Convert.ToDecimal(dgvData.Rows[i].Cells[7].Value); 
       } 
      } 
      else if (ir == 5) 
      { 
       if (dgvData.Rows[i].Cells[8].Value == null || dgvData.Rows[i].Cells[8].Value == "" || dgvData.Rows[i].Cells[8].Value == DBNull.Value) 
       { 
        dr["Amount"] = Convert.ToDecimal(0); 
       } 
       else 
       { 
        dr["Amount"] = Convert.ToDecimal(dgvData.Rows[i].Cells[8].Value); 
       } 
      } 
      else 
      { 
       if (dgvData.Rows[i].Cells[9].Value == null || dgvData.Rows[i].Cells[9].Value == "" || dgvData.Rows[i].Cells[9].Value == DBNull.Value) 
       { 
        dr["Amount"] = Convert.ToDecimal(0); 
       } 
       else 
       { 
        dr["Amount"] = Convert.ToDecimal(dgvData.Rows[i].Cells[9].Value); 
       } 
      } 

      dt.Rows.Add(dr); 
     } 
     } 

     //bind 
     dgvConverted.DataSource = dt; 
    } 
+1

Excel如何與此相關? – 2012-08-15 14:10:07

回答

1

使用row.IsNewRowforeach循環的開頭進行檢查,看是否被複制的行是空白行。

添加的代碼示例

foreach (DataGridViewRow row in this.dgvData.SelectedRows) 
    { 
    if (!row.IsNewRow) 
    { 
     //Your other code here 
    } 
    } 

我不知道爲什麼你使用的是i迭代,因爲你可以簡單地使用row.Cells[]代替dgvData.Rows[i].Cells[]

+0

excel - 我首先將數據從excel導入第一個datagridvew,然後試圖將該數據複製到下一個datagridview。我想出瞭如何刪除第一個datagridview中的空行 - DataBindingComplete - 刪除了最後一行。 – kx250f 2012-08-15 15:02:03

+0

我該如何使用row.IsNewRow? – kx250f 2012-08-15 15:02:44

+0

所以我試圖row.IsNewRow,現在沒有被複制到第二個datagridview。 – kx250f 2012-08-15 15:10:02

0

問題是您使用的是foreach循環然後獲取行索引錯誤。

只需使用for循環並具有適當的索引。

+0

那是怎麼回事?將此行更改爲for循環? – kx250f 2012-08-15 15:56:08

+0

foreach(在this.dgvData.SelectedRows DataGridViewRow行) - 我不知道你的意思 – kx250f 2012-08-15 15:57:41

+0

如果我做以下我總是得到第一個dgv的最後一行。 – kx250f 2012-08-15 16:13:39

相關問題