티스토리 뷰

홈페이지/C# 윈폼 & DevExpress

C# 표안에 이미지 넣기

사과같은 내 얼굴 2021. 11. 1. 01:16

using DevExpress.XtraGrid.Views.Grid;
using System;
using System.Data;
using System.Drawing;

namespace DevTest
{
    public partial class Form2 : DevExpress.XtraEditors.XtraForm
    {
        DataTable dt = null;
        bool _FormInit = true;

        public Form2()
        {
            InitializeComponent();

            //폼 Shown 이벤트 선언
            this.Shown += DevForm_Shown;
        }

        /// <summary>
        /// 폼이 보여질 때, 일어나는 이벤트 핸들러
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        public void DevForm_Shown(object sender, EventArgs e)
        {
            //그리드 설정 및 데이터 넣기
            InitGrid();
            this.Select();

            //콤보박스
            comboBoxAddItems();

            _FormInit = false;
        }

        public void InitGrid()
        {
            uiView_Main.OptionsView.ShowGroupPanel = false;
            uiView_Main.OptionsBehavior.Editable = false;

            this.uiView_Main.Columns.Clear();

            dt = GetData();
            uiGrid_Main.DataSource = dt;

            //Column, Row 폰트 사이즈, 종류 변경

            uiView_Main.Appearance.HeaderPanel.Font = new Font("Arial", 12, FontStyle.Bold);
            uiView_Main.Appearance.Row.Font = new Font("Arial", 9, FontStyle.Bold);



            ////컬럼 Header 가운데 정렬

            for (int idx = 0; idx < dt.Columns.Count; idx++)
            {
                uiView_Main.Columns[idx].AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;

            }

            for (int i = 0; i < uiView_Main.Columns.Count; i++)
            {
                uiView_Main.Columns[i].AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
                uiView_Main.Columns[i].AppearanceHeader.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Center;

            }

            //Cell 이벤트 선언
            uiView_Main.RowCellStyle += UiView_Main_RowCellStyle;
            uiView_Main.CustomDrawCell += UiView_Main_CustomDrawCell;

            //폼 처음 열릴 때, Row 한줄 추가
            uiView_Main.AddNewRow();
        }

        /// <summary>
        /// Custom Draw Cell 이벤트 핸들러
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param> 
        private void UiView_Main_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            string str = e.CellValue.ToString();
            string PreStr = string.Empty;

            if (e.RowHandle != 0)
            {
                PreStr = Convert.ToString(uiView_Main.GetRowCellValue(e.RowHandle - 1, e.Column));
            }

            if (str.Trim().Length == 0 && (e.RowHandle == 0 || PreStr.Trim().Length > 0))

            {
                GridView view = (GridView)sender;

                Image icon = ex1.Properties.Resources.file; //리소스에 저장한 Image 저장
                e.Graphics.DrawImage(icon, new Rectangle(e.Bounds.X +(uiView_Main.Columns[e.Column.AbsoluteIndex].Width) * 2, e.Bounds.Y, 17, 17));
                e.Appearance.DrawString(e.Cache, e.DisplayText, new Rectangle(e.Bounds.X +(uiView_Main.Columns[e.Column.AbsoluteIndex].Width), e.Bounds.Y, e.Bounds.Width - 20, e.Bounds.Height));
                e.Handled = true;

            }

        }



        /// <summary>
        /// RowCellStyle 이벤트
        /// 이미지 정렬
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void UiView_Main_RowCellStyle(object sender,DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            e.Appearance.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Center;
        }


        /// <summary>
        /// 테스트 데이터 선언
        /// </summary>
        /// <returns></returns>
        public DataTable GetData()
        {
            dt = new DataTable();

            dt.Columns.Add("Image1");
            dt.Columns.Add("Image2");
            dt.Columns.Add("Image3");

            return dt;
        }

        //콤보박스 데이터 넣기
        public void comboBoxAddItems()
        {
            //콤보박스 데이터 넣기
            comboBox1.DisplayMember = "Name"; // 보이는 값
            comboBox1.ValueMember = "Num"; // 안보이는 값

            var items = new[]
            {
                new{ Name ="전체", Num = "00"},
                new{ Name ="사과", Num = "01"},
                new{ Name = "배", Num = "02"},
                new{Name = "아보카도", Num = "03"}
            };

            comboBox1.DataSource = items;

        }


        
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_FormInit)
                return;

            if ($"{comboBox1.SelectedValue}".Trim() == "00")  //이게 틀린거구나 ㅎㅎ 이제 알았네
            {
                //콤보박스 데이터 넣기
                comboBox2.DisplayMember = "Name"; // 보이는 값
                comboBox2.ValueMember = "Num"; // 안보이는 값
                var items2 = new[]
               {
                    new{ Name ="전체", Num = "00"}
                };
                comboBox2.DataSource = items2;
            }
            else
            {
                //콤보박스 데이터 넣기
                comboBox2.DisplayMember = "Name"; // 보이는 값
                comboBox2.ValueMember = "Num"; // 안보이는 값

                var items2 = new[]
                {
                new{ Name ="전체", Num = "00"},
                new{ Name ="토마토", Num = "01"},
                new{ Name = "케일", Num = "02"},
                new{Name = "키위", Num = "03"}
                };

                comboBox2.DataSource = items2;
            }
        }


        //콤보박스가 비였을 때
        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }

}

'홈페이지 > C# 윈폼 & DevExpress' 카테고리의 다른 글

C# 윈폼 comboBox 데이터 바인딩(DataTable)  (0) 2022.08.20
C# ?? / ?. 차이  (0) 2022.07.31
row 2줄  (0) 2021.11.09
C# 윈폼 comboBox 데이터 바인딩  (0) 2021.11.01
댓글