There are two ways to do this: the first is to use the Datagridviewtextboxcolumn control that comes with DataGridView, and the second is to dynamically add a ComboBox control method, one of which is to first drag a DataGridView then adds two columns datagridviewtextboxcolumn in this DataGridView (the first column is called A, the second column is B) and then binds a code a.datasource= ds. tables[0]. DefaultView; A.displaymember="table_name"; A.valuemember="table_name"; ((datagridviewcomboboxcolumn) datagridview1.columns[0]). Defaultcellstyle.nullvalue ="--Please choose- -";//Default Valuefollowed by binding B code//The second column of the currently selected row is assigned a value((DataGridViewComboBoxCell) Datagridview1.rows[datagridview1.currentcell.rowindex]. cells[1]). DataSource = ds. tables[0]. DefaultView; ((DataGridViewComboBoxCell) Datagridview1.rows[datagridview1.currentcell.rowindex]. cells[1]). DisplayMember ="Comments"; ((DataGridViewComboBoxCell) Datagridview1.rows[datagridview1.currentcell.rowindex]. cells[1]). ValueMember ="column_name"; ((datagridviewcomboboxcolumn) datagridview1.columns[2]). Defaultcellstyle.nullvalue ="--Please choose- -"; then add the SelectedIndexChanged event codePrivate voidDatagridview1_editingcontrolshowing (Objectsender, Datagridvieweditingcontrolshowingeventargs e) {DataGridView DGV=(DataGridView) sender; if(DGV. CurrentCell.OwningColumn.Name = ="A") {ComboBox CB=(ComboBox) E.control; Cb. SelectedIndexChanged+=NewEventHandler (combobox_selectedindexchanged); }} SelectedIndexChanged event code Public voidCombobox_selectedindexchanged (Objectsender, EventArgs e) {ComboBox ComboBox=(ComboBox) sender; if(DataGridView1.CurrentCell.OwningColumn.Name = ="Table name") { if(Combobox.text! ="") { //This is the method of binding BDbfieldnote (Combobox.text); }}} Method Two: Instantiate the ComboBox object FirstPrivateComboBox combobox =NewComboBox (); PrivateComboBox cb =NewComboBox (); second: This. DATAGRIDVIEW1.CONTROLS.ADD (ComboBox);//to add a control to the DataGridViewDbtablename ();//Binding ComboBoxCombobox.visible =false; Combobox.selectedindexchanged+=NewEventHandler (combobox_selectedindexchanged);//Add Event Private voidCombobox_selectedindexchanged (Objectsender, EventArgs e) { stringName =((ComboBox) sender). Text; DataGridView1.CurrentCell.Value= name;//adds the selected value to the currently selected cell in DataGridViewDbfieldnote (name);//bind a second combobox (aka CB) } Public voidDbfieldnote (stringtablename) { This. DATAGRIDVIEW1.CONTROLS.ADD (CB); Cb. Visible=false; Cb. SelectedIndexChanged+=NewEventHandler (cb_selectedindexchanged); ...... } Private voidCb_selectedindexchanged (Objectsender, EventArgs e) {DataGridView1.CurrentCell.Value=CB. Text; } Private voidDatagridview1_currentcellchanged (Objectsender, EventArgs e) { if(Datagridview1.currentcell! =NULL) { if(DataGridView1.CurrentCell.ColumnIndex = =1)//If the first column is selected, the first ComboBox is displayed{System.Drawing.Rectangle rect= Datagridview1.getcelldisplayrectangle (DataGridView1.CurrentCell.ColumnIndex, DataGridView1.CurrentCell.RowIndex,false);//gets the properties of the currently selected cell (wide, high)Combobox.left =rect. Left; Combobox.top=rect. Top; Combobox.width=rect. Width; Combobox.height=rect. Height; Combobox.visible=true; } Else if(datagridview1.currentcell.columnindex==2)//If the second column is selected, the CB is displayed{System.Drawing.Rectangle Rect1= Datagridview1.getcelldisplayrectangle (Comboxindex +1, DataGridView1.CurrentCell.RowIndex,false); Cb. Left=rect1. Left; Cb. Top=rect1. Top; Cb. Width=rect1. Width; Cb. Height=rect1. Height; Cb. Visible=true; } } Else{combobox.visible=false; } }
Linkage treatment of datagridviewtextboxcolumn in Winfrom DataGridView