可以让DataGridView中多个ComboBox级联的工具类-初版
RelateComboBoxHelper
/// <summary>
/// Author:武广敬
/// Blog:tonyepaper.cnblogs.com
/// 级联菜单帮助类
/// </summary>
public class RelateComboBoxHelper
{
//未筛选的数据源
private static BindingSource[] _unFilterBS = new BindingSource[10];
//筛选后的数据源
private static BindingSource[] _filterBS = new BindingSource[10];
//级联主选单
private static string[] _sourceComboBoxName;
//级联选单
private static string[] _filterComboBoxName;
//Ex:"GoodsNo='{0}'"
private static string[] _filterExpression;
/// <summary>
/// 处理有级联的DataGridViewComboBoxColumn,使从属DataGridViewComboBoxColumn
/// 可以根据父DataGridViewComboBoxColumn的值改变自身的数据源
/// </summary>
/// <param name="dataGridView">需要处理级联的DataGridView</param>
/// <param name="sourceComboBoxName">级联父DataGridViewComboBoxColumn</param>
/// <param name="filterComboBoxName">级联子DataGridViewComboBoxColumn</param>
/// <param name="dataSource">级联子DataGridViewComboBoxColumn的数据源DataTable</param>
/// <param name="filterExpression">级联子DataGridViewComboBoxColumn根据父DataGridViewComboBoxColumn改变的筛选表达式 Ex:"GoodsNo='{0}'"</param>
public static void BuildComboBox(DataGridView dataGridView, string sourceComboBoxName, string filterComboBoxName, DataTable dataSource, string filterExpression)
{
BuildComboBox(dataGridView, new string[] { sourceComboBoxName }, new string[] { filterComboBoxName }, new DataTable[] { dataSource }, new string[] { filterExpression });
}
/// <summary>
/// 处理有级联的DataGridViewComboBoxColumn,使从属DataGridViewComboBoxColumn(使用下拉选单本身的数据源)
/// 可以根据父DataGridViewComboBoxColumn的值改变自身的数据源
/// </summary>
/// <param name="dataGridView">需要处理级联的DataGridView</param>
/// <param name="sourceComboBoxName">级联父DataGridViewComboBoxColumn</param>
/// <param name="filterComboBoxName">级联子DataGridViewComboBoxColumn</param>
/// <param name="filterExpression">级联子DataGridViewComboBoxColumn根据父DataGridViewComboBoxColumn改变的筛选表达式 Ex:"GoodsNo='{0}'"</param>
public static void BuildComboBox(DataGridView dataGridView, string sourceComboBoxName, string filterComboBoxName, string filterExpression)
{
BuildComboBox(dataGridView, new string[] { sourceComboBoxName }, new string[] { filterComboBoxName }, new DataTable[] { (dataGridView.Columns[filterComboBoxName] as DataGridViewComboBoxColumn).DataSource as DataTable }, new string[] { filterExpression });
}
/// <summary>
/// 处理多个级联的下拉选单
/// </summary>
/// <param name="dataGridView">需要处理级联的DataGridView</param>
/// <param name="sourceComboBoxName">级联父DataGridViewComboBoxColumn</param>
/// <param name="filterComboBoxName">级联子DataGridViewComboBoxColumn</param>
/// <param name="dataSource">级联子DataGridViewComboBoxColumn的数据源DataTable</param>
/// <param name="filterExpression">级联子DataGridViewComboBoxColumn根据父DataGridViewComboBoxColumn改变的筛选表达式 Ex:"GoodsNo='{0}'"</param>
public static void BuildComboBox(DataGridView dataGridView, string[] sourceComboBoxName, string[] filterComboBoxName, DataTable[] dataSource, string[] filterExpression)
{
try
{
dataGridView.CellBeginEdit += new DataGridViewCellCancelEventHandler(dataGridView_CellBeginEdit);
dataGridView.CellEndEdit += new DataGridViewCellEventHandler(dataGridView_CellEndEdit);
for (int i = 0; i < sourceComboBoxName.Length; i++)
{
_unFilterBS[i] = new BindingSource();
_unFilterBS[i].DataSource = dataSource[i];
_filterBS[i] = new BindingSource();
_filterBS[i].DataSource = dataSource[i];
(dataGridView.Columns[filterComboBoxName[i]] as DataGridViewComboBoxColumn).DataSource = _unFilterBS[i];
}
_filterComboBoxName = filterComboBoxName;
_sourceComboBoxName = sourceComboBoxName;
_filterExpression = filterExpression;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 处理多个级联的下拉选单(使用下拉选单本身的数据源)
/// </summary>
/// <param name="dataGridView">需要处理级联的DataGridView</param>
/// <param name="sourceComboBoxName">级联父DataGridViewComboBoxColumn</param>
/// <param name="filterComboBoxName">级联子DataGridViewComboBoxColumn</param>
/// <param name="filterExpression">级联子DataGridViewComboBoxColumn根据父DataGridViewComboBoxColumn改变的筛选表达式 Ex:"GoodsNo='{0}'"</param>
public static void BuildComboBox(DataGridView dataGridView, string[] sourceComboBoxName, string[] filterComboBoxName, string[] filterExpression)
{
DataTable[] datasource = new DataTable[filterComboBoxName.Length];
for (int i = 0; i < sourceComboBoxName.Length; i++)
{
datasource[i] = (dataGridView.Columns[filterComboBoxName[i]] as DataGridViewComboBoxColumn).DataSource as DataTable;
}
BuildComboBox(dataGridView, sourceComboBoxName, filterComboBoxName, datasource, filterExpression);
}
static void dataGridView_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
DataGridView datagridView = sender as DataGridView;
try
{
for (int i = 0; i < _sourceComboBoxName.Length; i++)
{
if (e.ColumnIndex == datagridView.Columns[_filterComboBoxName[i]].Index)
{
DataGridViewComboBoxCell filterComboBox = datagridView.Rows[e.RowIndex].Cells[e.ColumnIndex] as DataGridViewComboBoxCell;
filterComboBox.DataSource = _filterBS[i];
DataGridViewComboBoxCell sourceComboBox = datagridView.Rows[e.RowIndex].Cells[_sourceComboBoxName[i]] as DataGridViewComboBoxCell;
_filterBS[i].Filter = string.Format(_filterExpression[i], sourceComboBox.FormattedValue);
}
}
}
catch (Exception ex)
{
throw ex;
}
}
static void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
DataGridView datagridView = sender as DataGridView;
try
{
for (int i = 0; i < _sourceComboBoxName.Length; i++)
{
if (e.ColumnIndex == datagridView.Columns[_filterComboBoxName[i]].Index)
{
DataGridViewComboBoxCell filterComboBox = datagridView.Rows[e.RowIndex].Cells[e.ColumnIndex] as DataGridViewComboBoxCell;
filterComboBox.DataSource = _unFilterBS[i];
_filterBS[i].RemoveFilter();
}
}
}
catch (Exception ex)
{
throw ex;
}
}
}
使用方法
/// <summary>
/// Author:武广敬
/// Blog:tonyepaper.cnblogs.com
/// 级联菜单帮助类
/// </summary>
public class RelateComboBoxHelper
{
//未筛选的数据源
private static BindingSource[] _unFilterBS = new BindingSource[10];
//筛选后的数据源
private static BindingSource[] _filterBS = new BindingSource[10];
//级联主选单
private static string[] _sourceComboBoxName;
//级联选单
private static string[] _filterComboBoxName;
//Ex:"GoodsNo='{0}'"
private static string[] _filterExpression;
/// <summary>
/// 处理有级联的DataGridViewComboBoxColumn,使从属DataGridViewComboBoxColumn
/// 可以根据父DataGridViewComboBoxColumn的值改变自身的数据源
/// </summary>
/// <param name="dataGridView">需要处理级联的DataGridView</param>
/// <param name="sourceComboBoxName">级联父DataGridViewComboBoxColumn</param>
/// <param name="filterComboBoxName">级联子DataGridViewComboBoxColumn</param>
/// <param name="dataSource">级联子DataGridViewComboBoxColumn的数据源DataTable</param>
/// <param name="filterExpression">级联子DataGridViewComboBoxColumn根据父DataGridViewComboBoxColumn改变的筛选表达式 Ex:"GoodsNo='{0}'"</param>
public static void BuildComboBox(DataGridView dataGridView, string sourceComboBoxName, string filterComboBoxName, DataTable dataSource, string filterExpression)
{
BuildComboBox(dataGridView, new string[] { sourceComboBoxName }, new string[] { filterComboBoxName }, new DataTable[] { dataSource }, new string[] { filterExpression });
}
/// <summary>
/// 处理有级联的DataGridViewComboBoxColumn,使从属DataGridViewComboBoxColumn(使用下拉选单本身的数据源)
/// 可以根据父DataGridViewComboBoxColumn的值改变自身的数据源
/// </summary>
/// <param name="dataGridView">需要处理级联的DataGridView</param>
/// <param name="sourceComboBoxName">级联父DataGridViewComboBoxColumn</param>
/// <param name="filterComboBoxName">级联子DataGridViewComboBoxColumn</param>
/// <param name="filterExpression">级联子DataGridViewComboBoxColumn根据父DataGridViewComboBoxColumn改变的筛选表达式 Ex:"GoodsNo='{0}'"</param>
public static void BuildComboBox(DataGridView dataGridView, string sourceComboBoxName, string filterComboBoxName, string filterExpression)
{
BuildComboBox(dataGridView, new string[] { sourceComboBoxName }, new string[] { filterComboBoxName }, new DataTable[] { (dataGridView.Columns[filterComboBoxName] as DataGridViewComboBoxColumn).DataSource as DataTable }, new string[] { filterExpression });
}
/// <summary>
/// 处理多个级联的下拉选单
/// </summary>
/// <param name="dataGridView">需要处理级联的DataGridView</param>
/// <param name="sourceComboBoxName">级联父DataGridViewComboBoxColumn</param>
/// <param name="filterComboBoxName">级联子DataGridViewComboBoxColumn</param>
/// <param name="dataSource">级联子DataGridViewComboBoxColumn的数据源DataTable</param>
/// <param name="filterExpression">级联子DataGridViewComboBoxColumn根据父DataGridViewComboBoxColumn改变的筛选表达式 Ex:"GoodsNo='{0}'"</param>
public static void BuildComboBox(DataGridView dataGridView, string[] sourceComboBoxName, string[] filterComboBoxName, DataTable[] dataSource, string[] filterExpression)
{
try
{
dataGridView.CellBeginEdit += new DataGridViewCellCancelEventHandler(dataGridView_CellBeginEdit);
dataGridView.CellEndEdit += new DataGridViewCellEventHandler(dataGridView_CellEndEdit);
for (int i = 0; i < sourceComboBoxName.Length; i++)
{
_unFilterBS[i] = new BindingSource();
_unFilterBS[i].DataSource = dataSource[i];
_filterBS[i] = new BindingSource();
_filterBS[i].DataSource = dataSource[i];
(dataGridView.Columns[filterComboBoxName[i]] as DataGridViewComboBoxColumn).DataSource = _unFilterBS[i];
}
_filterComboBoxName = filterComboBoxName;
_sourceComboBoxName = sourceComboBoxName;
_filterExpression = filterExpression;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 处理多个级联的下拉选单(使用下拉选单本身的数据源)
/// </summary>
/// <param name="dataGridView">需要处理级联的DataGridView</param>
/// <param name="sourceComboBoxName">级联父DataGridViewComboBoxColumn</param>
/// <param name="filterComboBoxName">级联子DataGridViewComboBoxColumn</param>
/// <param name="filterExpression">级联子DataGridViewComboBoxColumn根据父DataGridViewComboBoxColumn改变的筛选表达式 Ex:"GoodsNo='{0}'"</param>
public static void BuildComboBox(DataGridView dataGridView, string[] sourceComboBoxName, string[] filterComboBoxName, string[] filterExpression)
{
DataTable[] datasource = new DataTable[filterComboBoxName.Length];
for (int i = 0; i < sourceComboBoxName.Length; i++)
{
datasource[i] = (dataGridView.Columns[filterComboBoxName[i]] as DataGridViewComboBoxColumn).DataSource as DataTable;
}
BuildComboBox(dataGridView, sourceComboBoxName, filterComboBoxName, datasource, filterExpression);
}
static void dataGridView_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
DataGridView datagridView = sender as DataGridView;
try
{
for (int i = 0; i < _sourceComboBoxName.Length; i++)
{
if (e.ColumnIndex == datagridView.Columns[_filterComboBoxName[i]].Index)
{
DataGridViewComboBoxCell filterComboBox = datagridView.Rows[e.RowIndex].Cells[e.ColumnIndex] as DataGridViewComboBoxCell;
filterComboBox.DataSource = _filterBS[i];
DataGridViewComboBoxCell sourceComboBox = datagridView.Rows[e.RowIndex].Cells[_sourceComboBoxName[i]] as DataGridViewComboBoxCell;
_filterBS[i].Filter = string.Format(_filterExpression[i], sourceComboBox.FormattedValue);
}
}
}
catch (Exception ex)
{
throw ex;
}
}
static void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
DataGridView datagridView = sender as DataGridView;
try
{
for (int i = 0; i < _sourceComboBoxName.Length; i++)
{
if (e.ColumnIndex == datagridView.Columns[_filterComboBoxName[i]].Index)
{
DataGridViewComboBoxCell filterComboBox = datagridView.Rows[e.RowIndex].Cells[e.ColumnIndex] as DataGridViewComboBoxCell;
filterComboBox.DataSource = _unFilterBS[i];
_filterBS[i].RemoveFilter();
}
}
}
catch (Exception ex)
{
throw ex;
}
}
}
Code
RelateComboBoxHelper.BuildComboBox(DataGridViewName, "级联父列名-如部门", "级联子列名-如部门成员", 所有部门成员数据的DataTable(需要包含[DeptNo列]),"子列名根据父列名筛选内容的筛选字串DeptNo='{0}'");
RelateComboBoxHelper.BuildComboBox(DataGridViewName, "级联父列名-如部门", "级联子列名-如部门成员", 所有部门成员数据的DataTable(需要包含[DeptNo列]),"子列名根据父列名筛选内容的筛选字串DeptNo='{0}'");
---------------------------------------------------------------------
每个人都是一座山.世上最难攀越的山,其实是自己.往上走,即便一小步,也有新高度.
每个人都是一座山.世上最难攀越的山,其实是自己.往上走,即便一小步,也有新高度.
--做最好的自己,我能!!!