Hello,
I have a grid that is completly dynamic. The user can add as many rows as they want to this grid. What I need to do is somehow get the values of each control in each row so that I can create either a linq query or xml to send down to the server.
Here is the code for adding a row when a button is clicked:
ButtonClick:
1 private void AddNewFilter_Click(object sender, RoutedEventArgs e) 2 { 3 Button addButton = sender as Button; 4 int row = (int)addButton.GetValue(Grid.RowProperty); 5 FilterGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(30) }); 6 AddRow(row); 7 addButton.SetValue(Grid.RowProperty, row + 1); 8 }
2 { 3 Button addButton = sender as Button; 4 int row = (int)addButton.GetValue(Grid.RowProperty); 5 FilterGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(30) }); 6 AddRow(row); 7 addButton.SetValue(Grid.RowProperty, row + 1); 8 }
AddRow Method:
1 public void AddRow(int row) 2 { 3 andOrComboBox = new ComboBox() 4 { 5 ItemsSource = new List<AndOrEnum>() { AndOrEnum.SelectOne, AndOrEnum.And, AndOrEnum.Or}, 6 SelectedIndex = 0 7 }; 8 andOrComboBox.SetValue(Grid.RowProperty, row); 9 andOrComboBox.SetValue(Grid.ColumnProperty, 0); 10 11 12 propertyComboBox = new ComboBox(); 13 List<PropertyValue> properties = new List<PropertyValue>(); 14 properties.Add(new PropertyValue() { Name = "SelectOne", TypeOfProperty = null }); 15 foreach (PropertyInfo info in propertyInfo) 16 { 17 18 object[] attributes = info.GetCustomAttributes(typeof(FilterAttribute),true); 19 if (attributes.Length > 0) 20 { 21 FilterAttribute f = (FilterAttribute)attributes[0]; 22 if (f.IsFilterable) 23 { 24 if (f.FilterName != null) 25 { 26 properties.Add(new PropertyValue() { Name = f.FilterName, TypeOfProperty = info.PropertyType}); 27 } 28 else 29 { 30 properties.Add(new PropertyValue() { Name = info.Name, TypeOfProperty = info.PropertyType }); 31 } 32 } 33 } 34 } 35 propertyComboBox.ItemsSource = properties; 36 propertyComboBox.ItemTemplate = propertyValueTemplate; 37 propertyComboBox.SelectedIndex = 0; 38 propertyComboBox.SetValue(Grid.RowProperty, row); 39 propertyComboBox.SetValue(Grid.ColumnProperty, 1); 40 propertyComboBox.SelectionChanged += new SelectionChangedEventHandler(propertyComboBox_SelectionChanged); 41 42 operatorComboBox = new ComboBox() 43 { 44 ItemsSource = new List<OperatorsEnum>() 45 { 46 OperatorsEnum.SelectOne, 47 OperatorsEnum.EqualTo, 48 OperatorsEnum.GreaterThan, 49 OperatorsEnum.GreaterThanOrEqualTo, 50 OperatorsEnum.LessThan, 51 OperatorsEnum.LessThanOrEqualTo, 52 OperatorsEnum.NotEqualTo 53 } , 54 SelectedIndex = 0 55 }; 56 operatorComboBox.SetValue(Grid.RowProperty, row); 57 operatorComboBox.SetValue(Grid.ColumnProperty, 2); 58 59 Button deleteButton = new Button() {Content = "Delete" }; 60 deleteButton.Click += new RoutedEventHandler(deleteButton_Click); 61 deleteButton.SetValue(Grid.RowProperty, row); 62 deleteButton.SetValue(Grid.ColumnProperty, 4); 63 64 FilterGrid.Children.Add(propertyComboBox); 65 FilterGrid.Children.Add(andOrComboBox); 66 FilterGrid.Children.Add(operatorComboBox); 67 FilterGrid.Children.Add(deleteButton); 68 69 } 70
2 { 3 andOrComboBox = new ComboBox() 4 { 5 ItemsSource = new List<AndOrEnum>() { AndOrEnum.SelectOne, AndOrEnum.And, AndOrEnum.Or}, 6 SelectedIndex = 0 7 }; 8 andOrComboBox.SetValue(Grid.RowProperty, row); 9 andOrComboBox.SetValue(Grid.ColumnProperty, 0); 10 11 12 propertyComboBox = new ComboBox(); 13 List<PropertyValue> properties = new List<PropertyValue>(); 14 properties.Add(new PropertyValue() { Name = "SelectOne", TypeOfProperty = null }); 15 foreach (PropertyInfo info in propertyInfo) 16 { 17 18 object[] attributes = info.GetCustomAttributes(typeof(FilterAttribute),true); 19 if (attributes.Length > 0) 20 { 21 FilterAttribute f = (FilterAttribute)attributes[0]; 22 if (f.IsFilterable) 23 { 24 if (f.FilterName != null) 25 { 26 properties.Add(new PropertyValue() { Name = f.FilterName, TypeOfProperty = info.PropertyType}); 27 } 28 else 29 { 30 properties.Add(new PropertyValue() { Name = info.Name, TypeOfProperty = info.PropertyType }); 31 } 32 } 33 } 34 } 35 propertyComboBox.ItemsSource = properties; 36 propertyComboBox.ItemTemplate = propertyValueTemplate; 37 propertyComboBox.SelectedIndex = 0; 38 propertyComboBox.SetValue(Grid.RowProperty, row); 39 propertyComboBox.SetValue(Grid.ColumnProperty, 1); 40 propertyComboBox.SelectionChanged += new SelectionChangedEventHandler(propertyComboBox_SelectionChanged); 41 42 operatorComboBox = new ComboBox() 43 { 44 ItemsSource = new List<OperatorsEnum>() 45 { 46 OperatorsEnum.SelectOne, 47 OperatorsEnum.EqualTo, 48 OperatorsEnum.GreaterThan, 49 OperatorsEnum.GreaterThanOrEqualTo, 50 OperatorsEnum.LessThan, 51 OperatorsEnum.LessThanOrEqualTo, 52 OperatorsEnum.NotEqualTo 53 } , 54 SelectedIndex = 0 55 }; 56 operatorComboBox.SetValue(Grid.RowProperty, row); 57 operatorComboBox.SetValue(Grid.ColumnProperty, 2); 58 59 Button deleteButton = new Button() {Content = "Delete" }; 60 deleteButton.Click += new RoutedEventHandler(deleteButton_Click); 61 deleteButton.SetValue(Grid.RowProperty, row); 62 deleteButton.SetValue(Grid.ColumnProperty, 4); 63 64 FilterGrid.Children.Add(propertyComboBox); 65 FilterGrid.Children.Add(andOrComboBox); 66 FilterGrid.Children.Add(operatorComboBox); 67 FilterGrid.Children.Add(deleteButton); 68 69 } 70
Like I said I need to figure out a way to access the controls for each row. This will be used so users can create whatever type of filter they want for specific datagrids. So each row is a filter that can be AND'd or OR'd to each other which is why I need to do it for each row. Oh also the first row in the grid is defined in XAML because it is used as a HeaderRow since Grids don't have HeaderRows, so the first row needs to be ignored. Any help is greatly appreciated. Best Regards, Brett