Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello i want try my own custom User Control as a Drop Down Box in my DataGridView Combobox Column

What I have tried:

i try google but i not find any solution

sorry for my bad English Grammar
Posted
Updated 6-May-21 3:13am
v2

// 1. Create new project
// 2. Add DataGridView Control to your Form
// 3. Add the following code:

private void AddData()
{
// Add Columns
dataGridView1.ColumnCount = 3;
dataGridView1.Column[0].Name = "Name";
dataGridView1.Column[1].Name = "DOB";
dataGridView1.Column[0].Name = "Tel";

// Add Rows
ArrayList row = new ArrayList();
row.Add("Ahmed");
row.Add("1/1/1990");
row.Add("22332344334");
dataGridView1.Rows.Add(row.ToArray());

// Add second Row
ArrayList row = new ArrayList();
row.Add("Mohamed");
row.Add("1/1/1998");
row.Add("2233234111");
dataGridView1.Rows.Add(row.ToArray());

// Add ComboBoxColumn
DataGridViewComboBoxColumn comboBox1 = new DataGridViewComboBoxColumn();
comboBox1.Name = "comboBox1";
comboBox1.HeaderText = "Job";
row = new ArrayList();
row.AddRange(new String[] {Teacher, Engineer, HouseKeeper, Doctor});
comboBox1.Items.AddRange(row.ToArray());
dataGridView1.Columns.Add(comboBox1);
}


// in the Form Load Event type the following code
private void Form1_Load(object sender, EventArgs e)
{
Add Data();
}

ComboBox comboBox1;

// in EditingControlShoing Event of DataGridView type the following code
private void dataGridView1_EditingControlShowig(object sender, DataGridViewEditingControlShowig e)
{
comboBox1 = e.Control as ComboBox;
if (comboBox1 != null)
{
comboBox1.SelectedIndexChanged -= new EventHandler(comboBox1_SelectedIndexChange);
comboBox1.SelectedIndexChanged += comboBox1_SelectedIndexChange;
}
}


private void comboBox1_SelectedIndexChange(object sender, EventArgs e)
{
string messageTest = (sender as ComboBox).SelectedItem.ToString();
MessageBox.Show(messageTest);
}

// Good Luck
 
Share this answer
 
Comments
Amar chand123 10-May-21 10:08am    
I want my custom user control bind with datagridview textbox column and if i type text or click datagridview textbox column my user control show like combobox dropdown box

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900