Solved

Populate checkboxlist with items from database using linq to sql?

Posted on 2014-12-29
6
150 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 100 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 400 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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 400 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

A publishing tool, a Version Control System, or a Collaboration Platform! These can be some of the defining words for the two very famous web-hosting Git repositories: Bitbucket and Github. Git is widely used amongst the programmers and developers f…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

627 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