Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Populate checkboxlist with items from database using linq to sql?

Posted on 2014-12-29
6
Medium Priority
?
153 Views
Last Modified: 2016-02-16
Hi:
My table : tblapplicationType
Fields are: Id & applicationType.
Can I Populate checkboxlist with items from tblapplicationType
My context :
dbDataContext db = new dbDataContext();
Would you please, help me writing the suitable code doing so.
Thanks in advance.
0
Comment
Question by:Mohammad Alsolaiman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 400 total points
ID: 40523652
You should be able to use the DataSource property to set the items for the CheckBoxList, with any source that is IEnumerable.
0
 

Author Comment

by:Mohammad Alsolaiman
ID: 40523685
IEnumerable! sorry
I'm newbie to c# . I'll watch some videos on that, but acn any one help with the code step by step. please
thanks Bob Learned .
0
 
LVL 34

Assisted Solution

by:it_saige
it_saige earned 1600 total points
ID: 40523962
Understand that this is a very rudimentry and simple example based on the provided information:
using System;
using System.Windows.Forms;

namespace EE_Q28587937
{
	public partial class Form1 : Form
	{
		private BindingSource source = null;
		private dbDataContext context = null;
		public Form1()
		{
			InitializeComponent();
			if (context == null)
				context = new dbDataContext();
			if (source == null)
				source = new BindingSource();
		}

		private void OnLoad(object sender, EventArgs e)
		{
			source.DataSource = context.GetTable(typeof(tblApplicationType));
			checkedListBox1.DataSource = source;
			checkedListBox1.DisplayMember = "ApplicationType";
			checkedListBox1.ValueMember = "ID";
		}

		private void OnItemCheck(object sender, ItemCheckEventArgs e)
		{
			MessageBox.Show(string.Format("You have {0} {1}", e.NewValue == CheckState.Checked ? "checked" : "unchecked", (checkedListBox1.Items[e.Index] as tblApplicationType).ApplicationType));
		}
	}
}

Open in new window

Produces the following output -Capture.JPGAnd when you check an item -Capture.JPGAnd then uncheck the item -Capture.JPG-saige-
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:Mohammad Alsolaiman
ID: 40526721
i try it , but i get this error.
The type or namespace name 'tblapplicationTypes' could not be found (are you missing a using directive or an assembly reference?)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        dbDataContextDataContext dbDataContext = new dbDataContextDataContext();
        private BindingSource source = null;
        private dbDataContextDataContext context = null;
        public Form1()
        {
            InitializeComponent();
            if (context == null)
                context = new dbDataContextDataContext();
            if (source == null)
                source = new BindingSource();
        }

        private void OnLoad(object sender, EventArgs e)
        {
            source.DataSource = context.GetTable(typeof(tblapplicationTypes));
            checkedListBox1.DataSource = source;
            checkedListBox1.DisplayMember = "ApplicationType";
            checkedListBox1.ValueMember = "ID";
        }

        private void OnItemCheck(object sender, ItemCheckEventArgs e)
        {
            MessageBox.Show(string.Format("You have {0} {1}", e.NewValue == CheckState.Checked ? "checked" : "unchecked", (checkedListBox1.Items[e.Index] as tblApplicationType).ApplicationType));
        }
    }
}

Open in new window

please help.
0
 
LVL 34

Accepted Solution

by:
it_saige earned 1600 total points
ID: 40526865
As I stated, this example was created based on the provided information.  So the types and definitions that I used in mine will be different from yours.  What you need to do is open the designer file for your dbDataContextDataContext and look at the table definition.  There are a few ways to do this:

1. In solution explorer, locate the the object that defines your data context and open the designer.

My data context object is DataClasses.dbml.I can right-click on the DBML and choose 'Open' or 'View Designer'.In the designer, locate the table that represents the table for your application types.  Here is mine.  The highlighted portion is what get's placed in the typeof method.-Or-

2. In solution explorer, locate the the object that defines your data context and open the Designer file.

My data context object is DataClasses.dbml.I right-click on the DataClasses.designer.cs file and choose 'Open' or 'View Code'.Selecting the drop down that provides the class definitions, I can find my table definition here.  Once again, this is what gets put into the typeof method.The DataContext class also provides a method that returns the table so that we would not have to worry about the line:
source.DataSource = context.GetTable(typeof(tblApplicationType));

Open in new window

Instead we could call this shortcut method on the context itself:Capture.JPGCapture.JPGSo we could change our code to -
source.DataSource = context.tblApplicationTypes;

Open in new window

For completeness here is the full *edited* code for Form1.cs -
using System;
using System.Windows.Forms;

namespace EE_Q28587937
{
	public partial class Form1 : Form
	{
		private BindingSource source = null;
		private dbDataContext context = null;
		public Form1()
		{
			InitializeComponent();
			if (context == null)
				context = new dbDataContext();
			if (source == null)
				source = new BindingSource();
		}

		private void OnLoad(object sender, EventArgs e)
		{
			source.DataSource = context.tblApplicationTypes;
			checkedListBox1.DataSource = source;
			checkedListBox1.DisplayMember = "ApplicationType";
			checkedListBox1.ValueMember = "ID";
		}

		private void OnItemCheck(object sender, ItemCheckEventArgs e)
		{
			MessageBox.Show(string.Format("You are {0} {1}", e.NewValue == CheckState.Checked ? "checking" : "unchecking", (checkedListBox1.Items[e.Index] as tblApplicationType).ApplicationType));
		}
	}
}

Open in new window

-saige-
0
 

Author Closing Comment

by:Mohammad Alsolaiman
ID: 40551208
thanks very much to all of you
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Thoughout my experience working on eCommerce web applications I have seen applications succumbing to increased user demand and throughput. With increased loads the response times started to spike, which leads to user frustration and lost sales. I ha…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question