20161014006 DataGridView Combobox 数据绑定与传值

Posted 我的梦会回来

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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());
                }

            }
        }

以上是关于20161014006 DataGridView Combobox 数据绑定与传值的主要内容,如果未能解决你的问题,请参考以下文章

c#设置datagridview行标题

VB.net datagridview 折叠展开

怎么插入datagridview的行

datagridview怎么把一行数据复制到另一个datagridview [C# VS2005]

c#怎么把datagridview给填满

C#datagridview怎么设置行标题的文字?