Adding C # to list object from checkbox

I am sure that someone asked about this before, but I am not looking for the right conditions or something, because I just can not find what I am looking for.

I have a Packages class:

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 checkListBox in my form bound to a data source. Here is the code section where I get the values ​​in 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 to add a new package to the list when an item is checked with the value item and displaymember from the selected items?

Edit: Well, maybe I'm completely wrong. Instead of telling you what I did, I will tell you what I would like to do.

I represent my user with a checklistbox that has a list of names with checkboxes next to them. They can choose more than one. This leads to the fact that I have one or more identifiers to use in my request and the name (s) to use as a description. I need to pass one or more id / name combinations of my "Package" with my context.

What is the best way to grab the ID / Name combination from user preferences and pass them into my context?

+6
source share
1 answer

First: Extract the 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 selected packages to an instance of your context:

 yourContext.Packages = GetPackages(); 

Third: Fill the CheckedListBox with packages:

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

Finally: After entering the user, get the test packages 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 eg pass it back to the context } 

Sidenote: I would suggest renaming Packages to Package , PackageID to ID and PackageName to Name (as suggested in the code example).

+1
source

Source: https://habr.com/ru/post/950898/


All Articles