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. Reading data from a dataset with a checkedlistbox

Reading data from a dataset with a checkedlistbox

Scheduled Pinned Locked Moved C#
questiondatabaseregexhelptutorial
7 Posts 2 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.
  • F Offline
    F Offline
    falles01
    wrote on last edited by
    #1

    Hi I'm posting a new question in case Malcolm doesn't read my reply in time. Basically what is wrong with this code? I want to automatically check the checklistbox items depending on if the items match the employees TechnicalSkillsID in the database table. Malcolm gave me a web form example which was a great response except I forgot to tell him I was using a winform. can you help at all? int i; for (i = 0; i <= (techSkillsCheckListBox2.Items.Count - 1); i++) { // SqlDataReader MyDataReader = adoCmd.ExecuteReader(CommandBehavior.CloseConnection); while (adoDR.Read()) { sql = "Select em.TechnicalSkillsID,t.ProgLanguagesDatabase from EmpSkills em,TechnicalSkills t where EmployeeID = '" + FirstnameText.Text + "' and em.TechnicalSkillsID = t.TechnicalSkillsID"; //populate a datatable with the result set DataRow[] rows = dataSet1.TechnicalSkills.Select(); foreach (DataRow row in rows) { string techID = row["TechnicalSkillsID"].ToString(); techSkillsCheckListBox2.SetItemChecked(i, true); } } } Sianny aka Sharny

    C 1 Reply Last reply
    0
    • F falles01

      Hi I'm posting a new question in case Malcolm doesn't read my reply in time. Basically what is wrong with this code? I want to automatically check the checklistbox items depending on if the items match the employees TechnicalSkillsID in the database table. Malcolm gave me a web form example which was a great response except I forgot to tell him I was using a winform. can you help at all? int i; for (i = 0; i <= (techSkillsCheckListBox2.Items.Count - 1); i++) { // SqlDataReader MyDataReader = adoCmd.ExecuteReader(CommandBehavior.CloseConnection); while (adoDR.Read()) { sql = "Select em.TechnicalSkillsID,t.ProgLanguagesDatabase from EmpSkills em,TechnicalSkills t where EmployeeID = '" + FirstnameText.Text + "' and em.TechnicalSkillsID = t.TechnicalSkillsID"; //populate a datatable with the result set DataRow[] rows = dataSet1.TechnicalSkills.Select(); foreach (DataRow row in rows) { string techID = row["TechnicalSkillsID"].ToString(); techSkillsCheckListBox2.SetItemChecked(i, true); } } } Sianny aka Sharny

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

      falles01 wrote:

      for (i = 0; i <= (techSkillsCheckListBox2.Items.Count - 1); i++)

      Why not i < techSkillsCheckListBox2.Items.Count ? Or even foreach (CheckBox box in techSkillsCheckListBox2.Items) ? This code is really broken. What you want to do, is fill a list with the items that are selected, then go through each check box and do something like checkbox.Checked = (myListOfItems.Contains(theKEyForThisCheckbox)); Of course, I just invented some variables, assume the Tag of the checkbox contains the key value, so you can look it up. The code you're presenting does a nested loop, and just sets each box to be checked, over and over again.

      Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

      F 3 Replies Last reply
      0
      • C Christian Graus

        falles01 wrote:

        for (i = 0; i <= (techSkillsCheckListBox2.Items.Count - 1); i++)

        Why not i < techSkillsCheckListBox2.Items.Count ? Or even foreach (CheckBox box in techSkillsCheckListBox2.Items) ? This code is really broken. What you want to do, is fill a list with the items that are selected, then go through each check box and do something like checkbox.Checked = (myListOfItems.Contains(theKEyForThisCheckbox)); Of course, I just invented some variables, assume the Tag of the checkbox contains the key value, so you can look it up. The code you're presenting does a nested loop, and just sets each box to be checked, over and over again.

        Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

        F Offline
        F Offline
        falles01
        wrote on last edited by
        #3

        Thanks but the part you are talking about that is wrong, is actually the part that is correct and gives the correct value. I got that off msdn. Yes I want to fill a list with items but as i said, I can't use listitems as I'm using a winform so I would like to know how I do that exactly. How do you check the items against the ID's against an empID in the database? Sianny aka Sharny

        C 1 Reply Last reply
        0
        • F falles01

          Thanks but the part you are talking about that is wrong, is actually the part that is correct and gives the correct value. I got that off msdn. Yes I want to fill a list with items but as i said, I can't use listitems as I'm using a winform so I would like to know how I do that exactly. How do you check the items against the ID's against an empID in the database? Sianny aka Sharny

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

          If you have a list of items and want to fill the control with them, you should be able to set the control's DataSource property with the list, and it should work. That code is from MSDN ? It's REALLY messy, especially with the nested loop. Do you have a link ? I believe you, I'm just interested.

          Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

          F 1 Reply Last reply
          0
          • C Christian Graus

            If you have a list of items and want to fill the control with them, you should be able to set the control's DataSource property with the list, and it should work. That code is from MSDN ? It's REALLY messy, especially with the nested loop. Do you have a link ? I believe you, I'm just interested.

            Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

            F Offline
            F Offline
            falles01
            wrote on last edited by
            #5

            I'm not sure what you mean but i think I have filled the list already as you said as the items are there. I just need the ticked items to save as correct ID's in the database against a given employee. I would've used bits and pieces of msdn stuff so that might be why its messy. I didn't copy a whole paragraph of code, just a line here and there. public override void TechnicalSkillsList() { DataSet1TableAdapters.TechnicalSkillsTableAdapter technicalskillsTableAdapter = new ResourceSearchTool.DataSet1TableAdapters.TechnicalSkillsTableAdapter(); DataSet1.TechnicalSkillsDataTable techSkillsDT = technicalskillsTableAdapter.GetData(); foreach (DataSet1.TechnicalSkillsRow techSkillsRow in techSkillsDT.Rows) this.techSkillsCheckListBox2.Items.Add(techSkillsRow.ProgLanguagesDatabase); }

            1 Reply Last reply
            0
            • C Christian Graus

              falles01 wrote:

              for (i = 0; i <= (techSkillsCheckListBox2.Items.Count - 1); i++)

              Why not i < techSkillsCheckListBox2.Items.Count ? Or even foreach (CheckBox box in techSkillsCheckListBox2.Items) ? This code is really broken. What you want to do, is fill a list with the items that are selected, then go through each check box and do something like checkbox.Checked = (myListOfItems.Contains(theKEyForThisCheckbox)); Of course, I just invented some variables, assume the Tag of the checkbox contains the key value, so you can look it up. The code you're presenting does a nested loop, and just sets each box to be checked, over and over again.

              Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

              F Offline
              F Offline
              falles01
              wrote on last edited by
              #6

              and how do you fill a list with the checked items by the way?

              1 Reply Last reply
              0
              • C Christian Graus

                falles01 wrote:

                for (i = 0; i <= (techSkillsCheckListBox2.Items.Count - 1); i++)

                Why not i < techSkillsCheckListBox2.Items.Count ? Or even foreach (CheckBox box in techSkillsCheckListBox2.Items) ? This code is really broken. What you want to do, is fill a list with the items that are selected, then go through each check box and do something like checkbox.Checked = (myListOfItems.Contains(theKEyForThisCheckbox)); Of course, I just invented some variables, assume the Tag of the checkbox contains the key value, so you can look it up. The code you're presenting does a nested loop, and just sets each box to be checked, over and over again.

                Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                F Offline
                F Offline
                falles01
                wrote on last edited by
                #7

                and what sort of list? I have a checkedlist box which is filled with data from the dataset.

                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