Gopi
Gopi

Reputation: 256

How to fill only Selected cells of GridView with Combobox

My aim is simple. Assume I have 5 Lines of data which I filled in DataGridView. In 1st Column I filled data as shown below. User should fill the data in 2nd Column.

Name: --

Age: --

District: --

D.O.B: --

State: --

Now I have to place ComboBoxes in 2nd Column of DataGridView for "District" and "State" rows only and leave the remaining rows editable. With DataGridView I can make all Rows either ComboBox or TextBox. I googled for this but of no use. I tried 3rd Party tools also but this type of implementation I didn't find. I tried to take ListView/TreeView instead of DataGridView but I did not achieved. I am using C# Winforms VS2010.

If given any idea I am ready to start from scratch. Thanks in advance.

EDIT: Below is the output where the highlighted "Rows" should be "ComboBox" and remaining should be "Textboxes". enter image description here

If I add the below code whole Column is changing to ComboBox.

        DataGridViewComboBoxColumn districtColumn = new DataGridViewComboBoxColumn();
        districtColumn.DataPropertyName = "container";
        districtColumn.HeaderText = "Details";
        districtColumn.ValueType = typeof(Container);
        districtColumn.DisplayMember = "District";  
        districtColumn.ValueMember = "code";
        districtColumn.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox;

        dataGridView1.Columns.Add(districtColumn);

Hope now You clearly understand my Problem.

Thanks..

Upvotes: 0

Views: 1846

Answers (1)

Hari Prasad
Hari Prasad

Reputation: 16966

Ahh... I got you this time.

What you need is DataGridViewComboBoxCell, create a comboxcell and place it in grid view cell.

DataGridViewComboBoxCell  cmbcell = new DataGridViewComboBoxCell();
cmbcell.DataSource = Districts; // Your data source

DataGridViewCell normalcell = new DataGridViewTextBoxCell();
normalcell.Value = "Name"; // creating the text cell

dataGridView1.Rows[iRowIndex].Cells[1] = cmbcell; // use your own logic to set row index
dataGridView1.Rows[iRowIndex].Cells[1] = normalcell;

Hope this helps !

Upvotes: 2

Related Questions