472,787 Members | 1,463 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

changing a datagrid to a combo box

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
3 2939
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: PeterZ | last post by:
G'day, After doing much searching and pinching bits of ideas from here there and everywhere I came up with a fairly 'clean' solution of including a comboBox into a dataGrid column. You can...
0
by: JL3574 l | last post by:
i have a datagrid with a combo box added in it's columns . the combobox pulls values from a database in it's dropdownlist fashion. my problem is that when i pick a select on the combo box it...
4
by: Ziv Riezman | last post by:
Hi All I Have a datagrid on asp.net. i need to add a combo box column that chages it's data according to a spefiiec row in a datagrid. Please help me ?? Thnaks Ziv Riezman
2
by: pmcguire | last post by:
OK. So I've been to http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp and learned a lot about what I might do with a datagrid. But I'm STILL not able to do what I want to do. I want to be able...
2
by: pmcguire | last post by:
I have derived a ComboBoxColumnStyle that inherits DataGridColumnStyle. It works fine except for one behavior. If the user selects a new value from the ComboBox's pulldown list on a brand new...
1
by: jimb | last post by:
I can get the dropdownlist into the datagrid, and I can populate it, but I can't read it. Anybody have a working example of a dropdownlist in an editable grid? Thanks. -- .....
6
by: Ron L | last post by:
I have a dataset whose source is a SQL 2k stored procedure that I am trying to display in a datagrid. This datasource has 4 columns that I am interested in here, a text column and 3 value columns...
4
by: Jan Nielsen | last post by:
Hi all I'm a former Access developer who would like to implement a many-to-many relation in about the same way you do in Access: With a subform and a combo box. Is it possible to use a...
2
by: kenglish_hi | last post by:
hello, I have a form with a datagrid and combo box. When the form loads up, i want the focus to be on the combo box. however, whenever it loads up, the focus is always on the datagrid. in the...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.