Natile
Natile

Reputation: 193

set value in combobox inside datagridview

I Have Datagridview with combobox inside and i can't set the index in code I read this and this - both not work.

here is my code:

  dgShuffle.DataSource = dtCards;
  DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();
  cmb.HeaderText = "Select Data";
  cmb.Name = "cmb";
  cmb.MaxDropDownItems = 2;
  cmb.Items.Add("real");
  cmb.Items.Add("sham");

  dgShuffle.Columns.Add(cmb);
  for (int i = 0; i < dgShuffle.RowCount; i++)
  {
       (dgShuffle.Rows[i].Cells[6] as DataGridViewComboBoxCell).Value = "real";
        // dgShuffle.Rows[i].Cells[6].Value = "real";
  }

To set by code i mean : programmatically (depend on the value in datatble). I Dont Get error at all. the value simply not display in the combobox

I checked the index of the combobox and this is correct , below the output from my immediate window:

?dgShuffle.Rows[i].Cells[6]

{DataGridViewComboBoxCell { ColumnIndex=6, RowIndex=0 }} [System.Windows.Forms.DataGridViewComboBoxCell]: {DataGridViewComboBoxCell { ColumnIndex=6, RowIndex=0 }} base: {DataGridViewComboBoxCell { ColumnIndex=6, RowIndex=0 }} AccessibilityObject: {System.Windows.Forms.DataGridViewCell.DataGridViewCellAccessibleObject} ColumnIndex: 6 ContentBounds: {X = 0 Y = 12 Width = 0 Height = 0} ContextMenuStrip: null DefaultNewRowValue: null Displayed: false EditedFormattedValue: "" EditType: {Name = "DataGridViewComboBoxEditingControl" FullName = "System.Windows.Forms.DataGridViewComboBoxEditingControl"} ErrorIconBounds: {X = 0 Y = 0 Width = 0 Height = 0} ErrorText: "" FormattedValue: "" FormattedValueType: {Name = "String" FullName = "System.String"} Frozen: false HasStyle: false InheritedState: Resizable | ResizableSet | Visible InheritedStyle: {DataGridViewCellStyle { BackColor=Color [Window], ForeColor=Color [ControlText], SelectionBackColor=Color [Highlight], SelectionForeColor=Color [HighlightText], Font=[Font: Name=Microsoft Sans Serif, Size=7.8, Units=3, GdiCharSet=177, GdiVerticalFont=False], WrapMode=False, Alignment=MiddleLeft }} IsInEditMode: false OwningColumn: {DataGridViewComboBoxColumn { Name=cmb, Index=6 }} OwningRow: {DataGridViewRow { Index=0 }} PreferredSize: {Width = 43 Height = 26} ReadOnly: false Resizable: true RowIndex: 0 Selected: false Size: {Width = 100 Height = 24} Style: {DataGridViewCellStyle { }} Tag: null ToolTipText: "" Value: null ValueType: {Name = "Object" FullName = "System.Object"} Visible: true

Next try :

  1. I create a new winforms project
  2. Drag datagridview to the default form (from the toolbox)
  3. Copy this code to Form1_Load :

        DataTable dtCards;
        dtCards = new DataTable();
        dtCards.Columns.Add("printedString");
        dtCards.Rows.Add("1");
        dtCards.Rows.Add("2");
        dtCards.Rows.Add("3");
        dtCards.Rows.Add("4");
        dataGridView1.DataSource = dtCards;
    
        DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();
        cmb.HeaderText = "Select Data";
        cmb.Name = "cmb";
        cmb.MaxDropDownItems = 2;
        cmb.Items.Add("real");
        cmb.Items.Add("sham");
    
        dataGridView1.Columns.Add(cmb);
        for (int i = 0; i < dataGridView1.RowCount; i++)
        {
            (dataGridView1.Rows[i].Cells[6] as DataGridViewComboBoxCell).Value = "real";
            // dgShuffle.Rows[i].Cells[6].Value = "real";
        }
    

What am i missing???

Upvotes: 1

Views: 12169

Answers (1)

Fabio
Fabio

Reputation: 32453

You are using datagridview bounded to the data source. You need specify that value in the datasource.

Add value for DataGridViewComboBoxColumn in the data source
Then set DataGridViewComboBoxColumn.DataPropertyName to the name of the column/property of your data source

DataTable dtCards;
dtCards = new DataTable();
dtCards.Columns.Add("printedString");
dtCards.Columns.Add("comboboxValue", typeof(String)); //adding column for combobox
dtCards.Rows.Add("1", "real");
dtCards.Rows.Add("2", "real");
dtCards.Rows.Add("3", "real");
dtCards.Rows.Add("4", "real");


DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();
cmb.HeaderText = "Select Data";
cmb.Name = "cmb";
cmb.MaxDropDownItems = 2;
cmb.Items.Add("real");
cmb.Items.Add("sham");
cmb.DataPropertyName = "comboboxValue"; //Bound value to the datasource

dataGridView1.Columns.Add(cmb);
dataGridView1.DataSource = dtCards;

Upvotes: 1

Related Questions