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. .NET (Core and Framework)
  4. Returning data with a bound listbox

Returning data with a bound listbox

Scheduled Pinned Locked Moved .NET (Core and Framework)
database
6 Posts 3 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.
  • S Offline
    S Offline
    surfluds
    wrote on last edited by
    #1

    I have a form with a listbox bound to a sql database. I use a dataset to fill the listbox. I need to have it multiselect and return the data selected. Trying to test this in a messagebox it returns dataview objects and mot the data selected. Any ideas Mike

    A S 3 Replies Last reply
    0
    • S surfluds

      I have a form with a listbox bound to a sql database. I use a dataset to fill the listbox. I need to have it multiselect and return the data selected. Trying to test this in a messagebox it returns dataview objects and mot the data selected. Any ideas Mike

      A Offline
      A Offline
      Abhinav S
      wrote on last edited by
      #2

      You need to use the SelectedItem**s** property. See here.

      1 Reply Last reply
      0
      • S surfluds

        I have a form with a listbox bound to a sql database. I use a dataset to fill the listbox. I need to have it multiselect and return the data selected. Trying to test this in a messagebox it returns dataview objects and mot the data selected. Any ideas Mike

        S Offline
        S Offline
        surfluds
        wrote on last edited by
        #3

        Thanks for that reply, I see that and have ammended my code as such, I am making headway though. I can now return the corect amount of selected items but it only returns the top item that many times. Any ideas from virewing my code.

        //Method used to add data to newly inserted rows
        private void AddData(System.Data.DataRow row, System.Data.DataRow row2)
        {
        StringBuilder strb = new StringBuilder();
        string str1 = "";

                object missing = System.Type.Missing;
                if ((checkBox1.Checked) || (checkBox2.Checked))
                {
                    MessageBox.Show("There are no rows to insert");
                }
                else
                {
                    //Add data from data row to the table
                    Microsoft.Office.Interop.Word.Selection selection = Globals.ThisDocument.Application.Selection;
                    int count = Globals.ThisDocument.Tables.Count;
                    if (selection.Tables.Count > 0)
                    {
                        Microsoft.Office.Interop.Word.Row newRow = Globals.ThisDocument.Tables\[count\].Rows.Add(ref missing);
        
                        newRow.Range.Font.Bold = 0;
        
                        newRow.Range.ParagraphFormat.Alignment =
                            Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
        
                        newRow.Cells\[2\].Range.ParagraphFormat.Alignment =
                            Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
        
                        newRow.Cells\[1\].Range.Text = row\["HSActionPlanSubHeader"\].ToString();
                        foreach (DataRowView itm in this.itemDescriptionListBox.SelectedItems)
                        {
                            //newRow.Cells\[2\].Range.Text = row2\["ItemDescription"\].ToString();
                            str1 = row2\["ItemDescription"\].ToString();
                            strb.Append(str1);
                        }
                        newRow.Cells\[2\].Range.Text = strb.ToString();
                        
                    }
                    else
                    {
                        MessageBox.Show("Cursor must be within a table.",
                            "Actions Pane", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
        
            }
        
        L 1 Reply Last reply
        0
        • S surfluds

          Thanks for that reply, I see that and have ammended my code as such, I am making headway though. I can now return the corect amount of selected items but it only returns the top item that many times. Any ideas from virewing my code.

          //Method used to add data to newly inserted rows
          private void AddData(System.Data.DataRow row, System.Data.DataRow row2)
          {
          StringBuilder strb = new StringBuilder();
          string str1 = "";

                  object missing = System.Type.Missing;
                  if ((checkBox1.Checked) || (checkBox2.Checked))
                  {
                      MessageBox.Show("There are no rows to insert");
                  }
                  else
                  {
                      //Add data from data row to the table
                      Microsoft.Office.Interop.Word.Selection selection = Globals.ThisDocument.Application.Selection;
                      int count = Globals.ThisDocument.Tables.Count;
                      if (selection.Tables.Count > 0)
                      {
                          Microsoft.Office.Interop.Word.Row newRow = Globals.ThisDocument.Tables\[count\].Rows.Add(ref missing);
          
                          newRow.Range.Font.Bold = 0;
          
                          newRow.Range.ParagraphFormat.Alignment =
                              Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
          
                          newRow.Cells\[2\].Range.ParagraphFormat.Alignment =
                              Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
          
                          newRow.Cells\[1\].Range.Text = row\["HSActionPlanSubHeader"\].ToString();
                          foreach (DataRowView itm in this.itemDescriptionListBox.SelectedItems)
                          {
                              //newRow.Cells\[2\].Range.Text = row2\["ItemDescription"\].ToString();
                              str1 = row2\["ItemDescription"\].ToString();
                              strb.Append(str1);
                          }
                          newRow.Cells\[2\].Range.Text = strb.ToString();
                          
                      }
                      else
                      {
                          MessageBox.Show("Cursor must be within a table.",
                              "Actions Pane", MessageBoxButtons.OK, MessageBoxIcon.Error);
                      }
                  }
          
              }
          
          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          I did not analyze your code, however this struck me:

          surfluds wrote:

          foreach (DataRowView itm in this.itemDescriptionListBox.SelectedItems) { //newRow.Cells[2].Range.Text = row2["ItemDescription"].ToString(); str1 = row2["ItemDescription"].ToString(); strb.Append(str1); }

          itm is enumerating a collection, however you don't use it anywhere??? :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          I only read formatted code with indentation, so please use PRE tags for code snippets.


          I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


          S 1 Reply Last reply
          0
          • S surfluds

            I have a form with a listbox bound to a sql database. I use a dataset to fill the listbox. I need to have it multiselect and return the data selected. Trying to test this in a messagebox it returns dataview objects and mot the data selected. Any ideas Mike

            S Offline
            S Offline
            surfluds
            wrote on last edited by
            #5

            Ok thanks for the reply, got it sorted, stupidly didnot use the correct variable in the foreach loop to reference the dataveiwrow. Couldn't see for looking.

            1 Reply Last reply
            0
            • L Luc Pattyn

              I did not analyze your code, however this struck me:

              surfluds wrote:

              foreach (DataRowView itm in this.itemDescriptionListBox.SelectedItems) { //newRow.Cells[2].Range.Text = row2["ItemDescription"].ToString(); str1 = row2["ItemDescription"].ToString(); strb.Append(str1); }

              itm is enumerating a collection, however you don't use it anywhere??? :)

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


              I only read formatted code with indentation, so please use PRE tags for code snippets.


              I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


              S Offline
              S Offline
              surfluds
              wrote on last edited by
              #6

              Yes thanks Luc, seen that finally, all works as it should. Tired. :sigh:

              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