How to insert 'Empty' field in ComboBox bound to DataTable

后端 未结 13 1446
谎友^
谎友^ 2020-12-14 09:07

I have a combo box on a WinForms app in which an item may be selected, but it is not mandatory. I therefore need an \'Empty\' first item to indicate that no value has been s

13条回答
  •  醉酒成梦
    2020-12-14 09:33

    I have found this way:

        DataTable hierarchies = new DataTable(); 
    
        cmbHierarchies.BeginUpdate();
        cmbHierarchies.ValueMember = this.Value;
        cmbHierarchies.DisplayMember = this.Display;
        hierarchies = DataView.ToTable();
        cmbHierarchies.DataSource = table;
        cmbHierarchies.EndUpdate();
    
        //Add empty row
        DataRow row = table.NewRow();
        table.Rows.InsertAt(row, 0);
        cmbHierarchies.SelectedIndex = 0;
    

提交回复
热议问题