zoukankan      html  css  js  c++  java
  • DataGridView 显示提示信息

    前言

    本篇文章主要介绍了在使用DataGridView时,为了显示当前行的详细信息从而进行提示的三种方法。

    内容

    我们可以简单制作一个如下的界面

    还要制作一个存储人信息的Person类,提供两个公开属性姓名和性别即可。现在介绍下三种方法。

    方法一:

    可以使用DataGridView的CellToolTipTextNeeded事件提供提示信息。后台主要打码如下:

    使用CellToolTipTextNeeded事件
     1 public partial class Form1 : Form
    2 {
    3 List<Person> persons = new List<Person>();
    4
    5 public Form1()
    6 {
    7 InitializeComponent();
    8 }
    9
    10 private void Form1_Load(object sender, EventArgs e)
    11 {
    12 persons.Add(new Person("小明",""));
    13 persons.Add(new Person("小红", ""));
    14 persons.Add(new Person("小蓝", ""));
    15 persons.Add(new Person("小李", ""));
    16 dgv.DataSource = persons;
    17 }
    18
    19 private void dgv_CellToolTipTextNeeded(object sender, DataGridViewCellToolTipTextNeededEventArgs e)
    20 {
    21 if (sender is DataGridView)
    22 {
    23 e.ToolTipText ="姓名:"+ persons[e.RowIndex].Name+" 性别:"+ persons[e.RowIndex].Sex;
    24 }
    25 }

    只要是鼠标移动到该行的任意一个单元格上都会提示该行的信息。

    效果图:

    方法二:

    自定义提示信息。使用这种方法,我们需要自己做提示框。我的做法是先托一个Panel,再在里面加入一个label,并且让这两者的AutoSize都为true。该方法是在点击该单元格时进行的提示。后台主要代码如下:

    自定义提示信息
     1 public partial class Form2 : Form
    2 {
    3 /// <summary>
    4 /// 是否为内容标记位。当鼠标点击表头或表头列时不应该提示记录相关信息
    5 /// </summary>
    6 bool isContentFlag = true;
    7
    8 /// <summary>
    9 /// 时间标记位。用来控制提示记录信息显示的时间
    10 /// </summary>
    11 int timeFlag = 0;
    12
    13 List<Person> persons = new List<Person>();
    14
    15 public Form2()
    16 {
    17 InitializeComponent();
    18 }
    19
    20 /// <summary>
    21 /// 当鼠标点击该行时,显示自定义的提示框
    22 /// </summary>
    23 /// <param name="sender"></param>
    24 /// <param name="e"></param>
    25 private void DataGridView_MouseClick(object sender, MouseEventArgs e)
    26 {
    27 if (sender is DataGridView)
    28 {
    29 DataGridView dgv = (DataGridView)sender;
    30 if (isContentFlag)
    31 {
    32 lblToolTip.Text = string.Empty; //先清空
    33 lblToolTip.Text += "姓名:" + persons[dgv.CurrentRow.Index].Name + " 性别:" + persons[dgv.CurrentRow.Index].Sex;
    34 panelToolTip.Location = new Point(e.X + dgv.Location.X + 5, e.Y + dgv.Location.Y + 10);
    35 panelToolTip.Visible = true;
    36
    37 timerToolTip.Start();
    38 timeFlag = 0;
    39
    40 }
    41 }
    42 }
    43
    44 private void Form2_Load(object sender, EventArgs e)
    45 {
    46 persons.Add(new Person("小明", ""));
    47 persons.Add(new Person("小红", ""));
    48 persons.Add(new Person("小蓝", ""));
    49 persons.Add(new Person("小李", ""));
    50 DataGridView.DataSource = persons;
    51 }
    52
    53 /// <summary>
    54 /// DataGridView提示信息计时器事件,显示5秒后提示消失
    55 /// </summary>
    56 /// <param name="sender"></param>
    57 /// <param name="e"></param>
    58 private void timerToolTip_Tick(object sender, EventArgs e)
    59 {
    60 if (timeFlag > 5)
    61 {
    62 panelToolTip.Visible = false;
    63 timerToolTip.Stop();
    64 timeFlag = 0;
    65 }
    66 else
    67 {
    68 timeFlag++;
    69 }
    70 }
    71
    72 /// <summary>
    73 /// DataGridView鼠标进入显示区域事件,添加该事件的意义是为了防止点击了表头或是表头列时,也显示提示信息
    74 /// </summary>
    75 /// <param name="sender"></param>
    76 /// <param name="e"></param>
    77 private void DataGridView_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
    78 {
    79 if (e.RowIndex > -1 && e.ColumnIndex > -1)
    80 {
    81 isContentFlag = true;
    82 }
    83 else
    84 {
    85 isContentFlag = false;
    86 }
    87 }
    88 }

    效果图:

    方法三:

    该方法使用了CellMouseEnter事件,当鼠标进入该单元格时进行提示,后台主要代码如下:

    使用CellMouseEnter事件
     1 public partial class Form3 : Form
    2 {
    3 List<Person> persons = new List<Person>();
    4
    5 public Form3()
    6 {
    7 InitializeComponent();
    8 }
    9
    10 private void Form3_Load(object sender, EventArgs e)
    11 {
    12 persons.Add(new Person("小明", ""));
    13 persons.Add(new Person("小红", ""));
    14 persons.Add(new Person("小蓝", ""));
    15 persons.Add(new Person("小李", ""));
    16 DataGridView.DataSource = persons;
    17 }
    18
    19 private void DataGridView_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
    20 {
    21 if (sender is DataGridView)
    22 {
    23 if (e.RowIndex > -1 && e.ColumnIndex > -1) //不能是标题行 或者标题列
    24 {
    25 DataGridView dgv = sender as DataGridView;
    26 dgv[e.ColumnIndex, e.RowIndex].ToolTipText = "姓名:" + persons[e.RowIndex].Name + " 性别:" +persons[e.RowIndex].Sex;
    27 }
    28 }
    29 }
    30 }

    效果图:

    总结:三种方法各有利弊,实际中使用哪种还应该看具体的需求是如何的。

  • 相关阅读:
    Windows Server 2008 IIS7.0 发布html和Asp.net网站
    GS+9.0地统计软件学习
    将Mxd文件压缩并上传到Geodatabase!
    Git代码版本管理
    一台机器,两个Oracle数据库,两个SDE服务!
    翻译:A Picturebox Control to Display Both Remote Sensing and Regular Digital Images
    ENVI处理Modis数据学习
    C#开源资源
    winform中ToString()、DateTime.ToString()、DateTime
    加速Vs2008
  • 原文地址:https://www.cnblogs.com/Johnny_Z/p/2149895.html
Copyright © 2011-2022 走看看