Mute Posted October 27, 2003 Posted October 27, 2003 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? Quote
Mehyar Posted October 27, 2003 Posted October 27, 2003 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, Quote Dream as if you'll live forever, live as if you'll die today
Mute Posted October 27, 2003 Author Posted October 27, 2003 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. Quote
Mehyar Posted October 27, 2003 Posted October 27, 2003 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. Quote Dream as if you'll live forever, live as if you'll die today
Mute Posted October 28, 2003 Author Posted October 28, 2003 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. Quote
*Experts* mutant Posted October 28, 2003 *Experts* Posted October 28, 2003 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. Quote
Mehyar Posted October 28, 2003 Posted October 28, 2003 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? Quote Dream as if you'll live forever, live as if you'll die today
Leaders dynamic_sysop Posted October 28, 2003 Leaders Posted October 28, 2003 (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 October 28, 2003 by dynamic_sysop Quote
Mute Posted October 29, 2003 Author Posted October 29, 2003 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 Quote
Mute Posted October 30, 2003 Author Posted October 30, 2003 Because I needed only the current Selectted Item, I solved the problem using the STATIC member. Quote
*Experts* jfackler Posted October 30, 2003 *Experts* Posted October 30, 2003 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 Quote
Leaders dynamic_sysop Posted October 30, 2003 Leaders Posted October 30, 2003 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) Quote
*Experts* jfackler Posted October 30, 2003 *Experts* Posted October 30, 2003 DirectCast should have occured to me. Thanks, Jon Quote
tetsuroy Posted December 2, 2003 Posted December 2, 2003 Use ComboBox.Text Property Following should work for you. FirstForm fm = new FirstForm(); MessageBox.Show(fm.Combo.Text); Tetsuro Quote
Leaders dynamic_sysop Posted December 2, 2003 Leaders Posted December 2, 2003 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. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.