winform DevExpress GridView复制单元格方法
GridView整行一条或者多行复制到剪贴板
private void menu_CopyInfo_Click(object sender, EventArgs e) { int[] selectedRow = this.gridView1.GetSelectedRows(); if (selectedRow == null || selectedRow.Length == 0) return; StringBuilder sbHeader = new StringBuilder(); StringBuilder sb = new StringBuilder(); if (selectedRow.Length == 1) { //单行复制的时候 foreach (GridColumn gridCol in this.gridView1.Columns) { if (gridCol.Visible) { sbHeader.AppendFormat("{0}:{1} \r\n", gridCol.Caption, this.gridView1.GetRowCellDisplayText(selectedRow[0], gridCol.FieldName)); } } sb.AppendLine(); } else { //多行复制的时候 foreach (GridColumn gridCol in this.gridView1.Columns) { if (gridCol.Visible) { sbHeader.AppendFormat("{0}\t", gridCol.Caption); } } foreach (int row in selectedRow) { foreach (GridColumn gridCol in this.gridView1.Columns) { if (gridCol.Visible) { sb.AppendFormat("{0}\t", this.gridView1.GetRowCellDisplayText(row, gridCol.FieldName)); } } sb.AppendLine(); } } Clipboard.SetText(sbHeader.ToString() + "\r\n" + sb.ToString()); }
GridView 单个单元格复制
private void menu_CellCopyInfo_Click(object sender, EventArgs e) { string rowCellmsg = string.Empty; GridViewInfo info = gridView1.GetViewInfo() as GridViewInfo; GridCellInfo cellInfo = info.GetGridCellInfo(gridView1.FocusedRowHandle, gridView1.FocusedColumn); if (cellInfo == null) return; if (cellInfo.CellValue == null) Clipboard.Clear(); //可能会产生null抛出异常,故需要清空剪贴板信息 else rowCellmsg = cellInfo.CellValue.ToString(); if (string.IsNullOrEmpty(rowCellmsg)) //单元格内容为空的时候,不能复制为空消息,故需要清空剪贴板的信息 { Clipboard.Clear(); } else { Clipboard.SetText(rowCellmsg); } }
本文来自博客园,作者:云辰,转载请注明原文链接:https://www.cnblogs.com/yunchen/p/13737774.html