开发者论坛

 找回密码
 注册 (请使用非IE浏览器)
查看: 7605|回复: 13

[源码] DataGridView几个高级方法

[复制链接]

0

精华

100

贡献

33

赞扬

帖子
132
软币
3320
在线时间
53 小时
注册时间
2013-8-31
发表于 2013-9-2 21:57:17 | 显示全部楼层 |阅读模式
格行换色功能
添加DataBindingComplete事件
  private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
            {
                DataGridViewRow row = this.dataGridView1.Rows[i];
                if ((i % 2) != 0)
                {
                    row.DefaultCellStyle.BackColor = Color.Gainsboro;
                }
            }
        }

显示行数据信息功能
添加RowEnter和RowLeave事件
private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                for (int i = 0; i < this.dataGridView1.Rows[e.RowIndex].Cells.Count; i++)
                {
                    StringBuilder builder = new StringBuilder();
                    builder.AppendFormat("行数据基本信息", new object[0]);
                    for (int j = 0; j < this.dataGridView1.Rows[e.RowIndex].Cells.Count; j++)
                    {
                        if (this.dataGridView1.Columns[j].Visible)
                        {
                            DataGridViewCell cell = this.dataGridView1.Rows[e.RowIndex].Cells[j];
                            builder.AppendFormat("    {0}:{1}\r\n", this.dataGridView1.Columns[j].HeaderText, cell.Value);
                        }
                    }
                    this.dataGridView1[i, e.RowIndex].ToolTipText = builder.ToString();
                }
            }
            catch
            {
            }
        }

private void dataGridView1_RowLeave(object sender, DataGridViewCellEventArgs e)
        {
            for (int i = 0; i < this.dataGridView1.Rows[e.RowIndex].Cells.Count; i++)
            {
                this.dataGridView1[i, e.RowIndex].ToolTipText = string.Empty;
            }
        }

评分

参与人数 4赞扬 +4 收起 理由
enter8514 + 1 很给力
海上孤舟 + 1 感谢分享
rakehell + 1 感谢分享
ibm2000 + 1 Thanks

查看全部评分

回复

使用道具 举报

0

精华

0

贡献

0

赞扬

帖子
2
软币
72
在线时间
5 小时
注册时间
2013-9-24
发表于 2013-9-25 10:39:35 | 显示全部楼层
good~謝謝分享~
回复

使用道具 举报

0

精华

0

贡献

6

赞扬

帖子
37
软币
287
在线时间
28 小时
注册时间
2013-11-19
发表于 2014-1-11 23:28:27 | 显示全部楼层
good~謝謝分享~
回复

使用道具 举报

0

精华

2

贡献

12

赞扬

帖子
29
软币
235
在线时间
16 小时
注册时间
2014-5-20
发表于 2014-5-20 16:19:01 | 显示全部楼层
谢谢楼主了
回复

使用道具 举报

0

精华

0

贡献

0

赞扬

帖子
6
软币
62
在线时间
0 小时
注册时间
2014-5-23
发表于 2014-5-23 17:06:14 | 显示全部楼层
谢谢谢,不错的高级高级方法
回复

使用道具 举报

0

精华

0

贡献

0

赞扬

帖子
14
软币
109
在线时间
4 小时
注册时间
2014-6-13
发表于 2014-6-13 15:41:38 | 显示全部楼层
感谢分享,学习提高
回复

使用道具 举报

0

精华

0

贡献

0

赞扬

帖子
3
软币
58
在线时间
0 小时
注册时间
2015-5-23
发表于 2015-5-23 11:15:39 | 显示全部楼层
隔行换色其实有更简单的办法,datagridview 有奇数行的属性,所以这样设置就行。
dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.AliceBlue;//奇数列
回复

使用道具 举报

0

精华

0

贡献

0

赞扬

帖子
3
软币
68
在线时间
3 小时
注册时间
2015-5-23
发表于 2015-5-23 16:31:08 | 显示全部楼层
bucuo 谢谢分享啊
回复

使用道具 举报

0

精华

0

贡献

0

赞扬

帖子
23
软币
104
在线时间
9 小时
注册时间
2017-7-1
发表于 2017-7-1 21:32:47 | 显示全部楼层
good~謝謝分享~
回复

使用道具 举报

0

精华

0

贡献

0

赞扬

帖子
13
软币
108
在线时间
4 小时
注册时间
2019-9-7
发表于 2019-11-15 10:45:44 | 显示全部楼层
不错,学习中,谢谢分享
回复

使用道具 举报

头像被屏蔽

0

精华

0

贡献

30

赞扬

禁止发言

帖子
5
软币
211
在线时间
12 小时
注册时间
2019-7-11
发表于 2019-12-19 10:40:10 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
回复

使用道具 举报

Archiver|手机版|小黑屋|开发者网 ( 苏ICP备08004430号-2 )
版权所有:南京韵文教育信息咨询有限公司

GMT+8, 2024-4-27 02:39

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表