Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I have a ComboBox in one WinForm, and I want to use its data in another WinForm, I try this:

...

FirstForm fm = new FirstForm();

MessageBox.Show(fm.Combo.SelectedItem.ToString());

...

it gives the following string:

System.Data.DataRowView

not items.

 

the fm.Combo.Text gives in result just first item text.

what I am doing wrong?

Posted

The items of a bounded to a table or dataview comboxes are DataRowViews,

 

try this instead, it would return the data contained in the column of this row view (write the column name you want)

MessageBox.Show(fm.Combo.SelectedItem.Row("TheColumnNameOfTheTextFieldMember");

 

Hope this helps,

Dream as if you'll live forever, live as if you'll die today
Posted

Hey,

I cant find "Row" method for "ComboBox.SelectedItem"

are you sure it has Row method?

 

as I said, it shows the selected item data in main form,

but I fail to show the selected item data in another form.

Posted
it doesnot show in the intellisense but it is there, this is late binding because the item could be anything not always a DataRowView, try it it would work.
Dream as if you'll live forever, live as if you'll die today
Posted

Mehyar,

as I see you are much better in C# than me, (I program in C++) it is a week as started C#,

almost a day I am trying to solve this problem with Combo

have you ever tried that, i mean read the data in Combo from another WinForm?

if yes, could you show a little example.

 

I followed you and did following:

MainForm mf = new MainForm();

DataView dv = (DataView) mf.Combo.DataSource;

dv.RowStateFilter = DataViewRowState.ModifiedCurrent;

foreach(DataRowView drv in dv)

MessageBox.Show(drv[mf.Groups.Text].ToString());

 

the result is zero,

what I need is, the current selected Row in Combo.

  • *Experts*
Posted
You wont get access to the ComboBox on another form that is already open that way. You need to have the instance of the form that is already open, not create a new one everytime.
Posted
Ok in order to help you i need you to tell me what do you need to do exactly, do you need to list the elements of the combo box or just get the selected element and display it in the 2nd winform?
Dream as if you'll live forever, live as if you'll die today
  • Leaders
Posted (edited)

if you are opening a second form and then wish to access controls / info on the first form, try this...

In the First Form:

// in Form1 , when opening Form2
SecondForm scndFrm = new SecondForm(); // your second Form's name would go here
base.AddOwnedForm(scndFrm);
scndFrm.Show();

in the second Form:

// to access the stuff on Form1...
FirstForm fm = (Form1)this.Owner;
MessageBox.Show(fm.Combo.SelectedItem.ToString());

Edited by dynamic_sysop

Posted

Here I attached ZIPped archive of project, please look at this.

I nave such an error when I try to read the current selected Item from ComboBox:

Object reference is not set to an instance of object.

temp.zip

  • *Experts*
Posted

dynamic_sysop,

Can you do that without turning off option strict?

I can't seem to find a vb version that will not send up an option strict violation in intellisense.

 

Jon

  • Leaders
Posted

is there an option strict in C# :-\ , i'd try it with option strict ON , but as far as i can see there isn't an option for it.

you have to specify the name of the Form on the left and the same name in the brackets when casting the form, so " FirstForm fm = (Form1)this.Owner; " would error , because it says (Form1) , but that was just an example.

if you had a form that was actually called FirstForm , then you would do the following in C# ...

FirstForm frmFirst=(FirstForm)base.Owner;
// subject to FirstForm being the Owner of the Form you are calling from.

if you were using vb.net you would directcast ( if Option Strict was ON ) , eg:

Dim frmFirst As FirstForm = DirectCast(MyBase.Owner,Form)

  • 1 month later...
  • Leaders
Posted

Re: Use ComboBox.Text Property

 

Following should work for you.

 

FirstForm fm = new FirstForm();

MessageBox.Show(fm.Combo.Text);

 

Tetsuro

if only it was that easy :rolleyes:

you can create a new instance of a Form like that yes, but you wont access an existing Form / it's values that way.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...