Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# adding to list object from checkbox

Tags:

c#

list

winforms

I am sure that someone has asked this before, but I am not searching for the right terms or something because I just can't seem to find what I am looking for.

I have a class Packages:

class Packages
{
    string PackageID {get; set;}
    string PackageName { get; set; }
}

I have a list of packages in my context.

public class TransferJobContext
{
    public string ImportFile;
    public string WorkSheetName;
    public DataSet TransferData;
    public List<Packages> Packages = new List<Packages>();
}

I have a checkedListBox on my form that is bound to a datasource. Here is the section of code where I get the values into the checkedListBox

using (var connection = my_DB.GetConnection())
{
    try
    {
        connection.Open();
        SqlDataReader rdr = null;
        dt = new DataTable();
        string CommandText = "SELECT ID, Name FROM TABLENAME WHERE UPPER(Import_File_Source) LIKE '%abc%' and STATUS = 1";

        SqlCommand cmd = new System.Data.SqlClient.SqlCommand(CommandText, connection);

        rdr = cmd.ExecuteReader();
        dt.Load(rdr);
        cbPackages.DataSource = dt;
        cbPackages.ValueMember = "ID";
        cbPackages.DisplayMember = "Name";

    }
    catch (Exception E)
    {
        MessageBox.Show(E.Message.ToString());
    }
    connection.Close();
}

How do I add a new Package to the list when an item is checked using the value member and displaymember of the selected items?

Edit: Ok maybe I am going about this completely wrong. Instead of telling you what I have done, I'll tell you what I would like to happen.

I am presenting my user with a checkedlistbox that has a list of names with check boxes next to them. They can select more than one. This results in me having one or more ID(s) to use in my query and name(s) to use as a description. I need to pass around with my context the one or many id/name combinations of my "Package".

What is the best what to capture the ID/Name combination of the selections the user made and pass them into my context?

like image 461
Leslie Avatar asked Aug 02 '13 11:08

Leslie


1 Answers

First: Fetch existing Packages from your database:

public function GetPackages() as List<Package>
{
 using (var connection = my_DB.GetConnection())
    {
        try
        {
            connection.Open();
            SqlDataReader rdr = null;
            dt = new DataTable();
            string CommandText = "SELECT ID, Name FROM TABLENAME WHERE UPPER(Import_File_Source) LIKE '%abc%' and STATUS = 1";

            SqlCommand cmd = new System.Data.SqlClient.SqlCommand(CommandText, connection);

             var packages = new List<Package>();
             using(var reader = cmd.ExecuteReader())
             {
             do while(reader.Read())
               {
               packages.Add(new Package({ID = reader.GetString(0), Name = reader.GetString(1)})
               }
             }

            cbPackages.DataSource = packages;
            cbPackages.ValueMember = "ID";
            cbPackages.DisplayMember = "Name";
            return packages;
        }
        catch (Exception E)
        {
            MessageBox.Show(E.Message.ToString());
            return new List<Package>();
        }
        connection.Close();
    }

}

Second: Assign the fetched packages to the instance of your context:

yourContext.Packages = GetPackages();

Third: Fill the CheckedListBox with your packages:

cbPackages.DataSource = yourContext.Packages;
cbPackages.ValueMember = "ID";
cbPackages.DisplayMember = "Name";

Finally: After user input, get the checkedpackages and do whatever you want with them:

foreach (Package  item in checkedListBox1.CheckedItems)
{
     MessageBox.Show(Package.ID);
     MessageBox.Show(Package.Name);
     //do whatever you want here e.g. pass it back to the context
}

Sidenote: I would suggest you to rename Packages to Package, PackageID to ID and PackageName to Name (As already proposed in the example code).

like image 155
Fabian Bigler Avatar answered Sep 23 '22 18:09

Fabian Bigler