Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. <ComboBox Name="ddlCountry" IsEditable="True" Text="Multi Select"
  2. IsTextSearchEnabled="False" StaysOpenOnEdit="True" Width="150" >
  3. <ComboBox.ItemTemplate>
  4. <DataTemplate>
  5. <CheckBox Name="chkCountry" Width="220" Checked="chkCountry_Checked" Unchecked="chkCountry_Unchecked"
  6. Content="{Binding CountryName}" IsChecked="{Binding Check_Status}" CommandParameter="{Binding CountryId}">
  7. </CheckBox>
  8. </DataTemplate>
  9. </ComboBox.ItemTemplate>
  10. </ComboBox>
  11.  
  12. public partial class MainWindow : Window
  13. {
  14. DataTable dtCountry = new DataTable();
  15. Dictionary<int, string> country = new Dictionary<int, string>();
  16. public MainWindow()
  17. {
  18. InitializeComponent();
  19. LoadCountry();
  20. }
  21. private void LoadCountry()
  22. {
  23. try
  24. {
  25. using (SqlConnection con = new SqlConnection(Global.constring))
  26. {
  27. using (SqlCommand cmd = new SqlCommand("select CountryId, CountryName from Country", con))
  28. {
  29. cmd.CommandType = CommandType.Text;
  30. using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
  31. {
  32. sda.Fill(dtCountry);
  33. }
  34. }
  35. }
  36. dtCountry.Columns.Add("Check_Status", typeof(System.Boolean)); // column added to set IsChecked property of the checkbox
  37.  
  38. foreach (DataRow row in dtCountry.Rows)
  39. {
  40. //need to set value to NewColumn column
  41. row["Check_Status"] = false; // or set it to some other value
  42. }
  43. dtCountry.AcceptChanges();
  44.  
  45. ddlCountry.ItemsSource = null;
  46. ddlCountry.Items.Clear();
  47.  
  48. ddlCountry.ItemsSource = dtCountry.DefaultView;
  49. ddlCountry.DisplayMemberPath = dtCountry.Columns["CountryName"].ToString();
  50. ddlCountry.SelectedValuePath = dtCountry.Columns["CountryId"].ToString();
  51.  
  52. ddlCountry.SelectedIndex = 0;
  53. }
  54. catch (Exception ex)
  55. {
  56.  
  57. }
  58. }
  59. private void chkCountry_Checked(object sender, RoutedEventArgs e)
  60. {
  61. country.Add(int.Parse(Convert.ToString(((System.Windows.Controls.Primitives.ButtonBase)sender).CommandParameter)),
  62. Convert.ToString(((System.Windows.Controls.ContentControl)sender).Content));
  63.  
  64. ddlCountry.Text = GetCountry();
  65. }
  66.  
  67. private void chkCountry_Unchecked(object sender, RoutedEventArgs e)
  68. {
  69. country.Remove(int.Parse(Convert.ToString(((System.Windows.Controls.Primitives.ButtonBase)sender).CommandParameter)));
  70. ddlCountry.Text = GetCountry();
  71. }
  72.  
  73. //Method to set selected values as text of the combobox
  74. private string GetCountry()
  75. {
  76. StringBuilder cntry = new StringBuilder();
  77. foreach (var item in cntry)
  78. {
  79. cntry.Append(item.Value).Append(",");
  80. }
  81.  
  82. return Convert.ToString(cntry).Trim(',');
  83. }
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement