WinForm DevExpress GridControl,GridView 下拉框应用,数据填充,获取选中值

首次发布:2024-04-19 11:53

效果图

image.png

下拉框设置

1713497192770.jpg

核心代码

using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;

namespace DXApplication1
{
    public partial class Form1 : DevExpress.XtraEditors.XtraForm
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //下拉框数据绑定
            repositoryItemComboBox1.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
            repositoryItemComboBox1.Items.Add(new SexModel() { Text = "男", Value = 0 });
            repositoryItemComboBox1.Items.Add(new SexModel() { Text = "女", Value = 1 });
            repositoryItemComboBox1.Items.Add(new SexModel() { Text = "人妖", Value = 2 });

            //下拉框选中值改变事件
            repositoryItemComboBox1.SelectedIndexChanged += new EventHandler(ComboBoxEdit_SelectedIndexChanged);

            LoadData();
        }

        private void ComboBoxEdit_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                //获取下拉框选中值
                SexModel item = (SexModel)(sender as ComboBoxEdit).SelectedItem;
                string text = item.Text.ToString();//显示值
                int value = (int)item.Value;//实际值

                //获取gridview当前的行数据
                var data = (gridView1.DataSource as List<PersonModel>)[this.gridView1.FocusedRowHandle];
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "提示");
            }
        }

        public void LoadData()
        {
            List<PersonModel> ps = new List<PersonModel>();

            PersonModel p = new PersonModel();
            p.Name = "罗分明";
            p.Age = 18;
            p.Sex = new SexModel() { Value = 0, Text = "男" };
            ps.Add(p);

            PersonModel p1 = new PersonModel();
            p1.Name = "lqwvje";
            p1.Age = 18;
            p1.Sex = new SexModel() { Value = 1, Text = "女" };
            ps.Add(p1);

            gridControl1.DataSource = ps;
        }
    }
}

源码下载链接: https://pan.baidu.com/s/1X3xORvbGhAmIs9xso5Qpog?pwd=ckse 提取码: ckse 

本文来自 www.luofenming.com