Populating a combobox from dataset/datagrid cell value

G

Guest

I'm sure this is a fairly basic question, but I've been looking all over the
web for days for suggestions on how to do this.

I've got a datagrid that's bound to a dataset on my form. It includes
several columns, the last of which (with the header Quantity) contains int16
values. When a user selects a row, I would like to have a combobox on the
form display the values from 1 to the number in the Quantity column (e.g.,
the selected row has 4 in the quantity column, so the combobox would display
1,2,3,4 for it's items).

I think I can populate the combobox once I've gotten the value from the
Quantity column using something like this:

System.Object[] noBottles = new object[quantity];
for (int i= 1; i<= quantity+1; i++)
{
noBottles=i;
}
cmbNumberOfBottles.Items.AddRange(noBottles);
cmbNumberOfBottles.SelectedIndex[0];


where 'quantity' is the integer from the Quantity column, but for the life
of me I can't get that Quantity number into a useable int format for this.
I'm sure this comes across as Grade-A programming newbie (which I am), but
any help would be appreciated. Also, if my combobox populating method looks
screwy, any advice would be very helpful there as well.

Thanks so much in advance!
 
C

Chris

Robert said:
I'm sure this is a fairly basic question, but I've been looking all over the
web for days for suggestions on how to do this.

I've got a datagrid that's bound to a dataset on my form. It includes
several columns, the last of which (with the header Quantity) contains int16
values. When a user selects a row, I would like to have a combobox on the
form display the values from 1 to the number in the Quantity column (e.g.,
the selected row has 4 in the quantity column, so the combobox would display
1,2,3,4 for it's items).

I think I can populate the combobox once I've gotten the value from the
Quantity column using something like this:

System.Object[] noBottles = new object[quantity];
for (int i= 1; i<= quantity+1; i++)
{
noBottles=i;
}
cmbNumberOfBottles.Items.AddRange(noBottles);
cmbNumberOfBottles.SelectedIndex[0];


where 'quantity' is the integer from the Quantity column, but for the life
of me I can't get that Quantity number into a useable int format for this.
I'm sure this comes across as Grade-A programming newbie (which I am), but
any help would be appreciated. Also, if my combobox populating method looks
screwy, any advice would be very helpful there as well.

Thanks so much in advance!


You need to take the value from the DataTable (or DataSet depending on
what you bound) from the datasource of the datagrid.

I'm a vb guy so I might get the syntax wrong....

DataTable DT = (DataTable) DataGrid.DataSource
Integer I = (Integer) DT.Rows[DataGrid.CurrentRowindex]

Something like that.

Chris
 
C

Chris

Chris said:
Robert said:
I'm sure this is a fairly basic question, but I've been looking all
over the web for days for suggestions on how to do this.
I've got a datagrid that's bound to a dataset on my form. It includes
several columns, the last of which (with the header Quantity) contains
int16 values. When a user selects a row, I would like to have a
combobox on the form display the values from 1 to the number in the
Quantity column (e.g., the selected row has 4 in the quantity column,
so the combobox would display 1,2,3,4 for it's items).
I think I can populate the combobox once I've gotten the value from
the Quantity column using something like this:

System.Object[] noBottles = new object[quantity];
for (int i= 1; i<= quantity+1; i++)
{
noBottles=i;
}
cmbNumberOfBottles.Items.AddRange(noBottles);
cmbNumberOfBottles.SelectedIndex[0];


where 'quantity' is the integer from the Quantity column, but for the
life of me I can't get that Quantity number into a useable int format
for this. I'm sure this comes across as Grade-A programming newbie
(which I am), but any help would be appreciated. Also, if my combobox
populating method looks screwy, any advice would be very helpful there
as well.

Thanks so much in advance!



You need to take the value from the DataTable (or DataSet depending on
what you bound) from the datasource of the datagrid.

I'm a vb guy so I might get the syntax wrong....

DataTable DT = (DataTable) DataGrid.DataSource
Integer I = (Integer) DT.Rows[DataGrid.CurrentRowindex]

Something like that.

Chris


Correction:
Integer I = (Integer) DT.Rows[DataGrid.CurrentRowindex][ColumnName]

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top