qq_26624753
2017-01-23 08:31
采纳率: 81.8%
浏览 1.6k

在WinForm下,DEV里面的GridControl控件问题

,设置第一列列为下拉列表,另一列根据第一列选择的结果,决定这列是下拉列表还是文本框,
举例第一列里面选择项有日期和自定义,选择日期,另一列就是下拉列表,里面数据是日期格式的选择,选择自定义,另一列就是可编辑的文本框,可以自由输入

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • blownewbee 2017-01-23 16:11

    在你的下拉的事件里更改gridview的columntemplate

    点赞 评论
  • larry_wen 2017-01-27 03:42
    using DevExpress.XtraEditors.Repository;
    using DevExpress.XtraGrid.Columns;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
    
                var cb = new RepositoryItemComboBox();
                cb.Items.Add("");
                gridControl1.RepositoryItems.Add(cb);
                gridView1.Columns.AddRange(new GridColumn[] { new GridColumn() { Caption = "选项", FieldName = "FieldName", ColumnEdit = cb, Visible = true }, new GridColumn() { Caption = "值", FieldName = "Value", Visible = true } });
    
                var dt = new Source();
                dt.AddField("文本", SqlDbType.NVarChar);
                dt.AddField("日期", SqlDbType.SmallDateTime);
                dt.AddData("");
                dt.AddData("");
    
                dt.Fields.ForEach(x => { cb.Items.Add(x.FieldName); });
                gridControl1.DataSource = dt.Datas;
    
                cb.EditValueChanged += (s, e) =>
                {
                    dt.Datas[gridView1.GetFocusedDataSourceRowIndex()].Value = null;
                };
    
                gridView1.CustomRowCellEdit += (s, e) =>
                {
                    if (e.Column.FieldName != "Value") return;
                    var item = dt.Fields.Find(x =>
                    {
                        return x.FieldName == gridView1.GetRowCellValue(e.RowHandle, "FieldName").ToString();
                    });
                    if (item != null) e.RepositoryItem = item.Editor;
                };
            }
        }
    
        public partial class Source
        {
    
            public virtual List<Field> Fields { get; } = new List<Field>();
            public virtual BindingList<Data> Datas { get; set; } = new BindingList<Data>();
    
            public virtual Data AddData(string fieldname, object value = null)
            {
                var r = new Data(fieldname, value);
                Datas.Add(r);
                return r;
            }
    
            public virtual Field AddField(string fieldname, SqlDbType type)
            {
                var r = CreateEditor(type);
                if (r != null)
                {
                    var f = new Field(fieldname, r);
                    Fields.Add(f);
                    return f;
                }
                else return null;
            }
    
            protected virtual RepositoryItem CreateEditor(SqlDbType type)
            {
                if (type == SqlDbType.NVarChar || type == SqlDbType.NChar)
                {
                    var edit = new RepositoryItemTextEdit();
                    return edit;
                }
                else if (type == SqlDbType.Date || type == SqlDbType.DateTime || type == SqlDbType.SmallDateTime)
                {
                    var date = new RepositoryItemDateEdit();
                    return date;
                }
                else return null;
            }
    
            public partial class Field
            {
                public string FieldName { get; set; } = "";
    
                public RepositoryItem Editor { get; set; } = null;
                public Field() { }
                public Field(string fieldname, RepositoryItem editor)
                {
                    FieldName = fieldname;
                    Editor = editor;
                }
            }
    
            public partial class Data : INotifyPropertyChanged
            {
                string _fieldname = "";
                object _value = null;
                public string FieldName
                {
                    get { return _fieldname; }
                    set
                    {
                        _fieldname = value;
                        OnPropertyChanged("FieldName");
                    }
                }
    
                public object Value
                {
                    get { return _value; }
                    set
                    {
                        _value = value;
                        OnPropertyChanged("Value");
                    }
                }
    
                public Data() { }
    
                public Data(string fieldname, object value)
                {
                    _fieldname = fieldname;
                    _value = value;
                }
    
                public event PropertyChangedEventHandler PropertyChanged;
                protected void OnPropertyChanged(string name)
                {
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
                }
            }
        }
    }
    
    
    点赞 评论

相关推荐 更多相似问题