■ GridControl 클래스의 셀에 아이콘을 표시하는 방법을 보여준다.
▶ 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 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 |
using System; using System.Data; using System.Drawing; using System.Drawing.Imaging; using System.IO; using DevExpress.Data; using DevExpress.Utils; using DevExpress.XtraEditors; using DevExpress.XtraEditors.Controls; using DevExpress.XtraEditors.Repository; using DevExpress.XtraGrid.Columns; using DevExpress.XtraGrid.Views.Base; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : XtraForm { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); this.gridView.Appearance.HeaderPanel.Font = new Font("나눔고딕코딩", 12f); this.gridView.Appearance.Row.Font = new Font("나눔고딕코딩", 12f); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 폼 로드시 처리하기 - Form_Load(sender, e) /// <summary> /// 폼 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Form_Load(object sender, EventArgs e) { string resourcePath = "TestProject.Resources."; #region GridView 컬럼을 설정한다. this.gridView.Columns.Add ( GetGridColumn ( "Check", "Check", UnboundColumnType.Boolean, 150, HorzAlignment.Center, true, FormatType.None, null ) ); this.gridView.Columns.Add ( GetGridColumn ( "Type", "Type", UnboundColumnType.Integer, 150, HorzAlignment.Center, true, FormatType.None, null ) ); this.gridView.Columns.Add ( GetGridColumn ( "Picture", "Picture", UnboundColumnType.Object, 150, HorzAlignment.Center, true, FormatType.None, null ) ); #endregion #region 바인딩할 데이타 테이블을 생성하고 데이타를 추가한다. DataTable sourceTable = new DataTable(); sourceTable.Columns.Add("Check" , typeof(bool)); sourceTable.Columns.Add("Type" , typeof(int)); sourceTable.Columns.Add("Picture", typeof(byte[])); byte[] dataSourceByteArray = GetBMPByteArray(GetResourceImage(GetType(), resourcePath + "data_source.bmp") as Bitmap); sourceTable.Rows.Add(new object[] { true , 1, dataSourceByteArray }); sourceTable.Rows.Add(new object[] { false, 2, null }); sourceTable.Rows.Add(new object[] { null , 3, null }); #endregion #region Check 필드 에디터를 생성하고 설정한다. RepositoryItemCheckEdit repositoryItemCheckEdit = this.gridControl.RepositoryItems.Add("CheckEdit") as RepositoryItemCheckEdit; repositoryItemCheckEdit.PictureChecked = GetResourceImage(GetType(), resourcePath + "read.bmp" ); repositoryItemCheckEdit.PictureUnchecked = GetResourceImage(GetType(), resourcePath + "unread.bmp"); repositoryItemCheckEdit.CheckStyle = CheckStyles.UserDefined; this.gridView.Columns["Check"].ColumnEdit = repositoryItemCheckEdit; #endregion #region Type 필드 에디터를 생성하고 설정한다. RepositoryItemImageComboBox repositoryItemImageComboBox = this.gridControl.RepositoryItems.Add("ImageComboBoxEdit") as RepositoryItemImageComboBox; ImageCollection imageCollection = new ImageCollection(); imageCollection.AddImage(GetResourceImage(GetType(), resourcePath + "error.png" )); imageCollection.AddImage(GetResourceImage(GetType(), resourcePath + "warning.png" )); imageCollection.AddImage(GetResourceImage(GetType(), resourcePath + "information.png")); repositoryItemImageComboBox.SmallImages = imageCollection; repositoryItemImageComboBox.Items.Add(new ImageComboBoxItem("Error" , 1, 0)); repositoryItemImageComboBox.Items.Add(new ImageComboBoxItem("Warning" , 2, 1)); repositoryItemImageComboBox.Items.Add(new ImageComboBoxItem("Information", 3, 2)); repositoryItemImageComboBox.GlyphAlignment = HorzAlignment.Center; this.gridView.Columns["Type"].ColumnEdit = repositoryItemImageComboBox; this.gridView.Columns["Type"].ShowButtonMode = ShowButtonModeEnum.ShowOnlyInEditor; #endregion #region Picture 필드 에디터를 생성하고 설정한다. RepositoryItemPictureEdit repositoryItemPictureEdit = this.gridControl.RepositoryItems.Add("PictureEdit") as RepositoryItemPictureEdit; repositoryItemPictureEdit.SizeMode = PictureSizeMode.Zoom; repositoryItemPictureEdit.NullText = " "; this.gridView.Columns["Picture"].ColumnEdit = repositoryItemPictureEdit; #endregion this.gridControl.DataSource = sourceTable; this.gridView.OptionsBehavior.Editable = this.allowEditCheckBox.Checked; } #endregion #region Allow Edit 체크 변경시 처리하기 - allowEditCheckBox_CheckedChanged(sender, e) /// <summary> /// Allow Edit 체크 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void allowEditCheckBox_CheckedChanged(object sender, EventArgs e) { this.gridView.OptionsBehavior.Editable = this.allowEditCheckBox.Checked; } #endregion #region 그리드 컬럼 구하기 - GetGridColumn(caption, fieldName, unboundColumnType, width, horzAlignment, allowEdit, formatType, formatString) /// <summary> /// 그리드 컬럼 구하기 /// </summary> /// <param name="caption">제목</param> /// <param name="fieldName">필드명</param> /// <param name="unboundColumnType">언바운드 컬럼 종류</param> /// <param name="width">너비</param> /// <param name="horzAlignment">수평 정렬</param> /// <param name="allowEdit">편집 허용 여부</param> /// <param name="formatType">형식 종류</param> /// <param name="formatString">형식 문자열</param> /// <returns>그리드 컬럼</returns> private static GridColumn GetGridColumn(string caption, string fieldName, UnboundColumnType unboundColumnType, int width, HorzAlignment horzAlignment, bool allowEdit, FormatType formatType, string formatString) { GridColumn gridColumn = new GridColumn(); gridColumn.Name = string.Empty; gridColumn.Caption = caption; gridColumn.FieldName = fieldName; gridColumn.UnboundType = unboundColumnType; gridColumn.Width = width; gridColumn.AppearanceHeader.Options.UseTextOptions = true; gridColumn.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center; gridColumn.AppearanceHeader.TextOptions.VAlignment = VertAlignment.Center; gridColumn.AppearanceCell.Options.UseTextOptions = true; gridColumn.AppearanceCell.TextOptions.HAlignment = horzAlignment; gridColumn.AppearanceCell.TextOptions.VAlignment = VertAlignment.Center; gridColumn.OptionsColumn.AllowEdit = allowEdit; gridColumn.DisplayFormat.FormatType = formatType; gridColumn.DisplayFormat.FormatString = formatString; gridColumn.Visible = true; return gridColumn; } #endregion #region BMP 바이트 배열 구하기 - GetBMPByteArray(sourceBitmap) /// <summary> /// BMP 바이트 배열 구하기 /// </summary> /// <param name="sourceBitmap">소스 비트맵</param> /// <returns>BMP 바이트 배열</returns> private byte[] GetBMPByteArray(Bitmap sourceBitmap) { MemoryStream memoryStream = new MemoryStream(); sourceBitmap.Save(memoryStream, ImageFormat.Bmp); return memoryStream.GetBuffer(); } #endregion #region 리소스 이미지 구하기 - GetResourceImage(formType, filePath) /// <summary> /// 리소스 이미지 구하기 /// </summary> /// <param name="tpForm">폼 타입</param> /// <param name="filePath">파일 경로</param> /// <returns>리소스 이미지</returns> /// <remarks> /// - 폼 타입은 해당 리소스 이미지를 표시할 폼을 지정한다. /// - 파일 경로는 TestProject 프로젝트의 Resources 폴더에 sample.jpg 이미지를 지정할 때 /// "TestProject.Resources.sample.jpg"가 된다. /// </remarks> private Image GetResourceImage(Type formType, string filePath) { Stream stream = formType.Assembly.GetManifestResourceStream(filePath); return Image.FromStream(stream); } #endregion } } |