■ GridControl 클래스에서 DataTable 객체를 사용해 가상 데이터 소스를 만드는 방법을 보여준다.
▶ SampleData.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
using System.Data; namespace TestProject { /// <summary> /// 샘플 데이터 /// </summary> public class SampleData { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 샘플 테이블 구하기 - GetSampleTable(count) /// <summary> /// 샘플 테이블 구하기 /// </summary> /// <param name="count">카운트</param> /// <returns>샘플 테이블</returns> public static DataTable GetSampleTable(int count) { DataTable table = new DataTable(); table.Columns.Add("항목1", typeof(string)); table.Columns.Add("항목2", typeof(string)); table.Columns.Add("항목3", typeof(string)); table.Columns.Add("항목4", typeof(string)); table.Columns.Add("항목5", typeof(string)); for(int i = 0; i < count; i++) { table.Rows.Add("값 " + i.ToString(), "값 " + i.ToString(), "값 " + i.ToString(), "값 " + i.ToString(), "값 " + i.ToString()); } table.AcceptChanges(); return table; } #endregion } } |
▶ DataTableVirtualSourcePropertyDescriptor.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
using System; using System.ComponentModel; namespace TestProject { /// <summary> /// 데이터 테이블 가상 소스 속성 설명자 /// </summary> public class DataTableVirtualSourcePropertyDescriptor : PropertyDescriptor { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 속성명 /// </summary> private string propertyName; /// <summary> /// 속성 타입 /// </summary> private Type propertyType; /// <summary> /// 읽기 전용 여부 /// </summary> private bool isReadOnly; /// <summary> /// 가상 소스 /// </summary> private DataTableVirtualSource virtualSource; /// <summary> /// 인덱스 /// </summary> private int index; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 컴포넌트 타입 - ComponentType /// <summary> /// 컴포넌트 타입 /// </summary> public override Type ComponentType { get { return typeof(DataTableVirtualSource); } } #endregion #region 명칭 - Name /// <summary> /// 명칭 /// </summary> public override string Name { get { return this.propertyName; } } #endregion #region 속성 타입 - PropertyType /// <summary> /// 속성 타입 /// </summary> public override Type PropertyType { get { return this.propertyType; } } #endregion #region 읽기 전용 여부 - IsReadOnly /// <summary> /// 읽기 전용 여부 /// </summary> public override bool IsReadOnly { get { return this.isReadOnly; } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - DataTableVirtualSourcePropertyDescriptor(virtuslSource, index, propertyName, propertyType, isReadOnly) /// <summary> /// 생성자 /// </summary> /// <param name="virtuslSource">가상 소스</param> /// <param name="index">인덱스</param> /// <param name="propertyName">속성명</param> /// <param name="propertyType">속성 타입</param> /// <param name="isReadOnly">읽기 전용 여부</param> public DataTableVirtualSourcePropertyDescriptor(DataTableVirtualSource virtuslSource, int index, string propertyName, Type propertyType, bool isReadOnly) : base(propertyName, null) { this.virtualSource = virtuslSource; this.index = index; this.propertyName = propertyName; this.propertyType = propertyType; this.isReadOnly = isReadOnly; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 값 리셋 가능 여부 구하기 - CanResetValue(component) /// <summary> /// 값 리셋 가능 여부 구하기 /// </summary> /// <param name="component">컴포넌트</param> /// <returns>값 리셋 가능 여부</returns> public override bool CanResetValue(object component) { return false; } #endregion #region 값 리셋하기 - ResetValue(component) /// <summary> /// 값 리셋하기 /// </summary> /// <param name="component">컴포넌트</param> public override void ResetValue(object component) { } #endregion #region 값 설정하기 - SetValue(component, value) /// <summary> /// 값 설정하기 /// </summary> /// <param name="component">컴포넌트</param> /// <param name="value">값</param> public override void SetValue(object component, object value) { this.virtualSource.SetRowValue(component, this.index, value); } #endregion #region 값 구하기 - GetValue(component) /// <summary> /// 값 구하기 /// </summary> /// <param name="component">컴포넌트</param> /// <returns>값</returns> public override object GetValue(object component) { return this.virtualSource.GetRowValue(component, this.index); } #endregion #region 값 직렬화 필수 여부 구하기 - ShouldSerializeValue(component) /// <summary> /// 값 직렬화 필수 여부 구하기 /// </summary> /// <param name="component">컴포넌트</param> /// <returns>값 직렬화 필수 여부</returns> public override bool ShouldSerializeValue(object component) { return true; } #endregion } } |
▶ DataTableVirtualSource.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 |
using System; using System.Collections; using System.ComponentModel; using System.Data; namespace TestProject { /// <summary> /// 데이터 테이블 가상 소스 /// </summary> public class DataTableVirtualSource : IList, ITypedList { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 소스 테이블 /// </summary> private DataTable sourceTable; /// <summary> /// 로우 카운트 /// </summary> private int rowCount; /// <summary> /// 컬럼 카운트 /// </summary> private int columnCount; /// <summary> /// 속성 설명자 컬렉션 /// </summary> private PropertyDescriptorCollection propertyDescriptorCollection; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 데이터 테이블 - DataTable /// <summary> /// 데이터 테이블 /// </summary> public DataTable DataTable { get { return this.sourceTable; } } #endregion #region 로우 카운트 - RowCount /// <summary> /// 로우 카운트 /// </summary> public int RowCount { get { return this.rowCount; } set { if(value < 1) { value = 0; } if(this.rowCount == value) { return; } this.rowCount = value; } } #endregion #region 컬럼 카운트 - ColumnCount /// <summary> /// 컬럼 카운트 /// </summary> public int ColumnCount { get { return this.columnCount; } set { if(value < 1) { value = 0; } if(this.columnCount == value) { return; } this.columnCount = value; SetPropertyDescriptorCollection(); } } #endregion #region (IList) 카운트 - Count /// <summary> /// 카운트 /// </summary> public virtual int Count { get { return this.rowCount; } } #endregion #region (IList) 동기화 여부 - IsSynchronized /// <summary> /// 동기화 여부 /// </summary> public virtual bool IsSynchronized { get { return true; } } #endregion #region (IList) 동기 루트 - SyncRoot /// <summary> /// 동기 루트 /// </summary> public virtual object SyncRoot { get { return true; } } #endregion #region (IList) 읽기 전용 여부 - IsReadOnly /// <summary> /// 읽기 전용 여부 /// </summary> public virtual bool IsReadOnly { get { return false; } } #endregion #region (IList) 고정 크기 여부 - IsFixedSize /// <summary> /// 고정 크기 여부 /// </summary> public virtual bool IsFixedSize { get { return true; } } #endregion #region (IList) 인덱서 - this[index] /// <summary> /// 인덱서 /// </summary> /// <param name="index">인덱스</param> /// <returns>값</returns> public object this[int index] { get { return this.sourceTable.Rows[index]; } set { } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - VirtualTable(sourceTable) /// <summary> /// 생성자 /// </summary> /// <param name="sourceTable">소스 테이블</param> public DataTableVirtualSource(DataTable sourceTable) { this.sourceTable = sourceTable; this.rowCount = GetRowCount(); this.columnCount = GetColumnCount(); SetPropertyDescriptorCollection(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Internal #region 행 값 설정하기 - SetRowValue(row, columnIndex, value) /// <summary> /// 행 값 설정하기 /// </summary> /// <param name="row">행 객체</param> /// <param name="columnIndex">컬럼 인덱스</param> /// <param name="value">값</param> internal void SetRowValue(object row, int columnIndex, object value) { DataRow dataRow = row as DataRow; if(dataRow != null) { dataRow[columnIndex] = value; } } #endregion #region 행 값 구하기 - GetRowValue(row, columnIndex) /// <summary> /// 행 값 구하기 /// </summary> /// <param name="row">행 객체</param> /// <param name="columnIndex">컬럼 인덱스</param> /// <returns>행 값</returns> internal object GetRowValue(object row, int columnIndex) { DataRow dataRow = row as DataRow; if(dataRow != null) { return dataRow[columnIndex]; } else { return null; } } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Public #region (IList) 열거자 구하기 - GetEnumerator() /// <summary> /// 열거자 구하기 /// </summary> /// <returns>열거자</returns> public virtual IEnumerator GetEnumerator() { return null; } #endregion #region (IList) 복사하기 - CopyTo(array, index) /// <summary> /// 복사하기 /// </summary> /// <param name="array">배열</param> /// <param name="index">인덱스</param> public virtual void CopyTo(Array array, int index) { } #endregion #region (IList) 추가하기 - Add(value) /// <summary> /// 추가하기 /// </summary> /// <param name="value">값</param> /// <returns>처리 결과</returns> public virtual int Add(object value) { throw new NotImplementedException(); } #endregion #region (IList) 지우기 - Clear() /// <summary> /// 지우기 /// </summary> public virtual void Clear() { throw new NotImplementedException(); } #endregion #region (IList) 포함 여부 구하기 - Contains(value) /// <summary> /// 포함 여부 구하기 /// </summary> /// <param name="value">값</param> /// <returns>포함 여부</returns> public virtual bool Contains(object value) { throw new NotImplementedException(); } #endregion #region (IList) 인덱스 구하기 - IndexOf(value) /// <summary> /// 인덱스 구하기 /// </summary> /// <param name="value">값</param> /// <returns>인덱스</returns> public virtual int IndexOf(object value) { throw new NotImplementedException(); } #endregion #region (IList) 삽입하기 - Insert(index, value) /// <summary> /// 삽입하기 /// </summary> /// <param name="index">인덱스</param> /// <param name="value">값</param> public virtual void Insert(int index, object value) { throw new NotImplementedException(); } #endregion #region (IList) 제거하기 - Remove(value) /// <summary> /// 제거하기 /// </summary> /// <param name="value">값</param> public virtual void Remove(object value) { throw new NotImplementedException(); } #endregion #region (IList) 제거하기 - RemoveAt(index) /// <summary> /// 제거하기 /// </summary> /// <param name="index">인덱스</param> public virtual void RemoveAt(int index) { throw new NotImplementedException(); } #endregion #region (ITypedList) 항목 속성 컬렉션 구하기 - GetItemProperties(propertyDescriptorArray) /// <summary> /// 항목 속성 컬렉션 구하기 /// </summary> /// <param name="propertyDescriptorArray">속성 설명자 배열</param> /// <returns>항목 속성 컬렉션</returns> public PropertyDescriptorCollection GetItemProperties(PropertyDescriptor[] propertyDescriptorArray) { return this.propertyDescriptorCollection; } #endregion #region (ITypedList) 리스트명 구하기 - GetListName(propertyDescriptorArray) /// <summary> /// 리스트명 구하기 /// </summary> /// <param name="propertyDescriptorArray">속성 설명자 배열</param> /// <returns>리스트명</returns> public string GetListName(PropertyDescriptor[] propertyDescriptorArray) { return this.sourceTable.TableName; } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 속성 설명자 컬렉션 설정하기 - SetPropertyDescriptorCollection() /// <summary> /// 속성 설명자 컬렉션 설정하기 /// </summary> protected virtual void SetPropertyDescriptorCollection() { DataTableVirtualSourcePropertyDescriptor[] propertyDescriptorArray = new DataTableVirtualSourcePropertyDescriptor[this.columnCount]; for(int i = 0; i < this.columnCount; i++) { propertyDescriptorArray[i] = new DataTableVirtualSourcePropertyDescriptor ( this, i, this.sourceTable.Columns[i].ColumnName, this.sourceTable.Columns[i].DataType, false ); } this.propertyDescriptorCollection = new PropertyDescriptorCollection(propertyDescriptorArray); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region 로우 카운트 구하기 - GetRowCount() /// <summary> /// 로우 카운트 구하기 /// </summary> /// <returns>로우 카운트</returns> private int GetRowCount() { if(this.sourceTable == null) { return 0; } return this.sourceTable.Rows.Count; } #endregion #region 컬럼 카운트 구하기 - GetColumnCount() /// <summary> /// 컬럼 카운트 구하기 /// </summary> /// <returns>컬럼 카운트</returns> private int GetColumnCount() { if(this.sourceTable == null) { return 0; } return this.sourceTable.Columns.Count; } #endregion } } |
▶ MainForm.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
using DevExpress.XtraEditors; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : XtraForm { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); DataTableVirtualSource virtualSource = new DataTableVirtualSource(SampleData.GetSampleTable(1_000_000)); this.gridControl.DataSource = virtualSource; } #endregion } } |