Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
This is my dynamic Data Grid code... On run time depending on data I will get grid views greater than 3.... So if u see my code... There I am providing text boxes in the grid view... so when the user enters the data... I want to change the values of the column of that row... So any one please suggest me that how to change the value of a column of a dynamic Data Grid... Pls don't give solution that to change the datatable and bind to that grid... I have issues in that way... so please suggest me appropriate solution... I am having On change event in text box...

C#
private void CreateDynamicGridView(DataSet dsBillingDetails)
       {

          // Create a GridView
           int rowPosition = 0;
           for (int index = 1; index < dsBillingDetails.Tables.Count; index++)
           {

                   DataGrid grdCharges = new DataGrid();
                   grdCharges.ItemsSource = dsBillingDetails.Tables[index].DefaultView;
                   grdCharges.AutoGenerateColumns = false;

                   Style grdHeaderStyle = this.FindResource("grdHeaderStyle") as Style;

                   <big>//Adding Button Dynamically</big>
                   DataGridTemplateColumn dgtcDel = new DataGridTemplateColumn();
                   FrameworkElementFactory fefDelButton = new FrameworkElementFactory(typeof(Button));
                   BitmapImage biDeleteIcon = new BitmapImage();

                   fefDelButton.SetBinding(Button.TagProperty, new Binding("LineItemID"));
                   fefDelButton.AddHandler(FrameworkElement.MouseLeftButtonDownEvent, new MouseButtonEventHandler(btnDelete_Click), true);
                   DataTemplate buttonTemplate = new DataTemplate();

                   dgtcDel.Header = "D";
                   dgtcDel.HeaderStyle = grdHeaderStyle;
                   buttonTemplate.VisualTree = fefDelButton;
                   dgtcDel.CellTemplate = buttonTemplate;
                   biDeleteIcon.BeginInit();
                   biDeleteIcon.UriSource = new Uri(@"..\Images\Deleteicon.png", UriKind.Relative);
                   biDeleteIcon.EndInit();

                   fefDelButton.SetValue(Image.SourceProperty, biDeleteIcon);
                   fefDelButton.SetValue(Image.VisibilityProperty, Visibility.Visible);
                   grdCharges.Columns.Add(dgtcDel);

                   //create a template column for text box
                   DataGridTemplateColumn dgtTextBox = new DataGridTemplateColumn();

                   //set title of column
                   dgtTextBox.Header = "Line Item Units";
                   dgtTextBox.Width = 150;

                   DataTemplate dtTextBox = new DataTemplate();
                   FrameworkElementFactory fefTextBox = new FrameworkElementFactory(typeof(System.Windows.Controls.TextBox));


                 fefTextBox.AddHandler(TextBox.TextChangedEvent, new TextChangedEventHandler(txtUnitCount_Changed));
                   dtTextBox.VisualTree = fefTextBox;
                   dgtTextBox.CellTemplate = dtTextBox;
                   grdCharges.Columns.Add(dgtTextBox);

                   grdBillingDetails.Children.Add(grdCharges);

                   //   grdCharges.Style = grdHeaderStyle;
                   Grid.SetRow(grdCharges, rowPosition + 1);
                   rowPosition = rowPosition + 2;
               }
       }
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900