Question

I need help with Binding my listbox to my entity in c#. w/ code if possible...

I need help with Binding my listbox to my entity in c#.

w/ code if possible

I have

using(var db = new database_name)

{

var r = sql statement;

I need help with whats next. My entity has diff rows and i need to display them in my listbox.Thank you

0 0
Add a comment Improve this question Transcribed image text
Answer #1

you will use Listbox.Items.Add property to display items in the list box.

Please see the example below. In case of any query, do comment.

Designer Code:

namespace ListBoxInCSharp

{

    partial class Form1

    {

        /// <summary>

        /// Required designer variable.

        /// </summary>

        private System.ComponentModel.IContainer components = null;

        /// <summary>

        /// Clean up any resources being used.

        /// </summary>

        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>

        protected override void Dispose(bool disposing)

        {

            if (disposing && (components != null))

            {

                components.Dispose();

            }

            base.Dispose(disposing);

        }

        #region Windows Form Designer generated code

        /// <summary>

        /// Required method for Designer support - do not modify

        /// the contents of this method with the code editor.

        /// </summary>

        private void InitializeComponent()

        {

            this.lstBoxDisplay = new System.Windows.Forms.ListBox();

            this.btnFill = new System.Windows.Forms.Button();

            this.SuspendLayout();

            //

            // lstBoxDisplay

            //

            this.lstBoxDisplay.FormattingEnabled = true;

            this.lstBoxDisplay.ItemHeight = 20;

            this.lstBoxDisplay.Location = new System.Drawing.Point(172, 58);

            this.lstBoxDisplay.Name = "lstBoxDisplay";

            this.lstBoxDisplay.Size = new System.Drawing.Size(324, 164);

            this.lstBoxDisplay.TabIndex = 0;

            //

            // btnFill

            //

            this.btnFill.Location = new System.Drawing.Point(275, 299);

            this.btnFill.Name = "btnFill";

            this.btnFill.Size = new System.Drawing.Size(116, 42);

            this.btnFill.TabIndex = 1;

            this.btnFill.Text = "FillListBox";

            this.btnFill.UseVisualStyleBackColor = true;

            this.btnFill.Click += new System.EventHandler(this.btnFill_Click);

            //

            // Form1

            //

            this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);

            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

            this.ClientSize = new System.Drawing.Size(800, 450);

            this.Controls.Add(this.btnFill);

            this.Controls.Add(this.lstBoxDisplay);

            this.Name = "Form1";

            this.Text = "ListBox Example";

            this.ResumeLayout(false);

        }

       #endregion

        private System.Windows.Forms.ListBox lstBoxDisplay;

        private System.Windows.Forms.Button btnFill;

    }

}

Code behind:

using System;

using System.Collections.Generic;

using System.Windows.Forms;

namespace ListBoxInCSharp

{

    public partial class Form1 : Form

    {

        //below list is declared for example purpose

        List<string> rows ;

        public Form1()

        {

            InitializeComponent();

        }

        private void btnFill_Click(object sender, EventArgs e)

        {

            //first clear the list before filling it

            lstBoxDisplay.Items.Clear();

            //rows is a list of string, prepared as below to just show the usage of list box, you will use your list of rows returned from Database

            rows = new List<string>(){ "Test", "Chegg" ,"ListBox", "Example"};

            //now iterate through your rows received from Database

            foreach (var row in rows)

            {

                //add it to List box Items collection, which shows the details on UI

                lstBoxDisplay.Items.Add(row);

                //if you want any specific properties of your row, you use as below, and replace the column name of you need to display

                //lstBoxDisplay.Items.Add(row.<column>);

            }

        }

    }

}

==============================code screen shot======================

Output:

Please rate your answer. Thanks

Add a comment
Know the answer?
Add Answer to:
I need help with Binding my listbox to my entity in c#. w/ code if possible...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help with my C coding homework. If possible can you please comment to help...

    I need help with my C coding homework. If possible can you please comment to help me understand whats going on in the code. Thanks in advance. Create a C program (3 points): o At the top of the file include the following comments: Your name The purpose (core concept found below) of the program The date created o Create two int variables: Initialize each to different values. o Create three pointers: Point the first two pointers to the first...

  • I need help regarding my Visual Basic code ISC/ITE 285 Homework Due: Monday, January 28 by...

    I need help regarding my Visual Basic code ISC/ITE 285 Homework Due: Monday, January 28 by 11:55 pm Create a text file named "Grades.txt" which will contain a list of numeric grades. Save the file in the projects Bin/Debug folder. A sample of the file contents is shown below: 80 96 95 86 54 97 Create a string array that will be populated from the file "Grades.txt" when the array is created (Class-level array). Also create a numeric array that...

  • I need help with this c++ code. This is my first computer science course. I have...

    I need help with this c++ code. This is my first computer science course. I have not learned arrays but I am learning vectors. I am also required to write this code without do loops or any other form of advanced coding technique. Please help me figure out this code using the basic coding techniques that you would find in an introduction to cs. Thank you for your help, I don't even know where to start :)

  • JavaScript expert, I need your help : My program doesn't work. I have the following program,...

    JavaScript expert, I need your help : My program doesn't work. I have the following program, it is simple web page: Create an object called person with name = John, age = 50. Then, access the object to display "John is 50 years old”. I have written some code here. Hint: Create an object with the var keyword, followed by a name and an "=" sign. Put the properties and values inside the {}; signs Note: Use getElementById() and innerHTML...

  • *has to with R program, I need the code for R script* I already have questions...

    *has to with R program, I need the code for R script* I already have questions 1 through solved and posted a picture of them. I need help with questions 4-11 please!!! thank u 4 plots a histogram showing the distribution of your X. 5 generates a data series Y with a length equal to 'howmany' 6 plots the distribution Y 7 assigns the series X to a new variable Z. 8 calculates the variance of the series Z using...

  • Hello, I have my piece of C programming code and I have a pointer initialized to...

    Hello, I have my piece of C programming code and I have a pointer initialized to point to my array and I need help to display the contents of my array using a while loop or do-while loop. The stop condition should be when the pointer reaches '\0'. The code is below: #include <stdio.h> int main () {    char array[80]; printf("Enter a string: "); scanf("%[^\n]", &array); printf("%s\n", array); char * p;    p = array;         }

  • I need some help with a html code, The code for the CRUD terms on the...

    I need some help with a html code, The code for the CRUD terms on the left side of the section box. I need the lines under create, read, update, delete to be the same color as the section box. Thank you! How do I make the CRUD i.e to "create" "new" on whats inside the section box. create read update delete

  • Hello I need help turning my code into a GUI applicaiton please. What I have for...

    Hello I need help turning my code into a GUI applicaiton please. What I have for code and the requirements are below. Please please help, I will be so greateful. This is my last assignment of my school and I'm just so burnt out mentally that I don't know where to start and my teacher is always to busy to help. Thank you so so so much! -----Code------ private void CalculateSC(ActionEvent event) { if(amount < 10){ chargeAmount = 1; }else...

  • Hi I was hoping to get some help with my code for this problem. I don't...

    Hi I was hoping to get some help with my code for this problem. I don't know how to correctly read a txt file and the difference between using string and cstring. My code is at the bottom please help thanks. 1. Create the following “data.txt” file. Each record consists of customer's name and their balance: Maria Brown 2100.90 Jeffrey Jackson 200.20 Bernard Smith 223.10 Matthew Davenport 1630.20 Kimberly Macias 19100.90 Amber Daniels 2400.40 The records in the file will...

  • I need help solving this basic C++ question. I need help fixing my code.   Question: Design...

    I need help solving this basic C++ question. I need help fixing my code.   Question: Design a class called a box that represents a box. Box classes have variables such as the length of the box, width, and height. 1. Member variables shall be dedicated members. 2. Define the creator of the Box Class. The creator may receive all of the data and may not receive any. 3. Add accessor and creator 4. Add an empty function, which indicates whether...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT