c#中为datagrid添加下拉列表框_c#应用
2008-02-23 05:44:35来源:互联网 阅读 ()
本文将介绍如何在 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 );
}
#region Windows 窗体设计器生成的代码
private void InitializeComponent()
{
this.dgdFunctionArea = new System.Windows.Forms.DataGrid();
this.buttonFocus = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dgdFunctionArea)).BeginInit();
this.SuspendLayout();
//
// dgdFunctionArea
//
this.dgdFunctionArea.DataMember = "";
this.dgdFunctionArea.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dgdFunctionArea.Location = new System.Drawing.Point(4, 8);
this.dgdFunctionArea.Name = "dgdFunctionArea";
this.dgdFunctionArea.Size = new System.Drawing.Size(316, 168);
this.dgdFunctionArea.TabIndex = 0;
//
// buttonFocus
//
this.buttonFocus.Location = new System.Drawing.Point(232, 188);
this.buttonFocus.Name = "buttonFocus";
this.buttonFocus.Size = new System.Drawing.Size(84, 23);
this.buttonFocus.TabIndex = 1;
this.buttonFocus.Text = "获取焦点";
this.buttonFocus.Click = new System.EventHandler(this.buttonFocus_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(332, 217);
this.Controls.Add(this.buttonFocus);
this.Controls.Add(this.dgdFunctionArea);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.dgdFunctionArea)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <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;
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash