How to set value to DataGridViewComboBox Column?

前端 未结 5 1488
盖世英雄少女心
盖世英雄少女心 2020-12-09 21:51

I want to know how to set the value of a DataGridViewComboBox cell. I already bind the DataGridViewComboBox with DataSource. But I want to set new value to this this datasou

相关标签:
5条回答
  • 2020-12-09 21:56

    The error is due to the fact that you have bound a DataSource to the ComboBox column and while you assign Select its trying to locate the same in the DataSource its bound too. Your best bet will be to add the same to the original DataSource which you bind to the ComboBox column

    0 讨论(0)
  • 2020-12-09 21:58

    This worked for me

    dim dt as datatable
    dt=fillMydata()  'do your function
    'dt has 2 col myId as myDescription
    Dim col As DataGridViewComboBoxColumn
    col = New DataGridViewComboBoxColumn
    col.HeaderText = "MyHeader"
    col.Name = "Myname"
    col.DataSource = data   '
    col.DisplayMember = "myDescription"
    col.ValueMember = "myId"
    col.DropDownWidth = 240
    
    ''''''''''''''''''''''''''''''''''''''''''''''
    'set the value
    col.DefaultCellStyle.NullValue = dt.Rows(0).Item("myDescription").ToString
    ''''''''''''''''''''''''''''''''''''''''''''''
    
    DataGridView1.Columns.Add(col)
    

    hope this help

    0 讨论(0)
  • 2020-12-09 22:15

    Try by adding the event

    private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
    {
     }
    
    0 讨论(0)
  • 2020-12-09 22:18

    The root cause of your problem is that the value "Select" does not exist in your list of combo box values. You will either need to add this to the datasource for the column, or to the datasource for the individual cell's combobox (using the editing control of that cell).

    Below is some more explanation on setting the value of the selected item which I'm going to leave as you might find it useful.

    There are two basic ways to set the value of a DataGridViewComboBoxColumn. You either use databinding or you set the value directly.

    It sounds like you are attempting to set directly, so I'll explain that first and then below I'll complete the answer with databinding.

    Taking the following contrived example:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
    
            BindingList<User> users = new BindingList<User> { new User() { UserName = "Fred", userid = 2 } };
    
            IList<MyValue> values = new List<MyValue> { new MyValue{id = 1, name="Fred"}, new MyValue{id = 2, name="Tom"}};
    
            dataGridView1.DataSource = users;
    
            DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn();
            col.DataSource = values;
            col.DisplayMember = "name";
            col.DataPropertyName = "userid";
            col.ValueMember = "id";
            dataGridView1.Columns.Add(col);
    
        }       
    }
    
    public class MyValue
    {
        public int id { get; set; }
        public string name { get; set; }
    }
    
    public class User
    {
        public string UserName { get; set; }
        public int userid {get;set;}
    }
    

    In this example, I have set the ValueMember property to be "id" which refers to the property names id in the list my ComboBox is bound to.

    So all I need to do is the following (where I am sure that my row and cell indexes are correct - you should write code to check that):

    dataGridView1.Rows[0].Cells[2].Value = 1;
    

    Now, in my example this works because my ValueMember is set to my "id" property which is an integer, if instead id was a string property I would need to set value like this:

    dataGridView1.Rows[0].Cells[2].Value = "1";
    

    (actually just tried this and it manages with an implicit cast)

    And of course, I would want to make sure that there was actually a value of "1" in my list.

    The above few lines are the direct cause of your error - your list that supplies the values to the combo box does not contain the value "Select".

    To set the value using databinding, you need to tell the ComboBoxColumn what it is binding against in the DataGridView datasource.

    The is done my setting the DataPropertyName of the ComboBoxColumn to the name of a property of the class that the DataGridView is bound against.

    0 讨论(0)
  • 2020-12-09 22:18

    There is a way to set the value but it is not allowed if DataSource is set.

    DataGridViewComboBoxCell cell =(DataGridViewComboBoxCell)gvList.Rows[0].Cells[0];
    cell.Items[0] = "Select";
    
    0 讨论(0)
提交回复
热议问题