Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
126 views
in Technique[技术] by (71.8m points)

c# - How to set value to DataGridViewComboBox Column?

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 datasource.

This is what i have so far:

gvList.Rows[0].Cells[0].value = "Select";

But it throws an error saying: Format Exception: DataGridViewComboBox Cell value is not valid .

How could I achieve this without error?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...