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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - iCeSnaker

[转载]两个未公开的ACCESS方法的使用技巧 Microsoft SQL Reporting Services – Running a Report from the Command Line Microsoft AntiSpyware Beta1 发布了,贴几张图片上来 大型组图:微软总部印象 两个桌面主题 [讨论] 制作博客园年刊 用C#实现生成PDF文档 C# Delegate 简介 单元测试的基本方法 小软件项目开发的管理 用C#读取XML文档 什么是需求? 呐喊 -- 希望博客圆一直是世外桃源 将sql server中的数据倒入Excel(c#) 在.net中轻松掌握Windows窗体间的数据交互(三) 在.net中轻松掌握Windows窗体间的数据交互(二) 在.net中轻松掌握Windows窗体间的数据交互(一) C# 编码规范和编程好习惯 C#实现的基本算法
C#中为DataGrid添加下拉列表框
iCeSnaker · 2004-09-21 · via 博客园 - iCeSnaker

本文将介绍如何在 System.Windows.Forms.DataGrid中切入使用ComboBox控件,主要包括三方面的内容。

  1. 在DataGrid中加入ComboBox列;

  2. 把在DataGrid中的修改保存到对应的网格;

   3. 设置DataGrid中网格的焦点。

  下面是整个源代码,一些功能可以看注释。

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace DataGridTest
{
 
public class Form1 : System.Windows.Forms.Form
 
{
  
private System.Windows.Forms.DataGrid dgdFunctionArea;
  
private DataTable dtblFunctionalArea;
  
private System.Windows.Forms.Button buttonFocus;
  
private System.ComponentModel.Container components = null;

  
public Form1()
  
{
   InitializeComponent();
   PopulateGrid();
  }


  
protected override void Dispose( bool disposing )
  
{
   
if( disposing )
   
{
    
if (components != null
    
{
     components.Dispose();
    }

   }

   
base.Dispose( disposing );
  }


  
Windows 窗体设计器生成的代码
  
/// <summary>
  
/// 应用程序的主入口点。
  
/// </summary>

  [STAThread]
  
static void Main() 
  
{
   Application.Run(
new Form1());
  }

  
//初始化DataGrid
  private void PopulateGrid()
  
{
   
//创建一个DataTable对象,包括四列,前三列为String,最后一列为Boolean。
   dtblFunctionalArea = new DataTable ("FunctionArea");
   
string[] arrstrFunctionalArea = new string [3]{"Functional Area","Min","Max"};
   DataColumn dtCol 
= null;
   
//创建String列 
   for(int i=0; i< 3;i++)
   

    dtCol 
= new DataColumn(arrstrFunctionalArea[i]);
    dtCol.DataType 
= Type.GetType("System.String");
    dtCol.DefaultValue 
= "";
    dtblFunctionalArea.Columns.Add(dtCol); 
   }
 

   
//创建Boolean列,用CheckedBox来显示。 
   DataColumn dtcCheck = new DataColumn("IsMandatory");
   dtcCheck.DataType 
= System.Type.GetType("System.Boolean");
   dtcCheck.DefaultValue 
= false;
   dtblFunctionalArea.Columns.Add(dtcCheck);

   
//把表绑定到DataGrid
   dgdFunctionArea.DataSource = dtblFunctionalArea; 

   
//为DataGrid加载DataGridTableStyle样式
   if(!dgdFunctionArea.TableStyles.Contains("FunctionArea"))
   
{
    DataGridTableStyle dgdtblStyle 
= new DataGridTableStyle();
    dgdtblStyle.MappingName 
= dtblFunctionalArea.TableName;
    dgdFunctionArea.TableStyles.Add(dgdtblStyle);
    dgdtblStyle.RowHeadersVisible 
= false;
    dgdtblStyle.HeaderBackColor 
= Color.LightSteelBlue;
    dgdtblStyle.AllowSorting 
= false;
    dgdtblStyle.HeaderBackColor 
= Color.FromArgb(8,36,107);
    dgdtblStyle.RowHeadersVisible 
= false;
    dgdtblStyle.HeaderForeColor 
= Color.White;
    dgdtblStyle.HeaderFont 
= new System.Drawing.Font("Microsoft Sans Serif", 9F, 
    System.Drawing.FontStyle.Bold, 
    System.Drawing.GraphicsUnit.Point, ((System.Byte)(
0)));
    dgdtblStyle.GridLineColor 
= Color.DarkGray;
    dgdtblStyle.PreferredRowHeight 
= 22;
    dgdFunctionArea.BackgroundColor 
= Color.White; 

    
//设置列的宽度 
    GridColumnStylesCollection colStyle = dgdFunctionArea.TableStyles[0].GridColumnStyles;
    colStyle[
0].Width = 100;
    colStyle[
1].Width = 50;
    colStyle[
2].Width = 50;
    colStyle[
3].Width = 80;
   }


   DataGridTextBoxColumn dgtb 
= (DataGridTextBoxColumn)dgdFunctionArea.TableStyles[0].GridColumnStyles[0]; 

   ComboBox cmbFunctionArea 
= new ComboBox();
   cmbFunctionArea.Items.AddRange(
new object[]{"选项一","选项二","选项三"});
   cmbFunctionArea.Cursor 
= Cursors.Arrow;
   cmbFunctionArea.DropDownStyle
= ComboBoxStyle.DropDownList;
   cmbFunctionArea.Dock 
= DockStyle.Fill;

   
//在选定项发生更改并且提交了该更改后发生

   cmbFunctionArea.SelectionChangeCommitted 
+= new  EventHandler(cmbFunctionArea_SelectionChangeCommitted); 

   
//把ComboBox添加到DataGridTableStyle的第一列

   dgtb.TextBox.Controls.Add(cmbFunctionArea); 

  }


  
//设置焦点模拟

  
private void GetFocus(int row,int col)
  
{
   
//先把焦点移动到DataGrid
   this.dgdFunctionArea.Focus(); 
   
//把焦点移动到DataGridCell
   DataGridCell dgc = new DataGridCell(row,col); 
   
this.dgdFunctionArea.CurrentCell = dgc; 
   DataGridTextBoxColumn dgtb 
= (DataGridTextBoxColumn)dgdFunctionArea.TableStyles[0].GridColumnStyles[col]; 

   
//设置焦点

   dgtb.TextBox.Focus();
  }
 

  
//把Combobox上修改的数据提交到当前的网格

 
private void cmbFunctionArea_SelectionChangeCommitted(object sender, EventArgs e)
 
{
  
this.dgdFunctionArea[this.dgdFunctionArea.CurrentCell] = ((ComboBox)sender).SelectedItem.ToString();

 }
 

 
//设置新的焦点

 
private void buttonFocus_Click(object sender, System.EventArgs e)
 
{
  
//焦点模拟,这里设置第三行第一列
  GetFocus(2,0);
 }

}


}
 

下面是测试界面:

总结,这里是通过DataGridTextBoxColumn.TextBox.Controls.Add方法实现在列中添加ComboBox控件;对于数据的保存是使用ComboBox.SelectionChangeCommitted事件来完成;设置焦点是通过DataGridTextBoxColumn.TextBox.Focus方法来实现。另外通过这个方法也可以添加DateTimePicker等类似的控件。