Collection Items ComboBox in Datagrid C#
I want to make a datagridview1 in C#
that's have 3 Rows (Number,Name and Country)
If i want to give a combobox
in datagridview1
in rows up is title or description of (Number,Name and Country)
below is......... a selected combobox dropdown
combobox of Number have 5 Choice or 5 Collection Items ("1","2","3","4","5")
combobox 开发者_运维问答of Name have 5 Choice or 5 Collection Items ("A","B","C","D","E")
combobox of Country have 5 Choice or 5 Collection Items ("Amerika","England","Indonesia","Australia","India")
can U help me how to make it???
Each column will need to be of type DataGridViewComboBoxColumn
.
This column type then exposes various properties that will allow you to correctly set up the column combobox options.
For the very simple case you are asking for you can do something like this (just for say the country column here):
DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn();
col.HeaderText = "Country";
col.Name = "Country";
col.Items.AddRange("Amerika","England","Indonesia","Australia","India");
dataGridView1.Columns.Add(col);
Now you get a combobox column in your grid with those countries available.
There a many further options that give you more flexibility and power. If for example your DataGridView has a DataSource which includes a "ChosenCountry" property you can set up binding between your column and the DataSource.
col.DataPropertyName = "ChosenCountry";
One step more, you can use an id for these countries rather than relying upon the plain text:
List<Country> countries = new List<Country>();
countries.Add(new Country { Id = 0, Name = "Amerkia" });
countries.Add(new Country { Id = 1, Name = "England" });
// and so on or even better get this from some external store using a query
DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn();
// Now you can use the CheckBoxColumn datasource:
col.DataSource = countries;
// And you set the Display and value members and the DataPropertyName:
col.DisplayMember = "Name";
col.ValueMember = "Id";
col.DataPropertyName = "CountryId";
Beyond this point you will need to ask questions detailing you exact scenario and problems to get a more specific answer.
精彩评论