On form2 (c # winforms vs2010 net4.0) there is a DataGridView1 in which there is a DatagridviewCombobox. Please tell me how to do the following for DatagridviewCombobox: when you click the dropdown arrow, open Form3 (on Form3 there is a ListBox1 line filled with the OK button), then you should return the highlighted line from ListBox1 as a value for DatagridviewCombobox and Form3 when you click the OK button to close? The code below works, but it’s not possible to close the DatagridviewCombobox list correctly, that is, after clicking on the dropdown arrow (in DatagridviewCombobox), form3 opens, then one line in ListBox1 is selected on Form3 and the ok button is pressed (btn1) DatagridviewCombobox cell value does not display a drop-down list.
Form code2:
namespace Test { public partial class Form2 : Form { DataSet ds = new DataSet(); private DataSet InitializeDataSet() { //...код загрузки всех таблиц из файла mdb return ds; } public Form2() { InitializeComponent(); DataSet ds = InitializeDataSet(); } private void Form2_Load(object sender, EventArgs e) { } ComboBox cb6; DataGridViewCell currentCell; private void DataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { if (DataGridView1.CurrentCell.ColumnIndex == 5) //cb6 { if (e.Control is ComboBox) { cb6 = (ComboBox)e.Control; if (cb6 != null) { cb6.DropDown += new EventHandler(cb6_DropDown); } currentCell = DataGridView1.CurrentCell; } } } private void DataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { if (cb6 != null) { cb6.DropDown -= new EventHandler(cb6_DropDown); } } //Добавить строку: private void btnAddRow_Click(object sender, EventArgs e) { int index = DataGridView1.Rows.Count; index++; DataGridView1.Rows.Add(); } //Показать форму3: private void cb6_DropDown(object sender, EventArgs e) { Form3 frm3 = new Form3(); //список для ListBox1: DataTable dt1 = ds.Tables["СПИСОК1"]; ArrayList List1 = new ArrayList(); foreach (DataRow item in dt1.Rows) { List1.Add(item["Полное_название"].ToString()); } frm3.ListBox1.DataSource = null; frm3.ListBox1.Items.Clear(); frm3.ListBox1.DataSource = List1; if (frm3.ShowDialog(this) == DialogResult.OK) { DataGridViewComboBoxCell Col6 = (DataGridViewComboBoxCell)DataGridView1.Rows[DataGridView1.CurrentCell.RowIndex].Cells["Column6"]; Col6.Items.Add(frm3.getItem1()); Col6.Value = ""; Col6.Value = frm3.getItem1(); DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit); DataGridView1.EndEdit(); } else { return; } frm3.Close(); frm3.Dispose(); } } }
Form code3:
namespace Test { public partial class Form3 : Form { public Form3() { InitializeComponent(); btn1.DialogResult = DialogResult.OK; btn2.DialogResult = DialogResult.Cancel; } private void Form3_Load(object sender, EventArgs e) { } private void btn1_Click(object sender, EventArgs e) { } private void btn2_Click(object sender, EventArgs e) { this.Close(); } public string getItem1() { if (!DBNull.Value.Equals(ListBox1.SelectedItem.ToString())) return ListBox1.SelectedItem.ToString(); else return String.Empty; } } }
DataGridView
, anEditor
object is created for it, which is displayed in place of the cell. If you are trying to change the default behavior of theEditor
, then you should write the new value in theEditor
, and not in theDataGridView
directly. Try changing the text in the object((ComboBox)sender)
in thecb6_DropDown
methodcb6_DropDown
in theDataGridView1
object. - kodv