how to set SelectedIndex in DataGridViewComboBoxColumn?

后端 未结 7 849
慢半拍i
慢半拍i 2021-01-18 04:00

i am using a datagridview in that i am using a datagridviewcomboboxcolumn, comboboxcolumn is displaying text but the problem is i want to select the first item of comboboxco

7条回答
  •  暖寄归人
    2021-01-18 04:45

    I've had some real trouble with ComboBoxes in DataGridViews and did not find an elegant way to select the first value. However, here is what I ended up with:

    public static void InitDGVComboBoxColumn(DataGridViewComboBoxCell cbx, List dataSource, String displayMember, String valueMember)
    {
        cbx.DisplayMember = displayMember;
        cbx.ValueMember = valueMember;
        cbx.DataSource = dataSource;
        if (cbx.Value == null)
        {
            if(dataSource.Count > 0)
            {
                T m = (T)cbx.Items[0];
                FieldInfo fi = m.GetType().GetField(valueMember, BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
                cbx.Value = fi.GetValue(m);
            }
        }
    }
    

    It basically sets the .Display and .ValueMember properties of the DataGridViewComboBoxCell and uses a List as DataSource. It then takes the first item, and uses reflection to get the value of the member that was used as ValueMember and sets the selected value via .Value

    Use it like this:

    public class Customer
    {
        private String name;
        public String Name
        {
            get {return this.name; }
            set {this.name = value; }
        }
    
        private int id;
        public int Id
        {
            get {return this.id; }
            set {this.id = value; }
        }
    }
    
    public class CustomerCbx
    {
        private String display;
        public String Display
        {
            get {return this.display; }
            set {this.display = value; }
        }
    
        private Customer value;
        public Customer Value
        {
            get {return this.value; }
            set {this.value = value; }
        }
    }
    
    public class Form{
    private void Form_OnLoad(object sender, EventArgs e)
    {
            //init first row in the dgv
            if (this.dgv.RowCount > 0)
            {
                DataGridViewRow row = this.dgv.Rows[0];
                DataGridViewComboBoxCell cbx = (DataGridViewComboBoxCell)row.Cells[0];
    
                Customer c1 = new Customer(){ Name = "Max Muster", ID=1 };
                Customer c2 = new Customer(){ Name = "Peter Parker", ID=2 };
                List custList = new List()
                {
                    new CustomerCbx{ Display = c1.Name, Value = c1},
                    new CustomerCbx{ Display = c2.Name, Value = c2},
                }
    
                InitDGVComboBoxColumn(cbx, custList, "display", "value");
            }
        }
    }
    }
    

    It seems pretty hacky to me, but I couldn't find any better way so far (that also works with complex objects other than just Strings). Hope that will save the search for some others ;)

提交回复
热议问题