VS2005,winform中datagridview控件有两个combobox列,两列分别要绑定到不同的数据源。第一列为机构,第二列为人员。现在想实现点击第一列选中机构,第二列从人员表里绑定该机构的所有人员,请各位大侠指点,最好详细点,感激不尽!

解决方案 »

  1.   

    为什么不用两个datagridview控件
    控件里面的有Combobox触发事件可以用吗?不太清楚
      

  2.   

        动态绑定,你的机构中的数据肯定固定的,
     当用户选择机构中一条数据,即触发selectchanged事件,
     把机构Id取出来,然后根据Id,把Id下的人员查找查找出来,先把员工combox
     中以前的数据清空,然后把查找的结果绑定到员工combox中……
      

  3.   

    http://www.cnblogs.com/tonyepaper/archive/2009/01/08/1372233.html
    供你参考.
      

  4.   


    先把员工combox
    中以前的数据清空,然后把查找的结果绑定到员工combox中……这一步怎么实现的,能不能说的详细点啊。
      

  5.   

    combox.item.chear 清空
    获取datagridview的人员编号select出来, 在绑定你要的combox
      

  6.   

    在第1个combox的控件下拉事件里, 按combox1的text 为sql条件, select出combox2要值 填充到进去
      

  7.   


    老大,人员列是怎么构建的,我是用后台代码增加的人员列,就是combobox列。可是不能实现这样的绑定呀。
    等我一会贴代码。
      

  8.   

    这段代码是窗体生成是构建datagridveiw的列,以及绑定机构列
    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;
                        }
                        //如何绑定检查员列?                }
                }
            }
      

  9.   

    string str部门 = Convert.ToString(dgv部门[1, dgv部门.CurrentCell.RowIndex].Value).Trim(); //获取部门列的值
    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())
        }
      

  10.   

    搞两个BindingSource一个是A不筛选的,另一个是B用来筛选的.默认使用未筛选的A BindingSource.        //未筛选的数据源
            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;
                }
            }
      

  11.   

    谢谢各位的指点,非常感激,我实现了联动,但却提示DataGridViewComboBoxCell 值无效,具体请见此贴:
    http://topic.csdn.net/u/20090109/15/004b5624-d2ae-4f24-b86e-369570b17e92.html再次感谢,结贴!