I created a Context Menu for a DataGridView is am using to manipulate data from various non-conforming Tab-delimited files. (Old financial stuff)
One of the functions I needed was the ability to move all columns to the right.
I tried to understand .Insert, .Add, and .Clone to no avail as the sharers are so far above me as coders.
Please take a look at this and give e some ideas for improvement because with long files the pause is traumatic.
private void dgv_MoveColumnRight(DataGridView dgv, int cIndex)
{
dgv.ColumnCount = dgv.ColumnCount + 1;
int cCount = dgv.ColumnCount - 2;
if (dgv.RowCount <= 1) { return; }
while (cCount >= cIndex)
{
dgv_CopyColumn(dgv, cCount);//showMe.Comment = "From: " + n1.ToString();
dgv_PasteColumn(dgv, cCount + 1);
//dgv.Rows[1].Cells[cCount].Value = "*";
cCount--;
Application.DoEvents();
}
for (int n = 0; n < dgv.RowCount; n++)
{
dgv.Rows[n].Cells[cIndex].Value = " ";
}
}
List<string> colData = new List<string>();
private void dgv_CopyColumn(DataGridView dgv, int cIndex)
{
colData.Clear();
for (int n = 0; n < dgvStatement.RowCount; n++)
{
if(dgv.Rows[n].Cells[cIndex].Value != null)
{
colData.Add(dgv.Rows[n].Cells[cIndex].Value.ToString());
}
else
{
colData.Add(" ");
}
showMe.Comment = n.ToString() + "t" + colData[n];
}
}
private void dgv_PasteColumn(DataGridView dgv, int cIndex)
{
if (colData.Count == 0) { return; }
for (int n = 0; n < dgvStatement.RowCount; n++)
{
dgv.Rows[n].Cells[cIndex].Value = colData[n];
}
}
The final code will pass the list instead of having it exposed.
Thanks