2012-04-24 65 views
-1
int rowsCount = 0; 
//This checks to see that both textbox for items and subitems do not gain focus at the same time 
if (textBoxSubItems.Text != string.Empty) 
    txtItems.Enabled = false; 
else 
    txtItems.Enabled = true; 
if (comboBoxItems.SelectedItem != null) 
{ 

    int idx = dataGridViewTimesheet.Rows.Add(); 
    DataGridViewRow row = dataGridViewTimesheet.Rows[idx]; 
    row.Cells["items"].Value = comboBoxItems.SelectedItem.ToString() + "-" + textBoxSubItems.Text; 
    row.Cells["fromTime"].Value = DateTime.Now.ToLongTimeString(); 
    row.Cells["toTime"].Value = null; 
    row.Cells["duration"].Value = null; 
    row.Cells["subTotal"].Value = null; 
    // row.Cells["comments"].Value = "1"; 
} 
else 
    MessageBox.Show("Please select an item"); 

string strGetColumnValue; 
if (dataGridViewTimesheet.Rows.Count != 0) 
    rowsCount = dataGridViewTimesheet.Rows.Count; 
else 
    MessageBox.Show("No row in the datagridview"); 
while (dataGridViewTimesheet.Rows.Count > 0) 
{ 
    try 
    { 
     if (dataGridViewTimesheet.CurrentRow != null) 
      for (int counter = 0; counter < dataGridViewTimesheet.Columns.Count; counter++) 
      { 
       if (dataGridViewTimesheet.Columns[counter].Index == 3) 
       { 
        strGetColumnValue = dataGridViewTimesheet.Rows[rowsCount].Cells[counter].Value.ToString(); 
        dataGridViewTimesheet.Rows[rowsCount - 1].Cells[3].Value = strGetColumnValue; 
       } 
      } 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.ToString()); 
    } 
} 

請問我在datagridview中有6列,這些行是動態添加的。我想要的是,當datagridview中的行多於一行時,它應該將當前行(創建的最後一行)的第二列的值分配給上一行的第三列。我如何做到這一點。將單元格中的值分配給另一個

+0

什麼是移動所有值下降的目的是什麼?這並不常見,可能是邏輯不正確的結果 – Matthew 2012-04-24 09:52:30

回答

0

嘗試這種事情

int count =1; 
    foreach (DataGridRow row in dataGridViewTimesheet.Rows) 
    { 
    if (count % 2 == 0) 
    { 
     string secondColumn = dataGridViewTimesheet.Rows[count -1].Cells[1].ToString(); 
     dataGridViewTimesheet.Rows[count].Cells[2].Value = secondColumn; 
    } 
    count++; 
    } 
相關問題