검색결과 리스트
Copy & Paste에 해당되는 글 1건
- 2012.04.11 [XtraGrid] Copy & Paste 구현
글
링크 : http://www.devexpress.com/Support/Center/p/A1266.aspx
Copy 구현 : http://www.devexpress.com/Support/Center/p/A332.aspx
셀렉션 옵션 : http://documentation.devexpress.com/#WindowsForms/CustomDocument711
using System;
using System.Drawing;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Data;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.Utils.Drawing;
namespace KIS.DevExpressHelper
{
public class GridLClipboardHelper
{
private const string CellDelimiter = "\t";
private const string LineDelimiter = "\r\n";
protected GridView _view;
/// 생성자
public GridLClipboardHelper(GridView view)
{
this.View = view;
}
public GridView View
{
get { return _view; }
set
{
if ( _view != value )
{
Detach();
Attach(value);
}
}
}
protected virtual void Attach(GridView view)
{
if ( view == null )
return;
this._view = view;
if ( view.IsMultiSelect == false )
{
view.OptionsSelection.MultiSelect = true;
view.OptionsSelection.MultiSelectMode = GridMultiSelectMode.RowSelect;
}
this.View.KeyDown += new KeyEventHandler(View_KeyDown);
}
private bool CompareGrid(GridView srcView, GridView tgtView)
{
int srcCnt = srcView.Columns.Count;
int tgtCnt = tgtView.Columns.Count;
if ( srcView == null || tgtView == null )
{
return false;
}
if ( srcCnt != tgtCnt )
{
return false;
}
for ( int i = 0; i < srcCnt; i++ )
{
if ( srcView.Columns[i].ColumnType != tgtView.Columns[i].ColumnType
|| srcView.Columns[i].FieldName != tgtView.Columns[i].FieldName
)
{
return false;
}
}
return true;
}
void View_KeyDown(object sender, KeyEventArgs e)
{
GridView view = sender as GridView;
// 붙여 넣기
if ( this.CompareGrid(this._view, view) == true && view.IsMultiSelect == true && e.Control == true && e.KeyCode == Keys.V )
{
this.Paste(view);
}
// 복사하기
else if ( this.CompareGrid(this._view, view) == true && view.IsMultiSelect == true && e.Control == true && e.KeyCode == Keys.C )
{
e.SuppressKeyPress = true;
this.Copy(view);
}
// 모두 선택
else if ( this.CompareGrid(this._view, view) == true && view.IsMultiSelect == true && e.Control == true && e.KeyCode == Keys.A )
{
this.SelectAll(view);
}
// 삭제
else if ( this.CompareGrid(this._view, view) == true && view.IsMultiSelect == true && e.KeyCode == Keys.Delete )
{
this.DeleteRows(view);
}
// 오려내기
else if ( this.CompareGrid(this._view, view) == true && view.IsMultiSelect == true && e.Control == true && e.KeyCode == Keys.X )
{
this.Copy(view);
this.DeleteRows(view);
}
}
/// 모두 선택
public void SelectAll(GridView view)
{
view.SelectAll();
}
// 복사하기
public void Copy(GridView view)
{
StringBuilder sb = new StringBuilder();
int columnNameCnt = view.Columns.Count;
for ( int i = 0; i < columnNameCnt; i++ )
{
try
{
sb.Append(view.Columns[i].FieldName.ToString());
}
catch ( ArgumentException )
{
sb.Append("");
}
if ( i+1 < columnNameCnt )
sb.Append(CellDelimiter);
}
sb.Append(LineDelimiter);
int[] rows = view.GetSelectedRows();
int rowsCnt = rows.Length;
for ( int i = 0; i < rowsCnt; i++ )
{
int handle = rows[i];
DataRowView rowView = view.GetRow(handle) as DataRowView;
if ( rowView != null )
{
for ( int j = 0; j < columnNameCnt; j++ )
{
try
{
sb.Append(rowView[view.Columns[j].FieldName].ToString());
}
catch ( ArgumentException )
{
sb.Append("");
}
if ( j+1 < columnNameCnt )
sb.Append(CellDelimiter);
}
}
if ( i+1 < rowsCnt )
sb.Append(LineDelimiter);
}
DataObject data = new DataObject();
data.SetData(DataFormats.Text, sb.ToString());
Clipboard.Clear();
Clipboard.SetDataObject(data, true);
}
// 붙여넣기
public void Paste(GridView view)
{
IDataObject data = Clipboard.GetDataObject();
if ( data.GetDataPresent(DataFormats.Text) == true )
{
view.BeginUpdate();
try
{
string strData = data.GetData(DataFormats.Text) as string;
string[] strRows = strData.Split(new string[] { LineDelimiter }, StringSplitOptions.None);
string[] strColumnNames = strRows[0].Split(new string[] { CellDelimiter }, StringSplitOptions.None);
List< object > list = new List< object >();
int dataCount = strRows.Length-1; // 헤더 개수 제외
int rowHandle = view.FocusedRowHandle;
int targetDeleteRowCount = view.DataRowCount - rowHandle + 1;
if ( rowHandle != GridControl.InvalidRowHandle && rowHandle != GridControl.NewItemRowHandle )
{
// 붙여 넣기할 Row가 현재 선택된 행부터 밑에 행보다 많다.
if ( targetDeleteRowCount > dataCount )
{
for ( int i = rowHandle + dataCount; i < view.DataRowCount; i++ )
{
list.Add(view.GetRow(i));
}
}
// 현재 선택된 행부터 끝까지 삭제한다.
for ( int i = view.DataRowCount-1; i >= rowHandle; i-- )
{
view.DeleteRow(i);
}
}
else if ( rowHandle == GridControl.NewItemRowHandle )
{
view.DeleteRow(rowHandle);
}
for ( int i = 1; i < strRows.Length; i++ )
{
view.AddNewRow();
int handle = view.FocusedRowHandle;
string[] strCells = strRows[i].Split(new string[] { CellDelimiter }, StringSplitOptions.None);
int colCnt = strColumnNames.Length;
for ( int j = 0; j < colCnt; j++ )
{
if ( strColumnNames[j].Trim().Equals("") == false && j < strCells.Length )
{
try
{
Type t = view.Columns[j].ColumnType;
object value = null;
if ( t == typeof(int) )
{
if ( strCells[j] != null && strCells[j].ToString().Equals("") == false )
{
try
{
value = Convert.ToInt32(strCells[j]);
if ( value != null )
view.SetRowCellValue(handle, strColumnNames[j], value);
}
catch ( Exception ex )
{
System.Console.WriteLine(ex.StackTrace);
}
}
}
else if ( t == typeof(DateTime) )
{
if ( strCells[j] != null && strCells[j].ToString().Equals("") == false )
{
try
{
value = Convert.ToDateTime(strCells[j]);
if ( value != null )
view.SetRowCellValue(handle, strColumnNames[j], value);
}
catch ( Exception ex )
{
System.Console.WriteLine(ex.StackTrace);
}
}
}
else if ( t == typeof(string) )
{
if ( strCells[j] != null )
{
try
{
value = Convert.ToString(strCells[j]);
if ( value != null )
view.SetRowCellValue(handle, strColumnNames[j], value);
}
catch ( Exception ex )
{
System.Console.WriteLine(ex.StackTrace);
}
}
}
}
catch ( Exception ex)
{
//MessageBox.Show(ex.Message + "\n\n" + ex.StackTrace);
}
}//if ( strColumnNames[j].Trim().Equals("") == false )
}//for ( int j = 0; j < colCnt; j++ )
}//for ( int i = 1; i < strRows.Length; i++ )
DataView dv = view.DataSource as DataView;
for ( int i = 0; i < list.Count; i++ )
{
DataRowView drv = list[i] as DataRowView;
if ( drv != null )
{
DataRowView newDRV = dv.AddNew();
newDRV.BeginEdit();
try
{
int columNameCount= drv.Row.Table.Columns.Count;
for ( int j = 0; j < columNameCount; j++ )
{
string fieldName = drv.Row.Table.Columns[j].ColumnName;
newDRV[fieldName] = drv[fieldName];
}
newDRV.EndEdit();
}
catch ( Exception )
{
newDRV.CancelEdit();
}
}
}
}
catch ( Exception ex )
{
MessageBox.Show(ex.StackTrace);
}
finally
{
view.EndUpdate();
}
}//if ( data.GetDataPresent(DataFormats.Text) == true )
}
/// 현재 선택된 로우를 삭제한다.
private void DeleteRows(GridView view)
{
view.BeginUpdate();
int[] rows = view.GetSelectedRows();
for ( int i = rows.Length-1; i >= 0 ; i-- )
{
view.DeleteRow(rows[i]);
}
view.EndUpdate();
}
protected virtual void Detach()
{
if ( this._view == null )
return;
this.View.KeyDown -= new KeyEventHandler(View_KeyDown);
_view = null;
}
}
}
'.Net > XtraGrid' 카테고리의 다른 글
[XtraGrid] XtraGrid Option (0) | 2013.01.24 |
---|---|
[XtraGrid] Online Document (0) | 2012.04.13 |
[XtraGrid] How to make my grid columns read-only (0) | 2012.04.08 |
[XtraGrid] Checkbox 구현하기 (1) | 2012.04.06 |
[XtraGrid] Fixed Columns (0) | 2012.04.05 |
RECENT COMMENT