日期:2014-05-18  浏览次数:20813 次

双击dgv后,如何把dgv中内容显示在ComboBox
dgv和ComboBox是在不同的窗体,ComboBox的DisplayMember和ValueMember与dgv的列名都是一样的,现在我双击dgv,我想ComboBox的SelectedItem变为dgv双击的当前行的值,该怎么实现呢??求解!!

------解决方案--------------------
你想实现dgv和ComBoBox的联动嘛?你选择comBoBox中的项会联动dgv,选择dgv联动ComBoBox
示例代码
C# code

            DataTable dt = new DataTable();
            dt.Columns.Add("DetpName", typeof(string));
            dt.Columns.Add("MemberName", typeof(string));
            DataRow dr1 = dt.NewRow();
            dr1["DetpName"] = "软体研发一部一课";
            dr1["MemberName"] = "AAA";
            dt.Rows.Add(dr1);

            DataRow dr2 = dt.NewRow();
            dr2["DetpName"] = "软体研发一部一课";
            dr2["MemberName"] = "BBB";
            dt.Rows.Add(dr2);

            DataRow dr3 = dt.NewRow();
            dr3["DetpName"] = "软体研发一部一课";
            dr3["MemberName"] = "CCC";
            dt.Rows.Add(dr3);

            DataRow dr4 = dt.NewRow();
            dr4["DetpName"] = "软体研发一部二课";
            dr4["MemberName"] = "DDD";
            dt.Rows.Add(dr4);

            DataRow dr5 = dt.NewRow();
            dr5["DetpName"] = "软体研发一部二课";
            dr5["MemberName"] = "EEE";
            dt.Rows.Add(dr5);

            DataRow dr6 = dt.NewRow();
            dr6["DetpName"] = "软体研发二部一课";
            dr6["MemberName"] = "FFF";
            dt.Rows.Add(dr6);

            DataRow dr7 = dt.NewRow();
            dr7["DetpName"] = "软体研发二部一课";
            dr7["MemberName"] = "GGG";
            dt.Rows.Add(dr7);

            DataRow dr8 = dt.NewRow();
            dr8["DetpName"] = "软体研发三部一课";
            dr8["MemberName"] = "HHH";
            dt.Rows.Add(dr8);

            this.dataGridView1.DataSource = dt;
            this.comboBox1.DataSource=dt;
            this.comboBox1.DisplayMember = "DetpName";
            this.comboBox1.ValueMember = "DetpName";

------解决方案--------------------
1、获取双击行的ID(相当于ComboBox的ValueMember)
2、循环ComboBox中的valueMember,如果跟dgv中的Id一样就直接
for(int i = 0; i < ComboBox.items.count; i++)
{
if(ID == ComboBox.valuemembers[i])
{
ComboBox.selectedIndex = i;
return;
}
}
大致思路是这样,代码自己整合。