20161014006 DataGridView Combobox 数据绑定与传值

        //Combobox
        private void T_Form_CY_CBD_D_CurrentCellChanged(object sender, EventArgs e)
        {
            DataGridViewCell CurrnetCell = T_Form_CY_CBD_D.CurrentCell;
            T_Form_CY_CBD_D.Columns["DFWorkRate"].ReadOnly = true;
            if (CurrnetCell != null && CurrnetCell.OwningColumn.Name == "DFDevice")
            {
                try
                {
                    Rectangle rect = T_Form_CY_CBD_D.GetCellDisplayRectangle(T_Form_CY_CBD_D.CurrentCell.ColumnIndex, T_Form_CY_CBD_D.CurrentCell.RowIndex, false);
                    string sexValue = T_Form_CY_CBD_D.CurrentCell.Value.ToString();

                    string str_sql = " Select [FMachineType],[FMPWorkRate] FROM [PLM].[dbo].[T_Form_CY_RateOfWork_D] ";
                    DataTable dt2 = PLMDAL.DBHelper.ExecSql(str_sql);

                    DataGridViewComboBoxColumn cbo = (DataGridViewComboBoxColumn)T_Form_CY_CBD_D.Columns["DFDevice"];
                    cbo.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
                    cbo.DataSource = dt2;
                    cbo.DisplayMember = "FMachineType";
                    cbo.ValueMember = "FMachineType";

                    if (CurrnetCell.Value.ToString().Trim() != "")
                    {
                        DataRow[] dr = dt2.Select("FMachineType = '" + CurrnetCell.Value.ToString().Trim() + "' ");
                        //MessageBox.Show(dr[0].ItemArray[1].ToString().Trim());
                        T_Form_CY_CBD_D.CurrentRow.Cells["DFWorkRate"].Value = dr[0].ItemArray[1].ToString().Trim();
                    }

                    //string selectValue = (string)(T_Form_CY_CBD_D.CurrentCell as DataGridViewComboBoxCell).Value;//取得所选值的key即绑定ValueMember的值。
                    //MessageBox.Show("" + selectValue.ToString() + "");
                    //MessageBox.Show(CurrnetCell.Value.ToString());

                    //
                    ////DataGridViewComboBoxCell DgvCell = T_Form_CY_CBD_D.Rows[e.RowIndex].Cells[1] as DataGridViewComboBoxCell;
                    //DataGridViewComboBoxCell DgvCell = T_Form_CY_CBD_D.CurrentCell as DataGridViewComboBoxCell;
                    //DgvCell.DataSource = dt2.Rows[0][0].ToString();
                    ////DgvCell.DisplayMember = "MeasureName";
                    ////DgvCell.ValueMember = "MeasureId";

                    //string selectValue = (string)(T_Form_CY_CBD_D.CurrentCell as DataGridViewComboBoxCell).Value;//取得所选值的key即绑定ValueMember的值。
                    //MessageBox.Show("" + selectValue.ToString() + "");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }

            }
        }

posted @ 2016-10-14 16:00  追逐苦痛  阅读(648)  评论(0编辑  收藏  举报