Uni Le
Uni Le

Reputation: 793

DataGridView set column cell Combobox

I have tables like that in Datagridview:

 Name   Money
 -------------
 Hi      100   //here Combobox with member {10,30,80,100} to choose
 Ki      30    //here Combobox with member {10,30,80,100} to choose

I want to change Column "Money" Value from combobox

I tried with this but dont know further:

DataTable dt = new DataTable();
dt.Columns.Add("Name", typeof(String));
dt.Columns.Add("Money", typeof(String));
dt.Rows.Add(new object[] { "Hi", 100});
dt.Rows.Add(new object[] { "Ki", 30});

DataGridViewComboBoxColumn column = new DataGridViewComboBoxColumn();
var list11 = new List<string>() { "10", "30", "80", "100" };
column.DataSource = list11;
column.ValueMember = "Money";
dataGridView1.Columns.Add(column); 

Upvotes: 17

Views: 103807

Answers (8)

Matt
Matt

Reputation: 27026

You can do that very easily in the Visual Studio designer like this:

  1. Select (left click) the data grid view (DGV)
  2. In the properties of the DGV, click on the link "Edit columns". A dialog opens.
  3. Select the column you want to change to combobox in the "Selected Columns" list
  4. On the right hand side of the dialog, in "Unbound Column properties", "Design" section, find ColumnType property
  5. Change ColumnType property value to DataGridViewComboBoxColumn
  6. Under section 'Data', click into the 3 dots right to 'Items' and enter the values you want to add in the String Collection Editor
  7. Save changes by clicking OK.

Run the application. Now the values you've entered in step 6. appear in a drop down box.

enter image description here

Upvotes: 0

Scott
Scott

Reputation: 1119

Note the index of your ComboBox column when you set up the grid in the designer. In this example it is 1. The Money column is index 1. The grid already has a member that is a DataGridViewComboBoxColumn. When you initialize the form that contains the control get it and initialize it. Like this:

DataGridViewComboBoxColumn cbc = (DataGridViewComboBoxColumn)dataGridView1.Columns[1];
cbc.Items.Add("10");
cbc.Items.Add("30");
cbc.Items.Add("80");
cbc.Items.Add("100");

When you populate the grid, insert text values into that cell. When the user clicks on the cell the droplist will appear and they'll be able to change the value.

Really, if all you want to do is just have a drop list with those values, you can do it right from within the designer by modifying the Items collection.

Upvotes: 1

Ranfi Rivas
Ranfi Rivas

Reputation: 1

((DataGridViewComboBoxColumn)dgvFacturas.Columns["estatus"]).DataSource = estatustemp.ToList();
((DataGridViewComboBoxColumn)dgvFacturas.Columns["estatus"]).ValueMember = "Key";
((DataGridViewComboBoxColumn)dgvFacturas.Columns["estatus"]).DisplayMember = "Value";

Upvotes: 0

Yashar Aliabbasi
Yashar Aliabbasi

Reputation: 2729

I know it's late but Try this:

            DataTable dt = new DataTable();
            dt.Columns.Add("Name", typeof(String));
            dt.Columns.Add("Money", typeof(String));
            dt.Rows.Add(new object[] { "Hi", 100 });
            dt.Rows.Add(new object[] { "Ki", 30 });

            DataTable dt2 = new DataTable();
            dt2.Columns.Add("Money", typeof(String));
            dt2.Columns.Add("Meaning", typeof(String));
            dt2.Rows.Add(new object[] { "30" ,"Name 1" });
            dt2.Rows.Add(new object[] { "100", "Name 2" });
            dt2.Rows.Add(new object[] { "80", "Name 3" });
            dt2.Rows.Add(new object[] { "90", "Name4" });

            DataGridViewComboBoxColumn money = new DataGridViewComboBoxColumn();

            money.DataSource = dt2;
            money.HeaderText = "Money";
            money.DataPropertyName = "Money";
            money.DisplayMember = "Meaning";
            money.ValueMember = "Money";

            DataGridViewTextBoxColumn name = new DataGridViewTextBoxColumn();
            name.HeaderText = "Name";
            name.DataPropertyName = "Name";

            DGV.Columns.Add(money);
            DGV.Columns.Add(name);
            DGV.DataSource = dt;

Upvotes: 0

yogi
yogi

Reputation: 19619

Try this

dataGridView1.AutoGenerateColumns = false;

DataTable dt = new DataTable();
dt.Columns.Add("Name", typeof(String));
dt.Columns.Add("Money", typeof(String));
dt.Rows.Add(new object[] { "Hi", 100 });
dt.Rows.Add(new object[] { "Ki", 30 });

DataGridViewComboBoxColumn money = new DataGridViewComboBoxColumn();
var list11 = new List<string>() { "10", "30", "80", "100" };
money.DataSource = list11;
money.HeaderText = "Money";
money.DataPropertyName = "Money";

DataGridViewTextBoxColumn name = new DataGridViewTextBoxColumn();
name.HeaderText = "Name";
name.DataPropertyName = "Name";

dataGridView1.DataSource = dt;
dataGridView1.Columns.AddRange(name, money);

Just use DataPropertyName instead of ValueMember

Upvotes: 36

bozydar.sz
bozydar.sz

Reputation: 359

You were almost done.

There are only two minor issues:

  1. In your table you are adding to rows "Money" value as integers, while in your column they are defined as string
  2. First add your table ad DataGridView DataSource, and then set column DataPropertyName

Full code below:

var table = new DataTable();
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Money", typeof(string));
table.Rows.Add("Hi", "100");
table.Rows.Add("Ki", "30");

var column = new DataGridViewComboBoxColumn();
column.DataSource = new List<string>() { "10", "30", "80", "100" };            

dataGridView1.Columns.Add(column);
dataGridView1.DataSource = table;

Upvotes: 3

Aghilas Yakoub
Aghilas Yakoub

Reputation: 29000

You can replace with

dt.Columns.Add("Money", typeof(List<string>));

Upvotes: 1

chridam
chridam

Reputation: 103445

You could try the following:

DataGridViewComboBoxColumn column = new DataGridViewComboBoxColumn();
column.Name = "Money";
column.DataSource = new string[] { "10", "30", "80", "100" };
dataGridView1.Columns.Add(column);

for (int row = 0; row < dataGridView1.Columns.Count; row++)
{
   DataGridViewComboBoxCell cell = 
       (DataGridViewComboBoxCell)(dataGridView1.Rows[row].Cells["Money"]);
   cell.DataSource = new string[] { "80", "100" };
}

Or this:

foreach (DataGridViewRow row in dataGridView1.Rows)
{
    DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)(row.Cells["Money"]);
    cell.DataSource = new string[] { "10", "30" };
}

Upvotes: 1

Related Questions