changing a datagrid to a combo box

D

Doug

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
 
R

Robert Heuvel

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.SelectedItem).mRow[2].ToString());
}
Inner classes are cooool!
 
D

Doug

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?






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.SelectedItem).mRow[2].ToString());
}
Inner classes are cooool!

Doug said:
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
 
R

Robert Heuvel

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()!


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?






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.SelectedItem).mRow[2].ToString());
}
Inner classes are cooool!

Doug said:
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
 

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