Windows Develop Bookmark and Share   
 index > Windows Forms Sample Applications > combobox in datagridview
 

combobox in datagridview

I placed combobox as one of the cells in datagridview.when i have to update a row i m selecting a value from the combobox but i m able to select the value from the combobox not after a single but after 2 or 3 clicks
why is it happening i m not able to know.And when i m trying to get the selected value of combobox and the selected Item i m getting only from the first row combobox.When i am trying to select the 2nd row combobox i am getting -1 as the selected index after 3 clicks on combobox and nothing as selectedItem.
Any help please

public partial class Form1 : Form

{

SqlConnection sqlcon = new SqlConnection("Data Source=RAJANI\\SQL2005;Initial Catalog=usha;Integrated Security=True");

public Form1()

{

InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e)

{

fillgrid();

//code for populating the combobox

DataSet ds1 = new DataSet();

SqlDataAdapter da1 = new SqlDataAdapter("select * from states", sqlcon);

da1.Fill(ds1,

"states");

colstate.DataSource = ds1.Tables[

"states"];

colstate.DisplayMember =

"state";

colstate.ValueMember =

"number";

colstate.HeaderText =

"STATES";

colstate.AutoSizeMode =

DataGridViewAutoSizeColumnMode.ColumnHeader;

}

public void fillgrid()

{

//code for filling the datagridview

DataSet ds = new DataSet();

SqlDataAdapter da = new SqlDataAdapter("select * from citys", sqlcon);

da.Fill(ds,

"citys");

dataGridView2.DataSource = ds.Tables[

"citys"];

}

private void dataGridView2_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)

{

ComboBox editingComboBox = (ComboBox)e.Control;

if (editingComboBox != null)

editingComboBox.SelectedIndexChanged +=

new System.EventHandler(this.editingComboBox_SelectedIndexChanged);

}

private void editingComboBox_SelectedIndexChanged(object sender, System.EventArgs e)

{

ComboBox colstate= (ComboBox)sender;

//Display index

MessageBox.Show(colstate.SelectedIndex.ToString());

//Display value

MessageBox.Show(colstate.Text);

}

usha.talasila  Friday, July 10, 2009 4:52 PM
Hi usha.talasila,

The showing ComboBox action is by design, the first click will select that cell and the second click will begin to show the ComboBox. You cannot change it to just one click show a ComboBox.

The second question. Based on my test, it does as your description. I don't found any document talking about it. Here is my understanding.
1. When you select an empty cell(new row cell), the value is null, so the ComboBox SelectedIndex will be initialized as -1, When you click the second time, ComboBox will be shown, It always select the previous SelectedIndex, so the selectedIndexChanged event fire the second time and return the last index you selected.

Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Monday, July 13, 2009 8:16 AM
Hi,
Thanks for the reply.Can u please let me know the alternate solution for this.
usha.talasila  Monday, July 13, 2009 2:59 PM
Hi usha.talasila,

I am not clear what kind of solution do you want? You don't want to see the -1? So what about put an if to check the SelectedIndex and if it is -1, don't accept.

Please tell me if I misunderstood you.

Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Tuesday, July 14, 2009 1:56 AM

I need the selected item and the selected index of the combobox in datagridview

Please let me know

thanks in advance.

usha.talasila  Tuesday, July 14, 2009 4:26 AM
Hi usha.talasila,

Then what about do a check to the SelectedIndex value, if it is -1, just ignore it. If not, use that value.

Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Tuesday, July 14, 2009 5:42 AM

You can use google to search for other answers

Custom Search

More Threads

• missing .DLL file, MSCOREE.DLL
• Showing C# Intellisense in TextBox
• Wrong versions in About TaskVision
• Blogs?
• taskvision can not work on w2k3
• workaround for the expiring ticket during idle time
• Cant upload my creatures
• Firewall/Proxy Question
• Want help related to Windows Forms Controls
• Direct ADO instead of Web Service