473,320 Members | 2,020 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

DataGridViewComboBoxColumn return wrong index.

I have a datagridview containing a datagridviewcombobox.
The combobox has a dataset containing city_name and city_id.
When I select a new city_name in the combobox I would like to return the city_id corresponding to that city_name.

Code:

//City ComboBox
comboboxCity.DataPropertyName = "city_name";
comboboxCity.HeaderText = "City";
comboboxCity.DataSource = city_table;
comboboxCity.ValueMember = "city_name";
comboboxCity.DisplayMember = comboboxCity.ValueMember;
comboboxCity.Name = "comboboxCity";
dgvVSK.Columns.Insert(20, comboboxCity);

This is the code where I retrieve the selected index after selecting a new city in the combobox.

Code:

private void comboboxControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
comboBox1 = e.Control as ComboBox;

if (comboBox1 != null)
{
comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);

}
}


private void comboBox1_SelectedIndexChanged(object sender, EventArgs e )
{
if (comboBox1 != null)
{
dgvVSK.Rows[dgvVSK.SelectedRows[0].Index].Cells["city_id"].Value = ((ComboBox)sender).SelectedIndex.ToString();
}
}

I do return the selectedindex for that combobox. But it does not correspond to the city_id but to the table row index.

Thanks,
Pierre
Apr 17 '09 #1
1 2694
Bassem
344 100+
dgvVSK.Rows[dgvVSK.SelectedRows[0].Index].Cells["city_id"].Value = ((ComboBox)sender).SelectedIndex.ToString();
Maybe this is the probelm else do,
dgvVSK.Rows[dgvVSK.SelectedRows[0].Index].Cells["city_id"].Value = ((ComboBox)sender).SelectedValue.ToString();
Apr 18 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Shawn | last post by:
I am trying to use a DataGridView to display one column of textboxes with a single value from a query and then next to it have a combobox that displays results from another query. What happens is...
3
by: Rain County | last post by:
I am programmatically building and populating a table and then making it the data source for a dataGridView. I want to add a column to the dataGridView which will be a DataGridViewComboBoxColumn. ...
4
by: John | last post by:
Hi, I've been trying to populate a DataGridViewComboBoxColumn with objects. The object is defined as : private struct manufactItem { public string name; public Guid? man_id;
0
by: Simon Tamman | last post by:
How do other people get around having to define the ValueMember in a DataGridViewComboBoxColumn when binding to a list of business objects? I'm currently doing the following, and it feels wrong...
0
by: Ken | last post by:
Hi I have a little application that does datavalidation. It supports dynamically loaded plugins (you drop a dll with a class implementing IValidator<Tin the same dir as the main application)....
1
by: sklett | last post by:
I have a DataGridView bound to a custom business object. The business object has 2 attributes that are FKs to a lookup table (think HairColorID, EyeColorID, etc) For these 2 columns I have...
0
by: =?ISO-8859-2?B?UmFmYbMgR2llemdhs2E=?= | last post by:
I have: protected void InitControls() { gvProfile.AutoGenerateColumns = false; DataTable dtProfile = new DataTable("KFS"); // dodajemy kolumny foreach(DataGridViewColumn dc in...
0
by: ajey | last post by:
hi all! Im doing a windows application (C#), i have a datagridview bound from a Sql database in which i want one column to be ComboBox.I use DataGridViewComboBoxColumn and bind that column value.......
1
by: =?Utf-8?B?RWQgQ29oZW4=?= | last post by:
I have tried to use the DataGridComboBoxColumn object with limited success. What I want to do is to create one new unbound column to be a combo box (so far, this has worked), populate it with items...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.