我在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
我想从组合框中更改列“Money”值
我试过这个,但不知道进一步:
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);
答案 0 :(得分:29)
试试这个
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);
只需使用DataPropertyName
代替ValueMember
答案 1 :(得分:3)
你差不多完成了。
只有两个小问题:
以下完整代码:
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;
答案 2 :(得分:1)
您可以尝试以下方法:
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" };
}
或者这个:
foreach (DataGridViewRow row in dataGridView1.Rows)
{
DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)(row.Cells["Money"]);
cell.DataSource = new string[] { "10", "30" };
}
答案 3 :(得分:1)
您可以替换为
dt.Columns.Add("Money", typeof(List<string>));
答案 4 :(得分:1)
在设计器中设置网格时,请注意ComboBox列的索引。在此的示例为1。Money列为索引1。网格已经具有一个成员,该成员是DataGridViewComboBoxColumn。当您初始化包含控件的表单时,请获取并对其进行初始化。像这样:
DataGridViewComboBoxColumn cbc = (DataGridViewComboBoxColumn)dataGridView1.Columns[1];
cbc.Items.Add("10");
cbc.Items.Add("30");
cbc.Items.Add("80");
cbc.Items.Add("100");
当您填充网格时,请在该单元格中插入文本值。当用户单击单元格时,将显示下拉列表,他们将能够更改值。
真的,如果您只想拥有一个包含这些值的下拉列表,则可以在设计人员内部通过修改Items集合来完成此操作。
答案 5 :(得分:0)
我知道现在已经很晚了但是试试这个:
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;
答案 6 :(得分:0)
((DataGridViewComboBoxColumn)dgvFacturas.Columns["estatus"]).DataSource = estatustemp.ToList();
((DataGridViewComboBoxColumn)dgvFacturas.Columns["estatus"]).ValueMember = "Key";
((DataGridViewComboBoxColumn)dgvFacturas.Columns["estatus"]).DisplayMember = "Value";
答案 7 :(得分:0)
您可以像这样在 Visual Studio设计器中轻松地做到这一点:
ColumnType
属性ColumnType
属性值更改为DataGridViewComboBoxColumn