• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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('\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;
        }

  


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
linux驱动之I2C发布时间:2022-07-13
下一篇:
(转)C++中内存泄漏的检测发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap