标签:datagridview ar os sp for on art bs amp
private void StepArtDgv_KeyUp(object sender, KeyEventArgs e)
{
//粘贴
if (e.KeyCode == Keys.V && e.Control)
{
// 获取剪切板的内容,并按行分割
string pasteText = Clipboard.GetText().Replace("\r\n", "@").TrimEnd(‘@‘);
//选中区域是区块后,
int xzRowCount_Z = StepArtDgv.SelectedCells[StepArtDgv.SelectedCells.Count - 1].RowIndex;//选中左上角的单元格的行索引
int xzColCount_Z = StepArtDgv.SelectedCells[StepArtDgv.SelectedCells.Count - 1].ColumnIndex;//选中左上角的单元格的列索引
int xzRowCount_Y = StepArtDgv.CurrentCell.RowIndex;//选中右下角的单元格的列索引
int xzColCount_Y = StepArtDgv.CurrentCell.ColumnIndex;//选中右下角的单元格的行索引
int Colcounts = xzColCount_Y + 1 - StepArtDgv.SelectedCells[StepArtDgv.SelectedCells.Count - 1].ColumnIndex;//粘贴区域的列数
int Rowcounts = xzRowCount_Y + 1 - StepArtDgv.SelectedCells[StepArtDgv.SelectedCells.Count - 1].RowIndex;//粘贴区域的行数
int MaxRowIndex = StepArtDgv.Rows.Count - 1; //获取做大的行数,最下面的行索引
int MaxColIndex = StepArtDgv.ColumnCount - 1; //获取做大的列数,最右面的行索引
//临时变量,索引随时都有可能在曾东增加,然而增加后无法还原,所以采用临时变量存储
int tempRowIndexs = xzRowCount_Z;
int tempColumnIndexs = xzColCount_Z;
if (xzColCount_Z == 0)
{
MessageBox.Show("项目名称不能粘贴!");
return;
}
string[] Row = null;
Row = pasteText.Split(‘@‘);//分割的行数数组
string[] col = null;//分割行后的列,每行的内容
col = Row[0].Replace("\t", "#").TrimEnd(‘#‘).Split(‘#‘);//专门存储列数
//判断是否存在非数字类型
for (int t = 0; t < Row.Length; t++)
{
col = Row[t].Replace("\t", "#").TrimEnd(‘#‘).Split(‘#‘);
//for (int t1 = 0; t1 < col.Length; t1++)
//{
////判断字符串
//if (!reg.IsMatch(col[t1].ToString().Trim()))
//{
// MessageBox.Show("粘贴的内容存在非数字类型!");
// return;
//}
//}
}
if (Row.Length > 0)
{
//粘贴区块,列行都是选中的倍数
if (Rowcounts % Row.Length == 0 && Colcounts % col.Length == 0)
{
for (int cs = 0; cs < Colcounts / col.Length; cs++)
{
for (int rs = 0; rs < Rowcounts / Row.Length; rs++)
{
for (int r = 0; r < Row.Length; r++)
{
if (xzRowCount_Z > MaxRowIndex) break;//跳出行
//获取每行中的内容
string rowMessage = Row[r].Replace("\t", "#").TrimEnd(‘#‘);
col = rowMessage.Split(‘#‘);
for (int c = 0; c < col.Length; c++)
{
if (xzColCount_Z > MaxColIndex) break;//粘贴内容出了DGV的区域,跳出该循环,进行下一行
StepArtDgv.Rows[xzRowCount_Z].Cells[xzColCount_Z].Value = col[c];
xzColCount_Z++;
}
xzRowCount_Z++;
xzColCount_Z = tempColumnIndexs;//因为列自增了,需要还原,进行下一行填充
}
}
xzRowCount_Z = tempRowIndexs;//行数从头开始
xzColCount_Z += col.Length;//列增加
tempColumnIndexs += col.Length;//列的临时变量也要跟着变
}
}//粘贴区块,行是选中的行的倍数
else if (Rowcounts % Row.Length == 0)
{
for (int rs = 0; rs < Rowcounts / Row.Length; rs++)
{
for (int r = 0; r < Row.Length; r++)
{
if (xzRowCount_Z > MaxRowIndex) break;//跳出行
//获取每行中的内容
string rowMessage = Row[r].Replace("\t", "#").TrimEnd(‘#‘);
col = rowMessage.Split(‘#‘);
for (int c = 0; c < col.Length; c++)
{
if (xzColCount_Z > MaxColIndex) break;//粘贴内容出了DGV的区域,跳出该循环,进行下一行
StepArtDgv.Rows[xzRowCount_Z].Cells[xzColCount_Z].Value = col[c];
xzColCount_Z++;
}
xzRowCount_Z++;
xzColCount_Z = tempColumnIndexs;//因为列自增了,需要还原,进行下一行填充
}
xzColCount_Z = tempColumnIndexs;//因为列自增了,需要还原,进行下一行填充
}
}//粘贴区块,列是选中的列的倍数
else if (Colcounts % col.Length == 0)
{
for (int cs = 0; cs < Colcounts / col.Length; cs++)
{
for (int r = 0; r < Row.Length; r++)
{
if (xzRowCount_Z > MaxRowIndex) break;//跳出行
//获取每行中的内容
string rowMessage = Row[r].Replace("\t", "#").TrimEnd(‘#‘);
col = rowMessage.Split(‘#‘);
for (int c = 0; c < col.Length; c++)
{
if (xzColCount_Z > MaxColIndex) break;//粘贴内容出了DGV的区域,跳出该循环,进行下一行
StepArtDgv.Rows[xzRowCount_Z].Cells[xzColCount_Z].Value = col[c];
xzColCount_Z++;
}
xzColCount_Z = tempColumnIndexs;
xzRowCount_Z++;//行数从头开始
}
xzRowCount_Z = tempRowIndexs;//行数从头开始
xzColCount_Z += col.Length;//列增加
tempColumnIndexs += col.Length;//列的临时变量也要跟着变
}
} //粘贴区块小于选中区块(粘贴区块只是一个单元格 || 行和列小于粘贴区块 || 不是行或者列的倍数)
else if (Rowcounts <= Row.Length && Colcounts <= col.Length || Rowcounts % Row.Length != 0 || Colcounts % col.Length != 0)
{
for (int r = 0; r < Row.Length; r++)//循环行
{
if (xzRowCount_Z > MaxRowIndex) break;//跳出行
//获取每行中的内容
string rowMessage = Row[r].Replace("\t", "#").TrimEnd(‘#‘);
col = rowMessage.Split(‘#‘);
//为每个单元格赋值
for (int c = 0; c < col.Length; c++)
{
if (xzColCount_Z > MaxColIndex) break;//粘贴内容出了DGV的区域,跳出该循环,进行下一行
StepArtDgv.Rows[xzRowCount_Z].Cells[xzColCount_Z].Value = col[c];
xzColCount_Z++;
}
xzRowCount_Z++;//下一行
xzColCount_Z = tempColumnIndexs;//因为列自增了,需要还原,进行下一行填充
}
}
}
}
}
标签:datagridview ar os sp for on art bs amp
原文地址:http://www.cnblogs.com/FLWL/p/4155879.html