Guest User

Untitled

a guest
Mar 23rd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. private void getcategorynames()
  2. {
  3. var categorytypes = (from categories in age.categories
  4. select categories.category_Name).ToList();
  5.  
  6.  
  7. foreach (string item in categorytypes)
  8. {
  9.  
  10. listcategories.Items.Add(item);
  11.  
  12.  
  13. }
  14.  
  15. age.Categories.ToList().ForEach((c) => listcategories.Items.Add(c));
  16.  
  17. Category category = (Category)listcategories.SelectedItem;
  18. // Do something with category.Id
  19.  
  20. private void getcategorynames() {
  21. age.Categories.ToList().ForEach((c) => listcategories.Items.Add(c));
  22. listcategories.SelectedIndexChanged += (sender, e) => {
  23. Category category = (Category)listcategories.SelectedItem;
  24. // Do something with category.Id
  25. };
  26. }
  27.  
  28. public class Cat
  29. {
  30. public int Id { get;set;}
  31. public string Name { get;set;}
  32.  
  33. public override string ToString()
  34. {
  35. return this.Name;
  36. }
  37.  
  38. }
  39.  
  40. private void getcategorynames()
  41. {
  42. var categorytypes = (from categories in age.categories
  43. select categories.category_Name).ToList();
  44.  
  45. listcategories.SelectedIndexChanged += new EventHandler(listcategories_SelectedIndexChanged);
  46.  
  47. foreach (var c in categorytypes.select(p=> new Cat { Id = p.category_Id, Name = p.category_Name}))
  48. {
  49.  
  50. listcategories.Items.Add(c);
  51.  
  52.  
  53. }
  54.  
  55. void listcategories_SelectedIndexChanged(object sender, EventArgs e)
  56. {
  57. Cat selected = (Cat)(sender as ListBox).SelectedItem;
  58. }
  59.  
  60. DataTable table = new DataTable();
  61. public Form1()
  62. {
  63. InitializeComponent();
  64.  
  65. //you fill the table from database, I will show you my example (becuase I dont have dataBase)!
  66. table.Columns.Add("CategoryID", typeof(int));
  67. table.Columns.Add("CategoryName", typeof(string));
  68. table.Rows.Add(1, "name 1");
  69. table.Rows.Add(2, "name 2");
  70. table.Rows.Add(3, "name 3");
  71.  
  72. listBox1.DataSource = new BindingSource(table, null);
  73. listBox1.DisplayMember = "CategoryName";
  74. listBox1.ValueMember = "CategoryID";
  75. }
  76.  
  77. private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
  78. {
  79. if (listBox1.SelectedIndex > -1)
  80. {
  81. DataRowView data = listBox1.SelectedItem as DataRowView;
  82. int id = int.Parse(data["CategoryID"].ToString());
  83. string name = data["CategoryName"].ToString();
  84. }
  85. }
  86.  
  87. class Group
  88. {
  89. private string _name;
  90. private string _id;
  91.  
  92. public Group(string name, string id)
  93. {
  94. _name = name;
  95. _id = id;
  96. }
  97.  
  98. public override string ToString()
  99. { return _name; }
  100.  
  101. public string id
  102. {
  103. get { return _id; }
  104. }
  105.  
  106. public string name
  107. {
  108. get { return _name; }
  109. }
  110.  
  111. }
  112.  
  113. lbAvailableGroups.Items.Add(new Group(ds.Tables[0].Rows[i][1].ToString(), ds.Tables[0].Rows[i][0].ToString()));
  114. }
  115.  
  116. Group p = (Group)lbUserGroups.SelectedItem;
  117. int GroupId = Convert.ToInt32(p.id);
  118. string GroupName = p.name
Add Comment
Please, Sign In to add comment