拥有多列的组合框

 
  
#region 示例 1 -- 拥有多列的组合框

// 初始化DataTable:
//
// 创建一个名为dtTest的数据表,为其添加2列
// ID: int
// Name: string
//
DataTable dtTest = new DataTable();
dtTest.Columns.Add(
" ID " , typeof ( int ));
dtTest.Columns.Add(
" Name " , typeof ( string ));

dtTest.Rows.Add(
1 , " John " );
dtTest.Rows.Add(
2 , " Amy " );
dtTest.Rows.Add(
3 , " Tony " );
dtTest.Rows.Add(
4 , " Bruce " );
dtTest.Rows.Add(
5 , " Allen " );

// 将组合框的数据源设置为DataTable。
this .comboBox1.DataSource = dtTest;
this .comboBox1.DisplayMember = " Name " ;
this .comboBox1.ValueMember = " ID " ;

// 将组合框的 DrawMode 设置为OwnerDrawFixed。
this .comboBox1.DrawMode = DrawMode.OwnerDrawFixed;

// 在DrawItem事件中绘制子项。
this .comboBox1.DrawItem += delegate ( object cmb, DrawItemEventArgs args)
{
// 绘制默认的背景
args.DrawBackground();

// 因为组合框被绑定到DataTable,所以组合框的子项是DataRowView对象。
DataRowView drv = (DataRowView) this .comboBox1.Items[args.Index];

// 取出每一列的值。
string id = drv[ " id " ].ToString();
string name = drv[ " name " ].ToString();

// 获得第一列的边界。
Rectangle r1 = args.Bounds;
r1.Width
/= 2 ;

// 绘制第一列的文本。
using (SolidBrush sb = new SolidBrush(args.ForeColor))
{
args.Graphics.DrawString(id, args.Font, sb, r1);
}

// 绘制一条分割线分割不同的列。
using (Pen p = new Pen(Color.Black))
{
args.Graphics.DrawLine(p, r1.Right,
0 , r1.Right, r1.Bottom);
}

// 获取第二列的边界。
Rectangle r2 = args.Bounds;
r2.X
= args.Bounds.Width / 2 ;
r2.Width
/= 2 ;

// 绘制第二列的文本。
using (SolidBrush sb = new SolidBrush(args.ForeColor))
{
args.Graphics.DrawString(name, args.Font, sb, r2);
}
};

#endregion

 

转载于:https://www.cnblogs.com/qhnokia/archive/2010/11/23/1885500.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值