教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 精品文档 > 政务民生 >

下拉列表中实现绑定多个数据字段(完整版)

来源:网络收集 时间:2026-10-02
导读: 点击下拉列表时,实现在下拉列表中显示多个数据表字段的代码,MutilColumnCombobox // Most of the important code for this control was // taken from a control written by Nishant Sivakumar. // http://doc.guandang.net/cs/combobox/DotNetMultiColumnCo

点击下拉列表时,实现在下拉列表中显示多个数据表字段的代码,MutilColumnCombobox

// Most of the important code for this control was
// taken from a control written by Nishant Sivakumar.
// http://doc.guandang.net/cs/combobox/DotNetMultiColumnComboBox.asp
//
// Bugfixes or Suggestions can be sent to dcaillouet@http://doc.guandang.net
//显示多列combobox,设置AutoComplete=true;AutoDropDown=true;

using System;
using System.Windows.Forms;
using System.Collections;
using System.Collections.ObjectModel;
using http://doc.guandang.netponentModel;
using System.Drawing;
using System.Globalization;

namespace MultiColumnComboBoxDemo
{
public class MultiColumnComboBox : ComboBox
{
private bool _AutoComplete;
private bool _AutoDropdown;
private Color _BackColorEven = Color.White;
private Color _BackColorOdd = Color.White;
private string _ColumnNameString = "";
private int _ColumnWidthDefault = 75;
private string _ColumnWidthString = "";
private int _LinkedColumnIndex;
private TextBox _LinkedTextBox;
private int _TotalWidth = 0;
private int _ValueMemberColumnIndex = 0;

private Collection<string> _ColumnNames = new Collection<string>();
private Collection<int> _ColumnWidths = new Collection<int>();

public MultiColumnComboBox()
{
DrawMode = DrawMode.OwnerDrawVariable;

// If all of your boxes will be RightToLeft, uncomment
// the following line to make RTL the default.
//RightToLeft = RightToLeft.Yes;

// Remove the Context Menu to disable pasting
ContextMenu = new ContextMenu();
}

public event System.EventHandler OpenSearchForm;

public bool AutoComplete
{
get
{
return _AutoComplete;
}
set
{
_AutoComplete = value;
}
}

public bool AutoDropdown
{
get
{
return _AutoDropdown;
}
set
{
_AutoDropdown = value;
}
}

public Color BackColorEven
{
get
{
return _BackColorEven;
}
set
{
_BackColorEven = value;
}
}

public Color BackColorOdd
{
get
{
return _BackColorOdd;
}
set
{
_BackColorOdd = value;
}
}

public Collection<string>
ColumnNameCollection
{
get
{
return _ColumnNames;
}
}

public string ColumnNames
{
get
{
return _ColumnNameString;
}

set
{
//

点击下拉列表时,实现在下拉列表中显示多个数据表字段的代码,MutilColumnCombobox

If the column string is blank, leave it blank.
// The default width will be used for all columns.
if (!Convert.ToBoolean(value.Trim().Length))
{
_ColumnNameString = "";
}
else if (value != null)
{
char[] delimiterChars = { ',', ';', ':' };
string[] columnNames = value.Split(delimiterChars);

if (!DesignMode)
{
_ColumnNames.Clear();
}

// After splitting the string into an array, iterate
// through the strings and check that they're all valid.
foreach (string s in columnNames)
{
// Does it have length?
if (Convert.ToBoolean(s.Trim().Length))
{
if (!DesignMode)
{
_ColumnNames.Add(s.Trim());
}
}
else // The value is blank
{
throw new NotSupportedException("Column names can not be blank.");
}
}
_ColumnNameString = value;
}
}
}

public Collection<int> ColumnWidthCollection
{
get
{
return _ColumnWidths;
}
}

public int ColumnWidthDefault
{
get
{
return _ColumnWidthDefault;
}
set
{
_ColumnWidthDefault = value;
}
}

public string ColumnWidths
{
get
{
return _ColumnWidthString;
}

set
{
// If the column string is blank, leave it blank.
// The default width will be used for all columns.
if (!Convert.ToBoolean(value.Trim().Length))
{
_ColumnWidthString = "";
}
else if (value != null)
{
char[] delimiterChars = { ',', ';', ':' };
string[] columnWidths = value.Split(delimiterChars);
string invalidValue = "";
int invalidIndex = -1;
int idx = 1;
int intValue;

// After splitting the str
ing into an array, iterate
// through the strings and check that they're all integers
// or blanks
foreach (string s in columnWidths)
{
//

点击下拉列表时,实现在下拉列表中显示多个数据表字段的代码,MutilColumnCombobox

If it has length, test if it's an integer
if (Convert.ToBoolean(s.Trim().Length))
{
// It's not an integer. Flag the offending value.
if (!int.TryParse(s, out intValue))
{
invalidIndex = idx;
invalidValue = s;
}
else // The value was okay. Increment the item index.
{
idx++;
}
}
else // The value is a space. Use the default width.
{
idx++;
}
}

// If an invalid value was found, raise an exception.
if (invalidIndex > -1)
{
string errMsg;

errMsg = "Invalid column width '" + invalidValue + "' located at column " + invalidIndex.ToString();
throw new ArgumentOutOfRangeException(errMsg);
}
else // The string is fine
{
_ColumnWidthString = value;

// Only set the values of the collections at runtime.
// Setting them at design time doesn't accomplish
// anything and causes errors since the collections
// don't exist at design time.
if (!DesignMode)
{
_ …… 此处隐藏:12003字,全部文档内容请下载后查看。喜欢就下载吧 ……

下拉列表中实现绑定多个数据字段(完整版).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wendang/1441271.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150份
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150份
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)