惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Help Net Security
Help Net Security
G
Google Developers Blog
雷峰网
雷峰网
WordPress大学
WordPress大学
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Engineering at Meta
Engineering at Meta
Security Latest
Security Latest
T
Threat Research - Cisco Blogs
AWS News Blog
AWS News Blog
F
Full Disclosure
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Exploit Database - CXSecurity.com
J
Java Code Geeks
U
Unit 42
C
Cyber Attacks, Cyber Crime and Cyber Security
V
V2EX
C
Cisco Blogs
博客园 - 司徒正美
Project Zero
Project Zero
L
LINUX DO - 热门话题
阮一峰的网络日志
阮一峰的网络日志
Blog — PlanetScale
Blog — PlanetScale
Scott Helme
Scott Helme
A
About on SuperTechFans
Hugging Face - Blog
Hugging Face - Blog
S
Securelist
小众软件
小众软件
aimingoo的专栏
aimingoo的专栏
S
Schneier on Security
G
GRAHAM CLULEY
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyberwarzone
Cyberwarzone
MongoDB | Blog
MongoDB | Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 叶小钗
T
Threatpost
Recorded Future
Recorded Future
C
CXSECURITY Database RSS Feed - CXSecurity.com
宝玉的分享
宝玉的分享
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
The Register - Security
The Register - Security
S
Security Archives - TechRepublic
博客园 - Franky
N
News | PayPal Newsroom
Simon Willison's Weblog
Simon Willison's Weblog
S
SegmentFault 最新的问题
W
WeLiveSecurity
A
Arctic Wolf
B
Blog

博客园 - 马啸西风

SQL Server 2008之XML数据存储 SQL Server 2008之托管代码 SQL Server 2008之触发器 SQL Server 2008之约束 SQL Server 2008之用户自定义函数 SQL Server 2008之错误处理 SQL Server 2008之创建高并发应用程序 SQL Server 2008之合并数据和表传递 SQL Server 2008之存储过程的设计和实现 SQL Server 2008之通过非聚集索引提高性能 SQL Server 2008之读取查询计划 SQL Server 2008之表结构实现 SQL Server 2008之索引设计 SQL Server 2008之视图的设计和实现 SQL Server 2008之表的设计和实现 SQL Server 2008之数据类型 启用Application library caching时,将多个不同步的程序集打包到一个ZIP包中的问题 垃圾收集导致的概率性发生的bug C#之集合
DataGridView中下拉列表框的实现
马啸西风 · 2011-03-07 · via 博客园 - 马啸西风

      最近一直在北京找工作,上网不是特别方便,也没有时间更新自己的博客。今天就来谈谈DataGridView里怎么更好的实现下拉列表吧!

      方式一:如下图所示,该方式也是较为简单的一种。

    

     你只需要添加一列类型为DataGridViewComboBoxColumn的列,然后添加数据源即可。但是我们看到这种方式的下拉列表看起来并不是十分的美观,至少我个人是这么觉得的。

     方式二:如下图所示。

     

      如上所示,这样只有在每次点击特定列的单元格时,才会显示下拉列表。其实原理也非常简单,只需要在选择DataGridView的单元格时,判断是不是要显示下拉列表的列,如果是的话,将下拉列表显示在点击的单元格内,对原有的单元格进行覆盖。如果不是就隐藏下拉列表。当然细节部分请参考代码。

      现将部分代码贴出来供大家参考。

//将下拉列表加入到DataGridView的控件集合内,否则下拉列表不会显示在你点击的单元格上 
dataGridView1.Controls.Add(comboBox1);

      然后监听DataGridView的CurrentCellChanged事件,执行如下的动作:

View Code

 1  private void dataGridView1_CurrentCellChanged(object sender, EventArgs e)
 2         {
 3             DataGridViewColumn column = dataGridView1.CurrentCell.OwningColumn;
 4             //如果是要显示下拉列表的列的话
 5             if (column.Name.Equals("Column2"))
 6             {
 7                 int columnIndex = dataGridView1.CurrentCell.ColumnIndex;
 8                 int rowIndex = dataGridView1.CurrentCell.RowIndex;
 9                 Rectangle rect = dataGridView1.GetCellDisplayRectangle(columnIndex, rowIndex, false);
10                 comboBox1.Left = rect.Left;
11                 comboBox1.Top = rect.Top;
12                 comboBox1.Width = rect.Width;
13                 comboBox1.Height = rect.Height;
14                 //将单元格的内容显示为下拉列表的当前项
15                 string consultingRoom = dataGridView1.Rows[rowIndex].Cells[columnIndex].Value.ToString();
16                 int index = comboBox1.Items.IndexOf(consultingRoom);
17 
18                 comboBox1.SelectedIndex = index;
19                 comboBox1.Visible = true;
20             }
21             else
22             {
23                 comboBox1.Visible = false;
24             }
25         }

       然后重绘下拉列表的每一项,如果不重绘的话,看不到下拉列表的每一项的内容,但是神奇的是可以选择(因为下拉列表此时是有内容的,只不过是没有显示出来)

View Code

1 private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
2         {
3             e.DrawBackground();
4             e.Graphics.DrawString(comboBox1.Items[e.Index].ToString(), e.Font, Brushes.Black,
5                 e.Bounds, StringFormat.GenericDefault);
6         }

      最后在下拉列表选择项变化的时候,更改DataGridView相应的单元格的内容

View Code

1 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
2         {
3             if (dataGridView1.CurrentCell != null)
4                 dataGridView1.CurrentCell.Value = comboBox1.Items[comboBox1.SelectedIndex];
5         }