private void ShiftColumnByOneRow(DataGridView dataGridView, int columnIndex)
{
if (dataGridView.Rows.Count > 1 && columnIndex >= 0 && columnIndex < dataGridView.Columns.Count)
{
// Store the value of the last cell in the column
var lastValue = dataGridView.Rows[dataGridView.Rows.Count - 1].Cells[columnIndex].Value;
// Shift each cell value down by one row
for (int i = dataGridView.Rows.Count - 1; i > 0; i--)
{
dataGridView.Rows[i].Cells[columnIndex].Value = dataGridView.Rows[i - 1].Cells[columnIndex].Value;
}
// Set the first cell value to the last cell value
dataGridView.Rows[0].Cells[columnIndex].Value = lastValue;
}
}
//Call this method when needed:
// Example usage
ShiftColumnByOneRow(yourDataGridView, columnIndex);
Я нашел этот код для смещения столбца на одну строку вниз: [code]private void ShiftColumnByOneRow(DataGridView dataGridView, int columnIndex) { if (dataGridView.Rows.Count > 1 && columnIndex >= 0 && columnIndex < dataGridView.Columns.Count) { // Store the value of the last cell in the column var lastValue = dataGridView.Rows[dataGridView.Rows.Count - 1].Cells[columnIndex].Value;
// Shift each cell value down by one row for (int i = dataGridView.Rows.Count - 1; i > 0; i--) { dataGridView.Rows[i].Cells[columnIndex].Value = dataGridView.Rows[i - 1].Cells[columnIndex].Value; }
// Set the first cell value to the last cell value dataGridView.Rows[0].Cells[columnIndex].Value = lastValue; } }
//Call this method when needed:
// Example usage ShiftColumnByOneRow(yourDataGridView, columnIndex); [/code] Как мне его увеличить? Я пробовал: [code]dataGridView.Rows[i-1].Cells[columnIndex].Value = dataGridView.Rows[i].Cells[columnIndex].Value; [/code] При этом все строки в столбце станут пустыми.