Solved

Populate checkboxlist with items from database using linq to sql?

Posted on 2014-12-29
6
148 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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 

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

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

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…
New Relic: Our company recently started researching several products to figure out what were the best ways for us to increase our web page speed and to quickly identify performance problems that we may be having. One of the products we evaluated wa…
This video teaches users how to migrate an existing Wordpress website to a new domain.
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…

738 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