c# Winform exclude a character when pasting in Datagridview

  • Thread starter Thread starter Booney440
  • Start date Start date
B

Booney440

Guest
I am pasting data in a datagridview and one of the cells has a % sign in column [17]. I need to remove the % sign at the end when pasting, I am pasting a few hundred rows. I have looked for some time and have not found a answer. My paste code below or should I handle it in another area?

1604706.jpg

private void btn_Paste_Click(object sender, EventArgs e)
{
//if user clicked Shift+Ins or Ctrl+V (paste from clipboard)

// if ((e.Shift && e.KeyCode == Keys.Insert) || (e.Control && e.KeyCode == Keys.V))

{

char[] rowSplitter = { '\r', '\n' };

char[] columnSplitter = { '\t' };

//get the text from clipboard

IDataObject dataInClipboard = Clipboard.GetDataObject();

string stringInClipboard = (string)dataInClipboard.GetData(DataFormats.Text);

//split it into lines

string[] rowsInClipboard = stringInClipboard.Split(rowSplitter, StringSplitOptions.RemoveEmptyEntries);

//get the row and column of selected cell in grid

int r = dataGridView1.SelectedCells[0].RowIndex;

int c = dataGridView1.SelectedCells[0].ColumnIndex;

//add rows into grid to fit clipboard lines

if (dataGridView1.Rows.Count < (r + rowsInClipboard.Length))

{

dataGridView1.Rows.Add(r + rowsInClipboard.Length - dataGridView1.Rows.Count);

}

// loop through the lines, split them into cells and place the values in the corresponding cell.

for (int iRow = 0; iRow < rowsInClipboard.Length; iRow++)

{

//split row into cell values

string[] valuesInRow = rowsInClipboard[iRow].Split(columnSplitter);

//cycle through cell values

for (int iCol = 0; iCol < valuesInRow.Length; iCol++)

{

//assign cell value, only if it within columns of the grid

{
dataGridView1.Rows[r + iRow].Cells[c + iCol].Value = valuesInRow[iCol];


for (int i = 0; i < dataGridView1.RowCount; i++) ;
}
}
}
}
}








Booney440

Continue reading...
 
Back
Top