DataGridView set column cell Combobox

后端 未结 8 2054
滥情空心
滥情空心 2020-12-05 19:36

I have tables like that in Datagridview:

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


        
相关标签:
8条回答
  • 2020-12-05 20:09

    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

    0 讨论(0)
  • 2020-12-05 20:10

    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.

    0 讨论(0)
提交回复
热议问题