Windows Develop Bookmark and Share   
 index > Windows Forms General > Datagridview combobox in VS2005
 

Datagridview combobox in VS2005

Hi I am using VS2005

I have a datagridview in a windows form. I want that when I will click on the datagridview cell then that cell will appear to me as datagridviewcombobox and a collection of data will appear to that cell which I have called from database prior. And the combobox.text should be that text which was in that cell prior to click. please help me

with regards
Shakhawat
Nerurmon1  Thursday, May 28, 2009 3:44 AM
Hi Nerurmon1,

Yes, it's not good to post duplicate thread, and I hope you can close that thread if that answers your question.

Please try the following code if your records are not too many:

void dataGridView2_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
//suppose the comboboxcolumn is the first column
if ( this.dataGridView2.CurrentCell.ColumnIndex == 0)
{
ComboBox cb = e.Control as ComboBox;
cb.SelectionChangeCommitted -= new EventHandler(cb_SelectionChangeCommitted);
cb.SelectionChangeCommitted += new EventHandler(cb_SelectionChangeCommitted);
}
}

void cb_SelectionChangeCommitted(object sender, EventArgs e)
{
ComboBox cb = sender as ComboBox;
for (int i = 0; i < this.dataGridView2.RowCount; i++)
{
if (this.dataGridView2.CurrentCell.RowIndex != i && cb.SelectedIndex!=-1)
{
if (this.dataGridView2.Rows[i].Cells[0].FormattedValue!=null &&
this.dataGridView2.Rows[i].Cells[0].FormattedValue.ToString() == cb.Text)
{
MessageBox.Show("this value has been existed");
cb.SelectedIndex = -1;

}
}
}
}

If there are too many records, to improve performance you can create a dictionary object to store the values which has been used. In the selectionChangeCommitted event handler, you only need to loop a dictionary object after then.

Best regards,
Bruce Zhou



Please mark the replies as answers if they help and unmark if they don't.
  • Marked As Answer byNerurmon1 Wednesday, June 03, 2009 10:42 AM
  •  
Bruce.Zhou  Wednesday, June 03, 2009 10:14 AM
You can use cell_click event of datagridview to doing that.
Please mark the replies as answers if they help and unmark if they don't.
Nitin Chaudhary  Thursday, May 28, 2009 6:19 AM
What to write in cell_Click event please

my datagridviewname is dgv_DietList

and another thing is that I want to add column in datagridview
what is the code to add a column in datagridview?
Nerurmon1  Thursday, May 28, 2009 6:32 AM
You can use this idea!...


textBoxAssName.Text = dataGridViewEmployeeRecords.CurrentRow.Cells["Name"].Value.ToString();

NIIT HOUSE GKP INDIA Please mark the replies as answers if they help and unmark if they don't.
Nitin Chaudhary  Friday, May 29, 2009 3:13 AM
Hi Nerurmon1,

You can add a DataGridViewComboBoxColumn in the DataGridView, and set its DisplayStyle to Nothing. That does exactly you want.

Here's the code to add a column in the DataGridView:

DataGridView dgv = new DataGridView();
this.Controls.Add(dgv);
DataGridViewComboBoxColumn col1 = new DataGridViewComboBoxColumn();
col1.Items.Add("AA");
col1.Items.Add("BB");
col1.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
dgv.Columns.Add(col1);

Please let me know if you have any problem.

Best regards,
Bruce Zhou
Please mark the replies as answers if they help and unmark if they don't.
Bruce.Zhou  Friday, May 29, 2009 3:35 AM
Hi Bruce.Zhou

I have a datagridview with fixed column colBreakfast, collunch, coldinner and in the database there are 3 datatable tblBreakfast, tblLunch, tbldinner. this three datatable has one same column Diet_Name. I want to collect 3 different Diet_Name column from three datatable and make new table in the VS2005 then this table will display in the datagridview.

i.e: When I will collect three column from three table then I will get three column and set this three column to a new table and this table will display in the datagrid three column accordingly


I am trying with your previous code and will let you know

Please help me
Nerurmon1  Saturday, May 30, 2009 4:47 AM


Hi Nerurmon1,
As per my understanding, you have one table which is datasource of datagridview and you have one combobox which have Diet_names collection, and you want to display dietname of respective datagridviewrow.

You should set datasource of Combobox to Diet_name and then add DataBindings to combobox, like

Combobox.DataBindings.Add(string propertyName, object dataSource, string dataMember)
propertyname is the property of combobox, say SelectedItem or Valuemember if you have any,
datasource is the same as datasource of datagridview,
and datamember isthe proeprty of datasource to bind to.


And you no need to handle click event.
NareshG  Tuesday, June 02, 2009 5:40 AM
Hi Nerurmon1,

How's your test result? If there's any difficulty, please let me know.

Best regards,
Bruce Zhou
Please mark the replies as answers if they help and unmark if they don't.
Bruce.Zhou  Wednesday, June 03, 2009 8:56 AM
I have done this but one thing is while i am selecting an item from combo box and then go to next record for select new item it is working fine but I want that if I select new item or change any item it datagridview it should not accept dupplicate means

Egg
Mango
Orange
Banana
Mango '''''''' See when I select mango again in the column then it should show message that exist item and not accept Mango again.

how can I do this
Nerurmon1  Wednesday, June 03, 2009 9:37 AM
Hi , i think u Asked the same question in the last post .. And did not continue that post .. Please do not do like this in this forum .... While Opening one thread . please close it if it is completed .... It will be helpful for others ..













Thanks And Regards
Satya Ranjan Biswal ....
Satya, _S_ Sense  Wednesday, June 03, 2009 9:48 AM
Hi Nerurmon1,

Yes, it's not good to post duplicate thread, and I hope you can close that thread if that answers your question.

Please try the following code if your records are not too many:

void dataGridView2_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
//suppose the comboboxcolumn is the first column
if ( this.dataGridView2.CurrentCell.ColumnIndex == 0)
{
ComboBox cb = e.Control as ComboBox;
cb.SelectionChangeCommitted -= new EventHandler(cb_SelectionChangeCommitted);
cb.SelectionChangeCommitted += new EventHandler(cb_SelectionChangeCommitted);
}
}

void cb_SelectionChangeCommitted(object sender, EventArgs e)
{
ComboBox cb = sender as ComboBox;
for (int i = 0; i < this.dataGridView2.RowCount; i++)
{
if (this.dataGridView2.CurrentCell.RowIndex != i && cb.SelectedIndex!=-1)
{
if (this.dataGridView2.Rows[i].Cells[0].FormattedValue!=null &&
this.dataGridView2.Rows[i].Cells[0].FormattedValue.ToString() == cb.Text)
{
MessageBox.Show("this value has been existed");
cb.SelectedIndex = -1;

}
}
}
}

If there are too many records, to improve performance you can create a dictionary object to store the values which has been used. In the selectionChangeCommitted event handler, you only need to loop a dictionary object after then.

Best regards,
Bruce Zhou



Please mark the replies as answers if they help and unmark if they don't.
  • Marked As Answer byNerurmon1 Wednesday, June 03, 2009 10:42 AM
  •  
Bruce.Zhou  Wednesday, June 03, 2009 10:14 AM
Hi Nerurmon1,

You can add a DataGridViewComboBoxColumn in the DataGridView, and set its DisplayStyle to Nothing. That does exactly you want.

Here's the code to add a column in the DataGridView:

DataGridView dgv = new DataGridView();
this.Controls.Add(dgv);
DataGridViewComboBoxColumn col1 = new DataGridViewComboBoxColumn();
col1.Items.Add("AA");
col1.Items.Add("BB");
col1.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
dgv.Columns.Add(col1);

Please let me know if you have any problem.

Best regards,
Bruce Zhou
Please mark the replies as answers if they help and unmark if they don't.

Hi :)

I have the same problem. But my contents comes from a xml file (Readxml).
So I have 4 columns and I need the comboboxstyle for the first one.
Any chance to do this with your code or are there a property (celltemplate??)
to change the style of the column?

Help would be great :)
Thank you in advance
PSB4ever  Friday, July 10, 2009 8:23 PM
Hi

Did you get some code to work for this i have the same issue where I read in a list of eight columns into the grid from an XML file
and then I want change each column cell to a combobox to look up a list of values for the user to select from to change the imported
value to and then rewrite out to xml.

I have the following code I have tried by putting in a combo column first then reading in the xml but the columns of the xml data just
get added after the combo column in the grid. Any help would be appreciated. Justin


'---add a combobox column---
Dim comboBoxCol As New DataGridViewComboBoxColumn

'---set the header text---
comboBoxCol.HeaderText = "Types"

'---add items to it---
comboBoxCol.Items.Add("Type A")
comboBoxCol.Items.Add("Type B")
comboBoxCol.Items.Add("Type C")

DataGridView1.Columns.Add(comboBoxCol)
'DataGridView1.Columns.Item(1).
'DataGridView1.Columns.Item(1) = New DataGridViewComboBoxColumn
'DataGridView1(1, 1) = New DataGridViewComboBoxCell
'DataGridViewColumn(1) = New DataGridViewComboBoxColumn

'***************************************************************************
' Read the XML data into the dataset.
DataSet1.ReadXml(file)

' Display the DataSet in the DataGridView control.
DataGridView1.DataSource = DataSet1


' Define the parent XML element.
DataGridView1.DataMember = "ProfileViewType"
ralstogj  Wednesday, September 30, 2009 8:32 AM

You can use google to search for other answers

Custom Search

More Threads

• MouseLeave dilemma
• Strange error message trying to connect to a java webservice
• Close startup form
• ToolStrip drop down menu to appear on a button click event
• How to Remove ListItem from a ListBox
• Using class File in Windows for the User control
• Winforms app without a UI
• creating a search form
• MDIClient Control
• DataGridView.CellPainting doesn't paint