• C#、devExpress 的 给bandedGrid加菜单功能 :复制、粘贴的例子(转)


    C#、devExpress 的 给bandedGrid加菜单功能 :复制、粘贴的例子
     

    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('
    ').TrimEnd('
    '); // 去掉最后一个
    ;
                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, "选择的数据行数与粘贴板的行数不匹配
    
    选择行数: " + iSelRowCount + "
    粘贴板行数: " + 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('
    ').TrimEnd('
    '); // 去掉最后一个
    ;
                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, "需要的列数与粘贴板的列数不匹配
    
    需要的列数: " + sFieldNames.Length + "
    粘贴板行数: " + 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('
    '));
                            }
                        }
                    }
                }
            }
     
            // 
            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;
            }
    

      

  • 相关阅读:
    Oracle VM VirtualBox 虚拟机中桥接模式一直不能用 ,需要安装 VBoxNetLwf.inf
    Windows7窗口跑到屏幕外面
    手动操作群晖蜂鸣器指示灯方法
    DHT11温湿度传感器接入HomeBridge
    DIY树莓派Homebridge智能台灯
    群晖NAS局域网无法跑满千兆排查
    虚拟机性能监控与故障处理工具------JDK的命令行工具
    垃圾收集器与内存分配策略-内存分配与回收策略
    Minor GC 与Full GC有什么不一样
    java的关键字与保留字
  • 原文地址:https://www.cnblogs.com/lhyqzx/p/9345742.html
Copyright © 2020-2023  润新知