Solved

Populate checkboxlist with items from database using linq to sql?

Posted on 2014-12-29
6
141 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
  • 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 32

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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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 32

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Using Quotation Marks in PHP This question (http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28217211.html) seems to come up a lot for developers who are new to PHP.  And it got me thinking, "How can we explain the rule…
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!
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.
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…

706 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now