-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中的行不止一个时,它应该将当前(创建的最后一行)行上第二列的值分配给前一行的第三列。我如何做到这一点。

4

1 回答 1

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++;
  }
于 2012-04-24T10:56:32.753 回答