DataGridView set column cell Combobox

2019-01-14 17:39发布

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); 

6条回答
对你真心纯属浪费
2楼-- · 2019-01-14 17:50

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

查看更多
贪生不怕死
3楼-- · 2019-01-14 17:54

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;
查看更多
劳资没心,怎么记你
4楼-- · 2019-01-14 17:57
((DataGridViewComboBoxColumn)dgvFacturas.Columns["estatus"]).DataSource = estatustemp.ToList();
((DataGridViewComboBoxColumn)dgvFacturas.Columns["estatus"]).ValueMember = "Key";
((DataGridViewComboBoxColumn)dgvFacturas.Columns["estatus"]).DisplayMember = "Value";
查看更多
聊天终结者
5楼-- · 2019-01-14 17:58

You can replace with

dt.Columns.Add("Money", typeof(List<string>));
查看更多
看我几分像从前
6楼-- · 2019-01-14 18:05

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" };
}
查看更多
兄弟一词,经得起流年.
7楼-- · 2019-01-14 18:07

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;
查看更多
登录 后发表回答