CSharp_DevExpress_编辑智能提示功能实现;

功能: 在RepositoryItemTextEdit内置编辑器编辑时,自动过滤匹配数据;


=>设计图


=>效果图


=>代码_FormView

using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using HelloWorld.classEntity;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using System.Collections;
using DevExpress.XtraGrid.Views.Grid;
using System.Drawing;
using DevExpress.XtraGrid.Columns;

namespace HelloWorld
{
    public partial class FormView : Form
    {
        // 数据表格
        private readonly DataTable fvdt;

        // 数据列表
        private readonly List<string> listData;
        // 过滤基数_修改后值
        private string editStr;
        // 容器位置
        private Point location;

        // 视图大小
        private readonly int viewWidth;
        private readonly int viewHeight;


        public FormView()
        {
            InitializeComponent();

            /// 初始数据_表格
            fvdt = getDataTable(16);

            /// 初始数据_列表_指定智能提示列
            listData = getDataList(fvdt, "name");
            // 弹出容器_设置
            int wvalue = getColumnWidth(fvgridview, "姓名");
            fv_pccLbCtrl.Width = wvalue;
            fvpopupcctrl.Width = wvalue + 6;
            fv_pccLbCtrl.Height = 150;
            fvpopupcctrl.Height = 156;

            // 容器位置_(默认)记得手动调节位置(添加增量);
            location = new Point(getColumnX(fvgridview, "姓名") + 20, 56);

            // 视图大小_设置
            viewWidth = fvgridctrl.Width;
            viewHeight = fvgridctrl.Height;
        }

        private void FormView_Load(object sender, EventArgs e)
        {
            // GridCtrl_绑定数据源
            fvgridctrl.DataSource = fvdt;
            fvcolDel.Caption = " "; // 操作行;
            // 视图单击_获取鼠标坐标
            fvgridview.MouseDown += fvgridview_MouseDown;
            

            // 名字_TextEdit
            RepositoryItemTextEdit fvcolNameTxt = new RepositoryItemTextEdit();
            fvcolName.ColumnEdit = fvcolNameTxt;
            // 事件处理
            fvcolNameTxt.Enter += fvcolNameTxt_Enter;
            fvcolNameTxt.EditValueChanged += fvcolNameTxt_EditValueChanged;
            fvcolNameTxt.Leave += fvcolNameTxt_Leave;
            // 容器列表_事件处理
            fv_pccLbCtrl.SelectedIndexChanged += fv_pccLbCtrl_SelectedIndexChanged;

            // 性别_RepositoryItemLookUpEdit
            fvcolSexGluedi.DataSource = getSexEntityData();
            fvcolSexGluedi.ValueMember = "SexId";
            fvcolSexGluedi.DisplayMember = "SexString";
        }

        /// <summary>
        /// 列表选择
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fv_pccLbCtrl_SelectedIndexChanged(object sender, EventArgs e)
        {
            object obj = (sender as ListBoxControl).SelectedItem;

            if (obj != null) {
                DataRow row = fvgridview.GetFocusedDataRow();
                row.BeginEdit();
                row["name"] = obj.ToString();
                row.EndEdit();
            }
        }

        /// <summary>
        /// 视图单击_获取鼠标坐标
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fvgridview_MouseDown(object sender, MouseEventArgs e)
        {
            // 鼠标位置
            location.Y = e.Location.Y +10;
            if ((location.Y + fvpopupcctrl.Height) > viewHeight)
            {
                location.Y = viewHeight - fvpopupcctrl.Height;
            }

            /*
            // 传入最后列标题(如果是最后列就这样设置)
            if ((location.X + fvpopupcctrl.Width) > viewWidth) {
                location.X = viewWidth - getColumnWidth(fvgridview, "出生日期") - fvpopupcctrl.Width;
            }
            */
        }

        /// <summary>
        /// 姓名编辑_进入编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fvcolNameTxt_Enter(object sender, EventArgs e)
        {
            if (!location.IsEmpty) {// 设置容器位置;
                fvpopupcctrl.Location = location;
            }

            fv_pccLbCtrl.Items.Clear();
            fv_pccLbCtrl.Items.AddRange(listData.ToArray());
            fvpopupcctrl.Show();
        }

        /// <summary>
        /// 姓名编辑_值改变后
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fvcolNameTxt_EditValueChanged(object sender, EventArgs e)
        {
            // 编辑器
            TextEdit edit = sender as TextEdit;
            editStr = edit.EditValue.ToString();    // 用于过滤列表数据;

            fv_pccLbCtrl.Items.Clear();
            fv_pccLbCtrl.Items.AddRange(listData.FindAll(getFilterResult).ToArray());
        }

        /// <summary>
        /// 姓名编辑_焦点离开
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fvcolNameTxt_Leave(object sender, EventArgs e)
        {
            fvpopupcctrl.Hide();
        }

        

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

        /// <summary>
        /// 过滤条件_列表
        /// </summary>
        /// <param name="filter">列表项</param>
        /// <returns></returns>
        private bool getFilterResult(string filter)
        {
            bool flag = false;  // 默认不匹配;
            if (editStr != null) {
                if (filter.IndexOf(editStr) >-1 ) {
                    flag = true;
                }
            }
            return flag;
        }

        /// <summary>
        /// 指定列宽
        /// </summary>
        /// <param name="gv">列所在视图</param>
        /// <param name="colCaption">列标题</param>
        /// <returns>指定列宽</returns>
        private static int getColumnWidth(GridView gv, string colCaption)
        {
            int withd = 0;    // 距离;

            GridColumnCollection gcc = gv.Columns;
            IEnumerator enumerator = gcc.GetEnumerator();

            GridColumn gc;
            while (enumerator.MoveNext())
            {
                gc = (GridColumn)enumerator.Current;
                if (colCaption.Equals(gc.ToString()))
                {
                    withd = gc.Width;
                    break;
                }
            }

            return withd;
        }

        /// <summary>
        /// 列X距离
        /// </summary>
        /// <param name="gv">列所在视图</param>
        /// <param name="colCaption">列标题</param>
        /// <returns>指定列x距离</returns>
        private static int getColumnX(GridView gv, string colCaption)
        {
            int columnX = 0;    // 距离;

            GridColumnCollection gcc = gv.Columns;
            IEnumerator enumerator = gcc.GetEnumerator();

            GridColumn gc;
            while (enumerator.MoveNext())
            {
                gc = (GridColumn)enumerator.Current;

                columnX += gc.Width;
                if (colCaption.Equals(gc.ToString()))
                {
                    break;
                }
            }

            return columnX;
        }

        /// <summary>
        /// 获列数据
        /// </summary>
        /// <param name="dt">数据表</param>
        /// <param name="colName">列名</param>
        /// <returns>列数据集合</returns>
        private static List<string> getDataList(DataTable dt, string colName)
        {
            List<string> names = new List<string>();
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow r in dt.Rows)
                {
                    names.Add((r[colName]).ToString());
                }
            }

            return names;
        }

        /// <summary>
        /// 获数据表
        /// </summary>
        /// <param name="rowNum">数据表行数</param>
        /// <returns>数据表</returns>
        private static DataTable getDataTable(int rowNum)
        {
            // 姓名部分数组
            string[] fnames = new string[5] { "李", "岑", "唐", "楚", "梁" };
            string[] mnames = new string[2] { "小", "大" };
            string[] lnames = new string[6] { "莉", "宝", "龙", "源", "静", "堂" };

            if (rowNum < 1)
            {
                rowNum = 6;
            }

            // 数据表
            DataTable dtable = new DataTable();
            dtable.Columns.Add("num");
            dtable.Columns.Add("name");
            dtable.Columns.Add("sex");
            dtable.Columns.Add("birthday");
            dtable.Columns.Add("del");


            // 添加数据行
            DataRow row;
            for (int i = 0; i < rowNum; i++)
            {
                row = dtable.NewRow();
                row["num"] = "2014030800" + i;
                row["name"] = fnames[(i % 5)] + mnames[(i % 2)] + lnames[(i % 6)];
                row["sex"] = ((i + 1) % 2) == 0 ? 0 : 1;
                row["birthday"] = DateTime.Today.AddYears(-(18 + i)).AddMonths(i % 12 + 1).AddDays(i % 28);
                row["del"] = false;
                dtable.Rows.Add(row);
            }
            return dtable;
        }

        /// <summary>
        /// 性别数据_RepositoryItemLookUpEdit
        /// </summary>
        /// <returns>性别列表</returns>
        private static List<SexEntity> getSexEntityData()
        {
            List<SexEntity> sexList = new List<SexEntity>();
            sexList.Add(new SexEntity(0, "女"));
            sexList.Add(new SexEntity(1, "男"));

            return sexList;
        }
    }
}

=>代码SexEntity实体类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HelloWorld.classEntity
{

    /// <summary>
    /// 性别类_SexEntity(int id, string str)
    /// </summary>
    public class SexEntity
    {
        // 性别submitValue
        private int sexId;
        public int SexId
        {
            get { return sexId; }
            set { sexId = value; }
        }

        // 性别showValue
        private string sexString;
        public string SexString
        {
            get { return sexString; }
            set { sexString = value; }
        }

        public SexEntity(int id, string str) {
            this.sexId = id;
            this.sexString = str;
        }

        public override string ToString()
        {
            return this.sexString;
        }
    }
}
展开阅读全文

没有更多推荐了,返回首页