By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,213 Members | 2,172 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,213 IT Pros & Developers. It's quick & easy.

changing a datagrid to a combo box

P: n/a
Hi

I have the following code (not mine) that populates a datagrid with some
file names. But I want to replace the datagrid with a combo box.
private void OnCurrentDataCellChanged(object sender, System.EventArgs e)

{try{

DataSet ds = dgMembers.DataSource as DataSet;

txtData.Text =
ds.Tables[0].Rows[dgMembers.CurrentCell.RowNumber].ItemArray.GetValue(0).ToString();

dataset = txtData.Text;

}catch}}

How can i work out what to change to make a combo box, say cmbMembers, have
the values that the datagrid used to have?

Thanks

Doug
Nov 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi,

not quite sure I got u right.

FYI: ComboBox can only display 1 column, DataGrid can display all columns

If you can live with just displaying 1 column you just have to set ComboBox.DataSoure and ComboBox.DisplayMember (column).

If you want to display more than 1 column and are willing to programm a few lines you just leave ComboBox.DataSoure and ComboBox.DisplayMember empty and fill the ComboBox yourself.
// helper class defined in your form to use only with your form
public class ComboBoxDisplayEnhancementRecord {
public DataRow mRow;
public ComboBoxDisplayEnhancementRecord(DataRow iRow) {
mRow = iRow;
}
// the ComboBox Items property uses ToString() to display text no matter WHAT type of
// object contained, so we adapt the ToString() to match our purpose.
public override string ToString() {
return mRow[0].ToString()+" "+mRow[1].ToString();
}
}
// how to fill the combobox
private void button1_Click(object sender, System.EventArgs e) {
// magic here is to just wrap the DataRow with our little enhancer class...
foreach(DataRow Row in dataSet11.Tables[0].Rows)
comboBox1.Items.Add(new ComboBoxDisplayEnhancementRecord(Row));
}
// how to find out which ROW was selected
private void button2_Click(object sender, System.EventArgs e) {
if ((comboBox1.SelectedItem != null) &&
(comboBox1.SelectedItem.GetType() == typeof(ComboBoxDisplayEnhancementRecord)))
MessageBox.Show(
((ComboBoxDisplayEnhancementRecord)comboBox1.Selec tedItem).mRow[2].ToString());
}
Inner classes are cooool!

"Doug" <qu********@hotmail.com> schrieb im Newsbeitrag news:43***********************@news.optusnet.com.a u...
Hi

I have the following code (not mine) that populates a datagrid with some
file names. But I want to replace the datagrid with a combo box.
private void OnCurrentDataCellChanged(object sender, System.EventArgs e)

{try{

DataSet ds = dgMembers.DataSource as DataSet;

txtData.Text =
ds.Tables[0].Rows[dgMembers.CurrentCell.RowNumber].ItemArray.GetValue(0).ToString();

dataset = txtData.Text;

}catch}}

How can i work out what to change to make a combo box, say cmbMembers, have
the values that the datagrid used to have?

Thanks



Doug

Nov 17 '05 #2

P: n/a
Thanks Robert,

I am a beginner and I am just hoping for a tip about a really simple way to change the properties of a single column datagrid to be able to be replaced by a combo box.

Like what things in the code below need to be changed if I want to delete the datagrid and substitute a combo box (ie in my example from dgMembers to cmbMembers).

Also in another part of the form, i have an OldeDbDataAdapter routine that has
OleDbDataAdapter da = new OleDbDataAdapter();

DataSet ds = new DataSet();

da.Fill(ds, adorecordset, "data");

dgMembers.SetDataBinding(ds, "data");

I dont think that i can simply substitute the dgMembers.SetDataBinding(ds, "data") with cmbMembers.SetDataBinding(ds, "data") because SetDataBindings may not be a property of a combobox?

I'm not at the level where i understand you code (even though I am sure your code is very accurate).

is it possible to change "txtData.Text = ds.Tables[0].Rows[dgMembers.CurrentCell.RowNumber].ItemArray.GetValue(0).ToString(); to

something like txtData.Text = cmbMembers.SelectedIndex.ToString; ??

and how would i emulate the SetDataBindings for a combo box?


"Robert Heuvel" <ro***********@isopass.com> wrote in message news:Ot**************@tk2msftngp13.phx.gbl...
Hi,

not quite sure I got u right.

FYI: ComboBox can only display 1 column, DataGrid can display all columns

If you can live with just displaying 1 column you just have to set ComboBox.DataSoure and ComboBox.DisplayMember (column).

If you want to display more than 1 column and are willing to programm a few lines you just leave ComboBox.DataSoure and ComboBox.DisplayMember empty and fill the ComboBox yourself.
// helper class defined in your form to use only with your form
public class ComboBoxDisplayEnhancementRecord {
public DataRow mRow;
public ComboBoxDisplayEnhancementRecord(DataRow iRow) {
mRow = iRow;
}
// the ComboBox Items property uses ToString() to display text no matter WHAT type of
// object contained, so we adapt the ToString() to match our purpose.
public override string ToString() {
return mRow[0].ToString()+" "+mRow[1].ToString();
}
}
// how to fill the combobox
private void button1_Click(object sender, System.EventArgs e) {
// magic here is to just wrap the DataRow with our little enhancer class...
foreach(DataRow Row in dataSet11.Tables[0].Rows)
comboBox1.Items.Add(new ComboBoxDisplayEnhancementRecord(Row));
}
// how to find out which ROW was selected
private void button2_Click(object sender, System.EventArgs e) {
if ((comboBox1.SelectedItem != null) &&
(comboBox1.SelectedItem.GetType() == typeof(ComboBoxDisplayEnhancementRecord)))
MessageBox.Show(
((ComboBoxDisplayEnhancementRecord)comboBox1.Selec tedItem).mRow[2].ToString());
}
Inner classes are cooool!

"Doug" <qu********@hotmail.com> schrieb im Newsbeitrag news:43***********************@news.optusnet.com.a u...
Hi

I have the following code (not mine) that populates a datagrid with some
file names. But I want to replace the datagrid with a combo box.
private void OnCurrentDataCellChanged(object sender, System.EventArgs e)

{try{

DataSet ds = dgMembers.DataSource as DataSet;

txtData.Text =
ds.Tables[0].Rows[dgMembers.CurrentCell.RowNumber].ItemArray.GetValue(0).ToString();

dataset = txtData.Text;

}catch}}

How can i work out what to change to make a combo box, say cmbMembers, have
the values that the datagrid used to have?

Thanks



Doug

Nov 17 '05 #3

P: n/a
You r in a tight spot!

If u really only have a 1 column datagrid problem it is very easy to substitute.

these changes you make with the form designer (SHIFT+F7) and the property editor!

- remove the DataGrid
- place the ComboBox on the form
- have ComboBox.DataSource point to your dataset
- have ComboBox.DisplayMember point to your 1 column

after your "da.Fill" the dataset the ComboBox should display the data.

you don't have to manually "fiddle" around with SetDataBinding & co.

so as to your code:

OleDbDataAdapter da = new OleDbDataAdapter(); // ok
DataSet ds = new DataSet(); // ok
da.Fill(ds, adorecordset, "data"); // ok
dgMembers.SetDataBinding(ds, "data"); // omit

Caution, don't fiddle around in the method InitializeComponent()!
"Doug" <qu********@hotmail.com> schrieb im Newsbeitrag news:43***********************@news.optusnet.com.a u...
Thanks Robert,

I am a beginner and I am just hoping for a tip about a really simple way to change the properties of a single column datagrid to be able to be replaced by a combo box.

Like what things in the code below need to be changed if I want to delete the datagrid and substitute a combo box (ie in my example from dgMembers to cmbMembers).

Also in another part of the form, i have an OldeDbDataAdapter routine that has
OleDbDataAdapter da = new OleDbDataAdapter();

DataSet ds = new DataSet();

da.Fill(ds, adorecordset, "data");

dgMembers.SetDataBinding(ds, "data");

I dont think that i can simply substitute the dgMembers.SetDataBinding(ds, "data") with cmbMembers.SetDataBinding(ds, "data") because SetDataBindings may not be a property of a combobox?

I'm not at the level where i understand you code (even though I am sure your code is very accurate).

is it possible to change "txtData.Text = ds.Tables[0].Rows[dgMembers.CurrentCell.RowNumber].ItemArray.GetValue(0).ToString(); to

something like txtData.Text = cmbMembers.SelectedIndex.ToString; ??

and how would i emulate the SetDataBindings for a combo box?


"Robert Heuvel" <ro***********@isopass.com> wrote in message news:Ot**************@tk2msftngp13.phx.gbl...
Hi,

not quite sure I got u right.

FYI: ComboBox can only display 1 column, DataGrid can display all columns

If you can live with just displaying 1 column you just have to set ComboBox.DataSoure and ComboBox.DisplayMember (column).

If you want to display more than 1 column and are willing to programm a few lines you just leave ComboBox.DataSoure and ComboBox.DisplayMember empty and fill the ComboBox yourself.
// helper class defined in your form to use only with your form
public class ComboBoxDisplayEnhancementRecord {
public DataRow mRow;
public ComboBoxDisplayEnhancementRecord(DataRow iRow) {
mRow = iRow;
}
// the ComboBox Items property uses ToString() to display text no matter WHAT type of
// object contained, so we adapt the ToString() to match our purpose.
public override string ToString() {
return mRow[0].ToString()+" "+mRow[1].ToString();
}
}
// how to fill the combobox
private void button1_Click(object sender, System.EventArgs e) {
// magic here is to just wrap the DataRow with our little enhancer class...
foreach(DataRow Row in dataSet11.Tables[0].Rows)
comboBox1.Items.Add(new ComboBoxDisplayEnhancementRecord(Row));
}
// how to find out which ROW was selected
private void button2_Click(object sender, System.EventArgs e) {
if ((comboBox1.SelectedItem != null) &&
(comboBox1.SelectedItem.GetType() == typeof(ComboBoxDisplayEnhancementRecord)))
MessageBox.Show(
((ComboBoxDisplayEnhancementRecord)comboBox1.Selec tedItem).mRow[2].ToString());
}
Inner classes are cooool!

"Doug" <qu********@hotmail.com> schrieb im Newsbeitrag news:43***********************@news.optusnet.com.a u...
Hi

I have the following code (not mine) that populates a datagrid with some
file names. But I want to replace the datagrid with a combo box.
private void OnCurrentDataCellChanged(object sender, System.EventArgs e)

{try{

DataSet ds = dgMembers.DataSource as DataSet;

txtData.Text =
ds.Tables[0].Rows[dgMembers.CurrentCell.RowNumber].ItemArray.GetValue(0).ToString();

dataset = txtData.Text;

}catch}}

How can i work out what to change to make a combo box, say cmbMembers, have
the values that the datagrid used to have?

Thanks



Doug

Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.