VS2005,winform中datagridview控件有两个combobox列,两列分别要绑定到不同的数据源。第一列为机构,第二列为人员。现在想实现点击第一列选中机构,第二列从人员表里绑定该机构的所有人员,请各位大侠指点,最好详细点,感激不尽!
解决方案 »
- 江湖救急-->画图问题
- 百度知道的那个回答对我有用的评价问题
- 怎么获得pictureBox1_Click(sender 里的图像?)
- dotnetbar中的advtree控件 怎么控制默认展开的级数 谢谢各位
- 急救~~~~~HttpWebRequst登录https站点(sorry 最后10分了)
- 请问一下一个学校计算机机房的配置方案
- 问弱智的问题,我在from里放了一个浏览器控件,运行后我想点最大化,控件大小也跟form一样大小
- 把c#翻译成c++
- 如何把函数作为另外一个函数参数?
- 如何用c#开发基于web的有关office 的程序?
- 为什么一个按钮必须点2次才执行其onclick事件?
- <100分> 请问一个关于WCF双向通讯超时问题,撒分...
控件里面的有Combobox触发事件可以用吗?不太清楚
当用户选择机构中一条数据,即触发selectchanged事件,
把机构Id取出来,然后根据Id,把Id下的人员查找查找出来,先把员工combox
中以前的数据清空,然后把查找的结果绑定到员工combox中……
供你参考.
先把员工combox
中以前的数据清空,然后把查找的结果绑定到员工combox中……这一步怎么实现的,能不能说的详细点啊。
获取datagridview的人员编号select出来, 在绑定你要的combox
老大,人员列是怎么构建的,我是用后台代码增加的人员列,就是combobox列。可是不能实现这样的绑定呀。
等我一会贴代码。
private void insert_Load(object sender, EventArgs e)
{
DataGridViewCheckBoxColumn zuzhang = new DataGridViewCheckBoxColumn();
zuzhang.HeaderText = "组长";
DataGridViewComboBoxColumn jigou = new DataGridViewComboBoxColumn();
jigou.HeaderText = "机构";
DataGridViewColumn jianchayuan = new DataGridViewColumn();
jianchayuan.HeaderText ="检查员";
dataGridView2.Columns.Add(zuzhang);
dataGridView2.Columns.Add(jigou);
dataGridView2.Columns.Add(jianchayuan);//添加列
dataGridView2.Rows.Add(); SqlConnection conn = new SqlConnection();
conn.ConnectionString = cqchz_mis.Connection.Connstring;
string cmdstr2;
cmdstr2 = "select * from jigou";
conn.Open();
SqlCommand cmd2 = new SqlCommand(cmdstr2, conn);
SqlDataAdapter adpt2 = new SqlDataAdapter();
adpt2.SelectCommand = cmd2;
DataSet ds2 = new DataSet();
adpt2.Fill(ds2, "jigou");
jigou.DisplayMember = "机构名称";
jigou.ValueMember = "机构代码";
jigou.DataSource = ds2.Tables["jigou"];
conn.Close();
}
我想通过datagridveiw的CellValueChanged事件来动态绑定人员列,可我不知道该怎么办了。
private void dataGridView2_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 1)
{
if (e.RowIndex > -1)
{
if (dataGridView2.Rows[e.RowIndex].Cells[1].Value == System.DBNull.Value)
{
return;
}
//如何绑定检查员列? }
}
}
string str查 = "select * from tb_员工 where 部门='" + str部门 + "'"; //根据部门值查询员工信息
sqlcommand com=new sqlcommand (str查);
sqldatareader dr=com.executeReader();
while(dr.read())
{
comboxColumn.items.add(dr[0].tostring())
}
private static BindingSource _unFilterBS = new BindingSource();
//筛选后的数据源
private static BindingSource _filterBS = new BindingSource();在dataGridView_CellBeginEdit事件中写,当需要筛选的下拉选单开始编辑时,使用筛选的B BindingSource,并对B BindingSource进行筛选.这时下拉选单里就只有筛选后的选项了. static void dataGridView_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
DataGridView datagridView = sender as DataGridView; try
{ if (e.ColumnIndex == datagridView.Columns[_filterComboBoxName].Index)
{
DataGridViewComboBoxCell filterComboBox = datagridView.Rows[e.RowIndex].Cells[e.ColumnIndex] as DataGridViewComboBoxCell;
filterComboBox.DataSource = _filterBS;
DataGridViewComboBoxCell sourceComboBox = datagridView.Rows[e.RowIndex].Cells[_sourceComboBoxName] as DataGridViewComboBoxCell;
_filterBS.Filter = string.Format(_filterExpression, sourceComboBox.FormattedValue);
}
}
catch (Exception ex)
{
throw ex;
}
}在 dataGridView_CellEndEdit事件中写,当需要筛选的下拉选单结束编辑时,使用不筛选的A BindingSource,并移除对B BindingSource的筛选.这时选单里就包含全部的选项了.
static void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
DataGridView datagridView = sender as DataGridView;
try
{
if (e.ColumnIndex == datagridView.Columns[_filterComboBoxName].Index)
{
DataGridViewComboBoxCell filterComboBox = datagridView.Rows[e.RowIndex].Cells[e.ColumnIndex] as DataGridViewComboBoxCell;
filterComboBox.DataSource = _unFilterBS;
_filterBS.RemoveFilter();
}
}
catch (Exception ex)
{
throw ex;
}
}
http://topic.csdn.net/u/20090109/15/004b5624-d2ae-4f24-b86e-369570b17e92.html再次感谢,结贴!