Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Help With a Combo Problem

Help With a Combo Problem

Scheduled Pinned Locked Moved C#
helpdatabase
21 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M monica2k

    can u pls elaborate a bit more.

    C Offline
    C Offline
    Christian Graus
    wrote on last edited by
    #4

    well, the page class has a property, IsPostback. That tells your page if you're viewing for the first time, or not. If you don't use this, and you set the datasource every time you load the page, including postback, you will lose your selected index, because it will revert to the first item of the data source you've just set. SO, when you set the data source and databind, you should first check if the page is being loaded for the first time. IF not, your ViewSate contains the contents of the control, as well as the selected Index, and you don't want to replace that. if (!IsPostBack) { combo1.DataSource = GetDataSource(); combo1.DataBind(); } Christian Graus - Microsoft MVP - C++

    M 1 Reply Last reply
    0
    • C Christian Graus

      well, the page class has a property, IsPostback. That tells your page if you're viewing for the first time, or not. If you don't use this, and you set the datasource every time you load the page, including postback, you will lose your selected index, because it will revert to the first item of the data source you've just set. SO, when you set the data source and databind, you should first check if the page is being loaded for the first time. IF not, your ViewSate contains the contents of the control, as well as the selected Index, and you don't want to replace that. if (!IsPostBack) { combo1.DataSource = GetDataSource(); combo1.DataBind(); } Christian Graus - Microsoft MVP - C++

      M Offline
      M Offline
      monica2k
      wrote on last edited by
      #5

      i Think i was unable to explain my problem I am making a windows application. i am not changing the datasource of my first combo after the form is loaded. My problem taht first time when i set the datasource property of my first combo then it calls its SelectedIndexChanged where i am having code to populate the second combo by querying for selectedvalueof first combo. and similary for third one. is any simpler solution which works is there Thanks Monica

      C 1 Reply Last reply
      0
      • M monica2k

        i Think i was unable to explain my problem I am making a windows application. i am not changing the datasource of my first combo after the form is loaded. My problem taht first time when i set the datasource property of my first combo then it calls its SelectedIndexChanged where i am having code to populate the second combo by querying for selectedvalueof first combo. and similary for third one. is any simpler solution which works is there Thanks Monica

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #6

        monica2k wrote: I am making a windows application Oh, OK. :-O So you're saying that the selected index does not exist when you get a selected index changed event ? Or what ? I think you need to post some code. Christian Graus - Microsoft MVP - C++

        M 1 Reply Last reply
        0
        • C Christian Graus

          monica2k wrote: I am making a windows application Oh, OK. :-O So you're saying that the selected index does not exist when you get a selected index changed event ? Or what ? I think you need to post some code. Christian Graus - Microsoft MVP - C++

          M Offline
          M Offline
          monica2k
          wrote on last edited by
          #7

          This Is my Main Combo Code To populate public static void PopulateMainCombo( ComboBox Combo, string ConnectionString, string TableName, string TextField, string ValueField, string DefaultValue ) { Combo.DataSource = null; string Query="select * from " + TableName; Query = Query + " order by " + TextField; Combo.DataSource=DataManager.ConnectingForCombo(ConnectionString,Query,TableName); Combo.DisplayMember = TextField; Combo.ValueMember = ValueField; } private void cmbMainDiagnosis_SelectedIndexChanged(object sender, System.EventArgs e) { bool blnVisible; blnSubDiagnosis=false; int intSelectedValue; intSelectedValue=(int) this.cmbMainDiagnosis.SelectedValue; cmbSubDiagnosis.Visible=false; cmbSubSubDiagnosis.Visible=false; blnVisible=Diagnosis.SetDiagnosisCombos(intSelectedValue,this.cmbSubDiagnosis,"tblsubdiagnosis_mas","strSubDiagnosisDesc","intMainDiagnosisCode","0"); blnSubDiagnosis=blnVisible; if (blnVisible==true) { cmbSubDiagnosis.Visible=true; } else cmbSubDiagnosis.Visible=false; } ///////////////////////////////////////////////////////////////////////////// and this is my Second Combo that is cmbsubDiagnosis on change of which my third combo that is cmbSubSubDiagnisis is to be populated. //////////////////////////////////////////////////////////////////////////// private void cmbSubDiagnosis_SelectedIndexChanged(object sender, System.EventArgs e) { if (blnSubDiagnosis==true) { bool blnVisible; int intSelectedValue; intSelectedValue=(int)this.cmbSubDiagnosis.SelectedValue; cmbSubSubDiagnosis.Visible=false; blnVisible=Diagnosis.SetDiagnosisCombos(intSelectedValue,this.cmbSubSubDiagnosis,"tblsubsubdiagnosis_mas","strSubsubDiagnosisDesc","intSubDiagnosisCode","0"); if (blnVisible==true) { cmbSubSubDiagnosis.Visible=true; } else cmbSubSubDiagnosis.Visible=false; } } /////////////////////////////////////////////////////////////////////////// and this is the function i use to puplate 2nd and 3 rd combo /////////////////////////////////////////////////////////////////////////// public static bool SetDiagnosisCombos(int paramDiagnosisCode,ComboBox Combo,string TableName, string TextField, string ValueField, string DefaultValue ) { String ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["DSN"]; Combo.DataSource = null; String query

          C 1 Reply Last reply
          0
          • M monica2k

            This Is my Main Combo Code To populate public static void PopulateMainCombo( ComboBox Combo, string ConnectionString, string TableName, string TextField, string ValueField, string DefaultValue ) { Combo.DataSource = null; string Query="select * from " + TableName; Query = Query + " order by " + TextField; Combo.DataSource=DataManager.ConnectingForCombo(ConnectionString,Query,TableName); Combo.DisplayMember = TextField; Combo.ValueMember = ValueField; } private void cmbMainDiagnosis_SelectedIndexChanged(object sender, System.EventArgs e) { bool blnVisible; blnSubDiagnosis=false; int intSelectedValue; intSelectedValue=(int) this.cmbMainDiagnosis.SelectedValue; cmbSubDiagnosis.Visible=false; cmbSubSubDiagnosis.Visible=false; blnVisible=Diagnosis.SetDiagnosisCombos(intSelectedValue,this.cmbSubDiagnosis,"tblsubdiagnosis_mas","strSubDiagnosisDesc","intMainDiagnosisCode","0"); blnSubDiagnosis=blnVisible; if (blnVisible==true) { cmbSubDiagnosis.Visible=true; } else cmbSubDiagnosis.Visible=false; } ///////////////////////////////////////////////////////////////////////////// and this is my Second Combo that is cmbsubDiagnosis on change of which my third combo that is cmbSubSubDiagnisis is to be populated. //////////////////////////////////////////////////////////////////////////// private void cmbSubDiagnosis_SelectedIndexChanged(object sender, System.EventArgs e) { if (blnSubDiagnosis==true) { bool blnVisible; int intSelectedValue; intSelectedValue=(int)this.cmbSubDiagnosis.SelectedValue; cmbSubSubDiagnosis.Visible=false; blnVisible=Diagnosis.SetDiagnosisCombos(intSelectedValue,this.cmbSubSubDiagnosis,"tblsubsubdiagnosis_mas","strSubsubDiagnosisDesc","intSubDiagnosisCode","0"); if (blnVisible==true) { cmbSubSubDiagnosis.Visible=true; } else cmbSubSubDiagnosis.Visible=false; } } /////////////////////////////////////////////////////////////////////////// and this is the function i use to puplate 2nd and 3 rd combo /////////////////////////////////////////////////////////////////////////// public static bool SetDiagnosisCombos(int paramDiagnosisCode,ComboBox Combo,string TableName, string TextField, string ValueField, string DefaultValue ) { String ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["DSN"]; Combo.DataSource = null; String query

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #8

            And what goes wrong ? =(int)this.cmbSubDiagnosis.SelectedValue; Is it possible that selectedValue is a string and you need Convert.ToInt16 instead of the cast ? Christian Graus - Microsoft MVP - C++

            M 1 Reply Last reply
            0
            • C Christian Graus

              And what goes wrong ? =(int)this.cmbSubDiagnosis.SelectedValue; Is it possible that selectedValue is a string and you need Convert.ToInt16 instead of the cast ? Christian Graus - Microsoft MVP - C++

              M Offline
              M Offline
              monica2k
              wrote on last edited by
              #9

              yes exactly the problem comes here. what to do next

              C 1 Reply Last reply
              0
              • M monica2k

                yes exactly the problem comes here. what to do next

                C Offline
                C Offline
                Christian Graus
                wrote on last edited by
                #10

                1. Change (int) to Convert.ToInt32( (like I said ) 2. Step through the debugger in future to see what's going on when you've got a line that blows up 3. Next time you ask a question, if you know where the code blows up, then give us the code, and either way, tell us what the error is you're getting. Christian Graus - Microsoft MVP - C++

                M 1 Reply Last reply
                0
                • C Christian Graus

                  1. Change (int) to Convert.ToInt32( (like I said ) 2. Step through the debugger in future to see what's going on when you've got a line that blows up 3. Next time you ask a question, if you know where the code blows up, then give us the code, and either way, tell us what the error is you're getting. Christian Graus - Microsoft MVP - C++

                  M Offline
                  M Offline
                  monica2k
                  wrote on last edited by
                  #11

                  Its Still giving error at the same point and the error is An unhandled exception of type 'System.InvalidCastException' occurred in mscorlib.dll Additional information: Specified cast is not valid.

                  C 1 Reply Last reply
                  0
                  • M monica2k

                    Its Still giving error at the same point and the error is An unhandled exception of type 'System.InvalidCastException' occurred in mscorlib.dll Additional information: Specified cast is not valid.

                    C Offline
                    C Offline
                    Christian Graus
                    wrote on last edited by
                    #12

                    Step 2 - use the debugger to find why the Value can't cast to a number. Christian Graus - Microsoft MVP - C++

                    M 1 Reply Last reply
                    0
                    • C Christian Graus

                      Step 2 - use the debugger to find why the Value can't cast to a number. Christian Graus - Microsoft MVP - C++

                      M Offline
                      M Offline
                      monica2k
                      wrote on last edited by
                      #13

                      when i used the debugger it shows that the selectedvalue has "System.Data.DataRowView" value that is why its not converting it but what is the solution to it.

                      C 1 Reply Last reply
                      0
                      • M monica2k

                        when i used the debugger it shows that the selectedvalue has "System.Data.DataRowView" value that is why its not converting it but what is the solution to it.

                        C Offline
                        C Offline
                        Christian Graus
                        wrote on last edited by
                        #14

                        OK, so that's what you need to cast it to, and then get the value out of it. Christian Graus - Microsoft MVP - C++

                        M 1 Reply Last reply
                        0
                        • C Christian Graus

                          OK, so that's what you need to cast it to, and then get the value out of it. Christian Graus - Microsoft MVP - C++

                          M Offline
                          M Offline
                          monica2k
                          wrote on last edited by
                          #15

                          Casting to Int32 is also not working . It is giving same error Is there any other way to achive my aim.Now where should i look. I am doing nothing different. Is there any other way to fill the combo with the rows from table with their code as value member. or is there any otehr event which i can use.

                          R C 2 Replies Last reply
                          0
                          • M monica2k

                            Casting to Int32 is also not working . It is giving same error Is there any other way to achive my aim.Now where should i look. I am doing nothing different. Is there any other way to fill the combo with the rows from table with their code as value member. or is there any otehr event which i can use.

                            R Offline
                            R Offline
                            Robert Rohde
                            wrote on last edited by
                            #16

                            Instead of casting to Int32 you have to first cast to System.Data.DataRowView. If you have this you can use its indexer to access the element you need. After that cast this element to Int32 and you are done.

                            M 1 Reply Last reply
                            0
                            • R Robert Rohde

                              Instead of casting to Int32 you have to first cast to System.Data.DataRowView. If you have this you can use its indexer to access the element you need. After that cast this element to Int32 and you are done.

                              M Offline
                              M Offline
                              monica2k
                              wrote on last edited by
                              #17

                              how to do that can u pls explain i am new to C#

                              A R 2 Replies Last reply
                              0
                              • M monica2k

                                how to do that can u pls explain i am new to C#

                                A Offline
                                A Offline
                                AfzalHassen
                                wrote on last edited by
                                #18

                                are you getting this problem only when the form is loading?? Afzal Hassen

                                1 Reply Last reply
                                0
                                • M monica2k

                                  how to do that can u pls explain i am new to C#

                                  R Offline
                                  R Offline
                                  Robert Rohde
                                  wrote on last edited by
                                  #19

                                  As Im really in a hurry here just a small snippet:

                                  DataRovwView drv = (DataRowView)this.cmbSubDiagnosis.SelectedValue;
                                  int myIntValue = (int)drv[IndexOrNameOfTheColumnYouWishToAccess];

                                  1 Reply Last reply
                                  0
                                  • M monica2k

                                    Casting to Int32 is also not working . It is giving same error Is there any other way to achive my aim.Now where should i look. I am doing nothing different. Is there any other way to fill the combo with the rows from table with their code as value member. or is there any otehr event which i can use.

                                    C Offline
                                    C Offline
                                    Christian Graus
                                    wrote on last edited by
                                    #20

                                    Your core problem is that you're shoving a datarowview into the value, the problem really is when you set the data source. You need to tell the combo which property to use to set the value for each row. The problem has nothing to do with the event. You have a datarowview in the value, you cannot magically turn it into an int, you need to either get it out as a datarowview ( which is what it is ) and then work out where your number is or, preferably, you need to fix the code that sets the datasource, so that it does in fact contain an int as you were hoping. In general, these 'please help me, something blows up' posts are useless, look how long it took us to get to the heart of the problem. You should learn to use your debugger. You'll get your answer a lot quicker if you post something like 'this line throws an invalid cast exception and the value in the row that I'm trying to cast is a datarowview.' A few seconds in the debugger will tell you that, and as you learn C#, that's the path you'd follow to fix the problem, by first knowing what it is. Christian Graus - Microsoft MVP - C++

                                    M 1 Reply Last reply
                                    0
                                    • C Christian Graus

                                      Your core problem is that you're shoving a datarowview into the value, the problem really is when you set the data source. You need to tell the combo which property to use to set the value for each row. The problem has nothing to do with the event. You have a datarowview in the value, you cannot magically turn it into an int, you need to either get it out as a datarowview ( which is what it is ) and then work out where your number is or, preferably, you need to fix the code that sets the datasource, so that it does in fact contain an int as you were hoping. In general, these 'please help me, something blows up' posts are useless, look how long it took us to get to the heart of the problem. You should learn to use your debugger. You'll get your answer a lot quicker if you post something like 'this line throws an invalid cast exception and the value in the row that I'm trying to cast is a datarowview.' A few seconds in the debugger will tell you that, and as you learn C#, that's the path you'd follow to fix the problem, by first knowing what it is. Christian Graus - Microsoft MVP - C++

                                      M Offline
                                      M Offline
                                      monica2k
                                      wrote on last edited by
                                      #21

                                      Thanks i will try to follow ur instructions . monica

                                      1 Reply Last reply
                                      0
                                      Reply
                                      • Reply as topic
                                      Log in to reply
                                      • Oldest to Newest
                                      • Newest to Oldest
                                      • Most Votes


                                      • Login

                                      • Don't have an account? Register

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • World
                                      • Users
                                      • Groups