How to set SelectedIndex in a DataGridViewComboBoxColumn?

I use datagridview in that I use datagridviewcomboboxcolumn, comboboxcolumn displays the text, but the problem is that I want to select the first comboboxcolumn element by default, how can I do this

DataGridViewComboBoxColumn dgvcb = (DataGridViewComboBoxColumn)grvPackingList.Columns["PackingUnits"];
Globals.G_ProductUtility G_Utility = new Globals.G_ProductUtility();
G_Utility.addUnittoComboDGV(dgvcb);
DataSet _ds = iRawMaterialsRequest.SelectBMR(bmr_ID, branch_ID, "PACKING");
grvPackingList.DataSource = _ds.Tables[0];
int i = 0;
foreach (DataRow dgvr in _ds.Tables[0].Rows)
{
    grvPackingList.Rows[i].Cells["Units"].Value = dgvr["Units"].ToString();
    i++;
}
+3
source share
7 answers

Available values โ€‹โ€‹in the combo box can be obtained through the items property

row.Cells[col.Name].Value = (row.Cells[col.Name] as DataGridViewComboBoxCell).Items[0];
+10
source

If I knew how to do this in this case, it would save me days of digging and
trial and error by trying to get it to set the correct index inside the CellEnter event.

Setting the DataGridViewComboBox index is the solution I was looking for ..... THANKS !!!

, , DataGridViewComboBoxCell, ,
, - :
1. , "EditingControlShowing".
2. , :
    . ComboBox.
    . "SelectedIndex" , .
            "0", , ,

, :

private void InitEvents()
{

    dgv4.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler( dgv4EditingControlShowing );

}


private void dgv4EditingControlShowing( object sender, DataGridViewEditingControlShowingEventArgs e )
{
   ComboBox ocmb = e.Control as ComboBox;
   if ( ocmb != null )
   {
      ocmb.SelectedIndex = 0;
   }
}
+1

DataGridViewComboBoxCell :

DataTable dt = new DataTable();
dt.Columns.Add("Item");
dt.Columns.Add("Value");
dt.Rows.Add("Item 1", "0");
dt.Rows.Add("Item 2", "1");
dt.Rows.Add("Item 3", "2");
dt.Rows.Add("Item 4", "3");

for (int i = 0; i < dvg.Rows.Count; i++)
{
    DataGridViewComboBoxCell comboCell = (DataGridViewComboBoxCell)dvg.Rows[i].Cells[1];
    comboCell.DisplayMember = "Item";
    comboCell.ValueMember = "Value";
    comboCell.DataSource = dt;
};
+1

datagridViewComboBoxCell:

DataTable dt = new DataTable();
dt.Columns.Add("Item");
dt.Columns.Add("Value");
dt.Rows.Add("Item1", "0");
dt.Rows.Add("Item1", "1");
dt.Rows.Add("Item1", "2");
dt.Rows.Add("Item1", "3");
DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();
cmb.DefaultCellStyle.Font = new Font("Tahoma", 8, FontStyle.Bold);
cmb.DefaultCellStyle.ForeColor = Color.BlueViolet;
cmb.FlatStyle = FlatStyle.Flat;
cmb.Name = "ComboColumnSample";
cmb.HeaderText = "ComboColumnSample";
cmb.DisplayMember = "Item";
cmb.ValueMember = "Value";
DatagridView dvg=new DataGridView();
dvg.Columns.Add(cmb);
cmb.DataSource = dt;
for (int i = 0; i < dvg.Rows.Count; i++)
{
dvg.Rows[i].Cells["ComboColumnSample"].Value = (cmb.Items[0] as 
DataRowView).Row[1].ToString();
}

0

ComboBoxes DataGridViews . , :

public static void InitDGVComboBoxColumn<T>(DataGridViewComboBoxCell cbx, List<T> 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);
        }
    }
}

.Display .ValueMember DataGridViewComboBoxCell List DataSource. , ValueMember, .Value

:

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<CustomerCbx> custList = new List<CustomerCbx>()
            {
                new CustomerCbx{ Display = c1.Name, Value = c1},
                new CustomerCbx{ Display = c2.Name, Value = c2},
            }

            InitDGVComboBoxColumn<CustomerCbx>(cbx, custList, "display", "value");
        }
    }
}
}

, , ( , ). , ;)

0

You need to set the elements for a new cell. This should be automatically done by the column when creating a new row from the user interface.

 var cell = new DataGridViewComboBoxCell() { Value = "SomeText" };
 cell.Items.AddRange(new String[]{"SomeText", "Abcd", "123"});
0
source

something else worked for me, what I did was just set the dtataGridComboBox value when a new user entry bu with the userAddedRow event is ever added. For the first line, I used the code in the constructor.

public partial class pt_drug : PatientDatabase1_3._5.basic_templet
{
    public pt_drug()
    {
        InitializeComponent();
        dataGridView_drugsDM.Rows[0].Cells[0].Value = "Tablet";
    }

    private void dataGridView_drugsDM_UserAddedRow(object sender, DataGridViewRowEventArgs e)
    {
        dataGridView_drugsDM.Rows[dataGridView_drugsDM.RowCount - 1].Cells[0].Value = "Tablet";
    }


}
0
source

Source: https://habr.com/ru/post/1788404/


All Articles