C# DevExpress中 GridControl某列添加小图标(删除图标为例)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Fing_king/article/details/5805509

效果如图:

首先导入命名空间

using DevExpress.XtraEditors;

using DevExpress.XtraGrid.Views.Grid;
using System.IO;

using System.Reflection;

 

定义一个PictureEdit

       private PictureEdit _pedDelete; //删除

 

然后在 GridView1的MouseMove事件中判断。

 //删除列表集合
        private void GridView1_MouseMove(object sender, MouseEventArgs e)
        {

            GridView view = sender as GridView;
            GridHitInfo htInfo = view.CalcHitInfo(e.X, e.Y);
            if (!htInfo.InRowCell)
            {

                if (_pedDelete != null)
                    _pedDelete.Visible = false;
                return;
            }

            string name = htInfo.Column.FieldName; //取得鼠标所在的列名
            if ("OrderCode".Equals(name)) //判断是那一列中显示 删除图标
            {
                GridViewInfo vInfo = view.GetViewInfo() as GridViewInfo;
                GridCellInfo cInfo = vInfo.GetGridCellInfo(htInfo);
                if (_pedDelete == null)
                {
                    _pedDelete = new PictureEdit(); //实例删除图片按钮
                    Stream stream = GetImgStream("GridImage.delete.gif"); //加载图片     加载图片函数在最后。
                    if (stream != null)
                        _pedDelete.Image = System.Drawing.Image.FromStream(stream);
                    _pedDelete.Click += new EventHandler(ImgDel_Click); //注册删除事件   具体实践如下
                    GridControl1.Controls.Add(_pedDelete);
                }

                Rectangle r = new Rectangle(cInfo.Bounds.Right - 16, cInfo.Bounds.Bottom - 15, 15, 15); //调整显示图片位置
                _pedDelete.Bounds = r; // 设定位置
                _pedDelete.Tag = htInfo.RowHandle;
                r.X -= 16;
                _pedDelete.Visible = true;
            }
            else
            {
                if (_pedDelete != null)
                    _pedDelete.Visible = false;
            }
        }

 

         /// <summary>
        /// 小图标删除点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>        protected void ImgDel_Click(object sender, EventArgs e)
        {
            int rowhandle = (int)(sender as PictureEdit).Tag;
            if (MessageBox.Show(this, "确定删除吗?", "信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
            {
                GridView1.DeleteRow(rowhandle);  //删除当前行,修改行的RowState 为Deleted
                GridView1.UpdateCurrentRow();
                GridView1.RefreshData();  //更新视图
            }
        }

 

 

        /// <summary>
        /// 得到项目中图片
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static Stream GetImgStream(string name)
        {
            Assembly asm = Assembly.GetEntryAssembly();
            string asmName = asm.GetName().Name;
            Stream noStream = asm.GetManifestResourceStream(asmName + ".Image." + name);
            return noStream;
        }

 

此效果可以延伸为 添加图标,编辑图标。三者可以同时存在,也可单个存在。就看大家如何用。

展开阅读全文

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