功能函数:

CopyFromGrid

PasteToGrid

PasteNewRowsToGrid

 

private void mnuPaste_Click(object sender, EventArgs e)
{
this.Cursor = Cursors.WaitCursor;
//if(bandedGridView1.GetSelectedRows)
int[] iSelRowIndexs=bandedGridView1.GetSelectedRows();
if(iSelRowIndexs==null || iSelRowIndexs.Length==0)
{
Common.DisplayMsg(this.Text,"先请选择需要范围");
}else
{
string sFieldName = bandedGridView1.FocusedColumn.FieldName;
if(sFieldName !=null)
{
if (sFieldName == "Discount" || sFieldName == "ProjectRating")
PasteToGrid(bandedGridView1, iSelRowIndexs[0], iSelRowIndexs.Length, sFieldName);
}
}
this.Cursor = Cursors.Default;
}

private void CopyFromGrid(DevExpress.XtraGrid.Views.BandedGrid.BandedGridView bandedGrid)
{
int[] iSelRowIndexs = bandedGridView1.GetSelectedRows();
if(iSelRowIndexs ==null || iSelRowIndexs.Length==0)
{
Common.DisplayMsg(this.Text,"请先选择需要复制的内容");
return;
}

DevExpress.XtraGrid.Columns.GridColumn[] gcs = null;

Clipboard.Clear();
StringBuilder sb = new StringBuilder();
int iRow = 0;
for (int iTemp = 0; iTemp < iSelRowIndexs.Length; iTemp++)
{
iRow = iSelRowIndexs[iTemp];
gcs = bandedGridView1.GetSelectedCells(iRow);
foreach(DevExpress.XtraGrid.Columns.GridColumn gc in gcs)
{
sb.Append(bandedGridView1.GetRowCellValue(iRow, gc));

if (gc != gcs[gcs.Length - 1]) // 加上 tab符,除了最后一列外
sb.Append(Convert.ToChar(Keys.Tab));
}

if (iRow != iSelRowIndexs[iSelRowIndexs.Length - 1]) // 加上换行符。除了最后一行外
sb.Append(Convert.ToChar(Keys.Return));
}

Clipboard.SetText(sb.ToString());
}

// 这里暂时只有 decimal型数据,并且每次粘贴只有一列的
// 要么是折扣列,要么是评估收视率列
private void PasteToGrid(DevExpress.XtraGrid.Views.BandedGrid.BandedGridView bandedGrid,int iStartRowIndex,int iSelRowCount,string sFieldName)
{
string sClipBoard = Clipboard.GetText().TrimEnd('\n').TrimEnd('\r'); // 去掉最后一个\r\n;
string[] aRow = sClipBoard.Split(Convert.ToChar(Keys.Return)); // 取得行
string[] aCell = new string[1];

if (aRow == null || aRow.Length == 0)
{
Common.DisplayMsg(this.Text,"粘贴板没有数据");
return;
}

if (iSelRowCount != aRow.Length)
{
Common.DisplayMsg(this.Text, "选择的数据行数与粘贴板的行数不匹配\r\n\r\n选择行数: " + iSelRowCount + "\r\n粘贴板行数: " + aRow.Length);
return;
}

decimal dTemp=0.0M;
bool bResult = false;
for (int i = 0; i < aRow.Length; i++)
{
aCell = aRow[i].Split(Convert.ToChar(Keys.Tab));// 取得列
if (!Common.IsNullOrEmptyObject(aCell[0]))
{
bResult = Decimal.TryParse(aCell[0], out dTemp);
if (dTemp < 0) dTemp = 0;
if (sFieldName == "Discount")
{
if (dTemp == 0) dTemp = 100;
if (dTemp > 100) dTemp = 100;
if (dTemp > 0 && dTemp <= 1) dTemp = dTemp * 100;
}
bandedGrid.SetRowCellValue(iStartRowIndex + i, sFieldName, dTemp);
}
else
{
bandedGrid.SetRowCellValue(iStartRowIndex + i, sFieldName, 0);
}
}
}

private void PasteNewRowsToGrid(DevExpress.XtraGrid.Views.BandedGrid.BandedGridView bandedGrid, string[] sFieldNames)
{
string sClipBoard = Clipboard.GetText().TrimEnd('\n').TrimEnd('\r'); // 去掉最后一个\r\n;
string[] aRow = sClipBoard.Split(Convert.ToChar(Keys.Return)); // 取得行
string[] aCell = new string[1];

if (aRow == null || aRow.Length == 0)
{
Common.DisplayMsg(this.Text,"粘贴板没有数据");
return;
}

if (sFieldNames.Length != aRow[0].Split(Convert.ToChar(Keys.Tab)).Length)
{
Common.DisplayMsg(this.Text, "需要的列数与粘贴板的列数不匹配\r\n\r\n需要的列数: " + sFieldNames.Length + "\r\n粘贴板行数: " + aRow.Length);
return;
}

decimal dTemp=0.0M;
int iTemp = 0;
bool bResult = false;
int iRowIndex = 0;

int iMaxDaypartID = Common.GetMaxTableID(sqlHelper.ConnStringCPRP, "DaypartID", "Daypart");

for (int i = 0; i < aRow.Length; i++)
{
bandedGridView1.AddNewRow();
bandedGridView1.UpdateCurrentRow();
iRowIndex = bandedGridView1.RowCount - 1;

bandedGridView1.SetRowCellValue(iRowIndex, "DaypartID", ++ iMaxDaypartID);

if (! arrAddedDaypartID.Contains(iMaxDaypartID))
{
arrAddedDaypartID.Add(iMaxDaypartID);
}

aCell = aRow[i].Split(Convert.ToChar(Keys.Tab));// 取得列
for (int j = 0; j < aCell.Length; j++)
{
bResult = int.TryParse(aCell[0], out iTemp);
if (bResult)
{
bandedGridView1.SetRowCellValue(iRowIndex, bandedGridView1.Columns[sFieldNames[j]], iTemp);
}
else
{
bResult = Decimal.TryParse(aCell[0], out dTemp);
if (bResult)
{
bandedGridView1.SetRowCellValue(iRowIndex, bandedGridView1.Columns[sFieldNames[j]], dTemp);
}
else
{
bandedGridView1.SetRowCellValue(iRowIndex, bandedGridView1.Columns[sFieldNames[j]], aCell[j].Trim('\n'));
}
}
}
}
}

//
private void mnuPaste2_Click(object sender, EventArgs e)
{
this.Cursor = Cursors.WaitCursor;

string[] sFieldNames = new string[] { "MediaName","StartTime","EndTime","Dayofweek","ProgramName","Position","Cost1","Cost2","Cost3" };
PasteNewRowsToGrid(bandedGridView1,sFieldNames);

this.Cursor = Cursors.Default;
}