wpf datagird button click events

Aug 12, 2010 at 6:57 PM
Edited Aug 12, 2010 at 6:58 PM
I have a datagrid that I am populating from a query then based on the data returned for each row will have a location and up to 7 col. with a pass or fail based on that i use a converter to create a button col for the dates and make it red or green. question is how do i find out with button was clicked by location and Date location Date1 date2 ....... { /// <summary> /// Interaction logic for Window1.xaml /// </summary> public partial class Window1 : Window { public Window1() { InitializeComponent(); } private void Window_Loaded(object sender, RoutedEventArgs e) { DataAccess dbaccess = new DataAccess(); DataSet ds = dbaccess.GetRelData(); string[] colHeaders; List<string> colheadervalue = new List<string>(); int ColCount = ds.Tables[0].Columns.Count; for (int x = 0; x < ColCount; x++) { string colval = ds.Tables[0].Columns[x].ToString(); colheadervalue.Add(colval); string mycol = colheadervalue[x].ToString(); if (x == 0) { DataGridTextColumn rn = new DataGridTextColumn(); rn.Header = "locatonName"; rn.Binding = new Binding("locationNum"); dataGrid1.Columns.Add(rn); } else { //dataGrid1.Columns.Add(new DataGridTextColumn { Header = mycol }); dataGrid1.Columns.Add(MakeColumn(mycol)); } } dataGrid1.ItemsSource = ds.Tables[0].DefaultView; } private DataGridTemplateColumn MakeColumn(string columnName) { FrameworkElementFactory elementFactory = new FrameworkElementFactory(typeof(Button)); Binding binding = new Binding(columnName); binding.Converter = new ButtonColorConverter(); binding.ConverterParameter = columnName; // elementFactory.SetValue(Button.RenderTransformProperty, new RotateTransform(90)); elementFactory.SetValue(Button.BackgroundProperty, binding); DataTemplate cellTemplate = new DataTemplate(); cellTemplate.VisualTree = elementFactory; DataGridTemplateColumn column = new DataGridTemplateColumn(); column.Header = columnName; column.CellTemplate = cellTemplate; column.CellEditingTemplate = cellTemplate; return column; } } }