前段时间我有一个朋友面试公司的时候遇到这个面试题,他也给了份原题给我瞧瞧,并没有什么特别的要点,关于这一类问题,如何在网格上的单元格嵌入多个控件(如按钮、超链接等)问题,我在网上搜索了下这类问题,发现很多解答但是都杂乱,本篇文章帮助大家了解如何应对这类问题。

微软提供的DataGirdView网格控件可通过GetCellDisplayRectangle()方法将需要的控件嵌入单元格中,如:

this.Load += Form1_Load;

void Form1_Load(object sender, EventArgs e)
{
  MulAutoBtnEdit();
}

private void MulAutoBtnEdit()
{
  this.dataGridView1.Columns.Add("ColBtnEdit", "嵌入操作按钮");
  this.dataGridView1.Columns["ColBtnEdit"].Width = 150;
  int index = this.dataGridView1.Columns["ColBtnEdit"].Index;
  this.dataGridView1.Columns["ColBtnEdit"].Resizable = DataGridViewTriState.False;
  Button btnAdd = GetBtnByType("BtnAdd","新增");
  Button btnEdit = GetBtnByType("BtnEdit", "修改");
  Button btnDel = GetBtnByType("BtnDel", "删除");
  this.dataGridView1.Controls.Add(btnAdd);
  this.dataGridView1.Controls.Add(btnEdit);
  this.dataGridView1.Controls.Add(btnDel);
  Rectangle rectangle = this.dataGridView1.GetCellDisplayRectangle(index, 0, true);//获取当前单元格上的矩形区域
  btnAdd.Size = btnEdit.Size = btnDel.Size = new Size(rectangle.Width / 3 + 1, rectangle.Height);
  btnAdd.Location = new Point(rectangle.Left, rectangle.Top);
  btnEdit.Location = new Point(rectangle.Left + btnAdd.Width, rectangle.Top);
  btnDel.Location = new Point(rectangle.Left + btnAdd.Width + btnDel.Width, rectangle.Top);
}

private Button GetBtnByType(string strBtnName,string strBtnText)
{
  Button btn = new Button();
  btn.Name = strBtnName;
  btn.Text = strBtnText;
  btn.Click += btn_Click;
  return btn;
}

private void btn_Click(object sender, EventArgs e)
{
  if(sender is Button)
  {
    Button btn = (Button)sender;
    MessageBox.Show(string.Format("点击按钮:{0}",btn.Text));
  }
}

效果图:

通过以上的操作即可将多个按钮嵌入到单元格中,不考虑维护性、效率问题,只在于给予大家思路,对于“超链接”控件等都可用类似的方法处理,类似网址如下:

https://bbs.csdn.net/topics/340208660

在网上发行另一种方式,思路差不多,地址我也贴出来:

https://blog.csdn.net/linzi1015910507/article/details/52595863

关于第三方DevExpress组件上的网格如GridView等,本人并没有找到好的解决上面问题的方法,有不足之处,知情人事可在留言板上赐教。

A young idler ~ an old beggar !

DataGridView的单元格如何嵌入多个按钮控件的更多相关文章

  1. WinForm中DataGridView验证单元格输入的是数字

    转载:http://www.cnblogs.com/ganqiyin/archive/2013/02/18/2915491.html 事件:DataGridView验证单元格输入的是数字,DataGr ...

  2. 当前线程不在单线程单元中,因此无法实例化 ActiveX 控件

    “/”应用程序中的服务器错误. 当前线程不在单线程单元中,因此无法实例化 ActiveX 控件“c552ea94-6fbb-11d5-a9c1-00104bb6fc1c”. 说明: 执行当前 Web ...

  3. DataGridView 的单元格的边框、 网格线样式的设定【转】

    1) DataGridView 的边框线样式的设定DataGridView 的边框线的样式是通过 DataGridView.BorderStyle 属性来设定的. BorderStyle 属性设定值是 ...

  4. DataGridView合并单元格

    昨天一个同事问我DataGridView单元格合并的问题,一开始按照我的设想是算出两个单元格的Rectangle,然后直接使用e.Graphics.FillRectangle(backColorBru ...

  5. DataGridView的单元格控制只能输入数字

    主要是应用DataGridView的EditingControlShowing事件.当单元格进入编辑模式时,可以处理执行该编辑控件的自定义初始化的此事件. public DataGridViewTex ...

  6. 如何通过DataGridView 实现单元格合并和二维表头

    先看下实现出来的效果(这里随便写了几组数据,用来测试) 先初始一个DataGridView 设置哪几列 DataGridView 里男女这两列的 AutoSizeMode 可以设置Fill. publ ...

  7. DataGridView合并单元格(多行多列合并)

    一.点击在拖入的显示控件(TreeList)右上方的箭头,在Treelist任务中选择数据源,添加项目数据源,依次选择数据库.数据集,新建连接,浏览选择数据库(*.mdb),依次点击 下一步,选择“表 ...

  8. DataGridView合并单元格(一列或一行)

    #region"合并单元格的测试(一列或一行)" // int?是搜索一种类型(可空类型),普通的int不能为null,而用int?,其值可以为null //private int ...

  9. DataGridView 的单元格的边框、 网格线样式的设定

    1) DataGridView 的边框线样式的设定DataGridView 的边框线的样式是通过 DataGridView.BorderStyle 属性来设定的. BorderStyle 属性设定值是 ...

随机推荐

  1. 项目总结三:目标检测项目(Car detection with YOLOv2)

    1. the YOLO model (YOLO ,you only look once) (1)We will use 5 anchor boxes. So you can think of the ...

  2. threadid=1: thread exiting with uncaught exception (group=0x40fca9a8)的问题

    今天在项目开发中碰到了这样一个问题: 项目在Nexus5[Android 6.0]上面运行正常,在华为荣耀6[Android 4.4.2]上面运行到指定activity崩溃(不是应用启动崩溃):然后后 ...

  3. 大数据Hadoop核心架构HDFS+MapReduce+Hbase+Hive内部机理详解

    微信公众号[程序员江湖] 作者黄小斜,斜杠青年,某985硕士,阿里 Java 研发工程师,于 2018 年秋招拿到 BAT 头条.网易.滴滴等 8 个大厂 offer,目前致力于分享这几年的学习经验. ...

  4. 比MySQL快6倍 深度解析国内首个云原生数据库POLARDB的“王者荣耀”

    随着移动互联网.电子商务的高速发展,被使用最多的企业级开源数据系统MySQL面临着巨大挑战——为迎接“双11"的高并发要提前做好分库分表;用户不断激增要将读写分离才能应对每天上亿次的访问,但 ...

  5. 【Apache Pulsar】Apache Pulsar单机环境及Go语言开发环境搭建

    0x01 简介 Apache Pulsar是一个开源的分布式发布-订阅消息系统,与Kafka类似,但比后者更加强大.Pulsar最初由Yahoo开发并维护,目前已经成为Apache软件组织的一个孵化子 ...

  6. Perl语法的基本规则

    因为是比较凌乱的用法规则收集,所以能看懂则看,不能看懂也无所谓.以后也会遇到. Perl脚本第一行使用#!.Perl的后缀名一般为".plx"或".pl",运行 ...

  7. Perl构建和打包自己的模块

    当写好一个或多个模块后,可以将它构建.打包成"tar.gz",以便让别人安装或者上传到CPAN(如果愿意的话).对于模块的使用者来说,也不用再使用use lib 'LIB_PATH ...

  8. springmvc和structs2的区别

    1.从安全性角度分析spring mvc和struts2的区别: spring mvc:controller 1.spring mvc 默认controller是单实例(通过注解@Scope(“pro ...

  9. 第一册:lesson sixty one.

    原文: A bad cold. A:Where is Jim? B:He is in bed. A:What's the matter with him? B:He fells ill. A:He l ...

  10. 我的IdentityServer目录

    概念部分 理解oauth协议 理解什么是claim 学习Identity Server 4的预备知识 Open ID Connect(OIDC)在 ASP.NET Core中的应用 操作部分 入门: ...