简体   繁体   中英

WPF how to I edit rows in a DataGrid that I added programmatically C#

In my application I have an empty DataGrid with three columns created in xaml:

<DataGrid.Columns>
            <DataGridTextColumn Header="Step Number" Binding="{Binding StepNumber}" />
            <DataGridTextColumn Header="Requirement" Binding="{Binding Requirement}" />
            <DataGridTextColumn Header="Description" Binding="{Binding Description}" Width="*" />
</DataGrid.Columns>

I am using the following code to add items (Rows) to the DataGrid :

 Dictionary<string, List<int>> reqLocations = modifier.ValidateRequirements(col, startRow, RequirementCallbackHandler, CredentialHandler);

  List<DataGridItems> rowItems = new List<DataGridItems>();

  foreach (KeyValuePair<string, List<int>> kvp in reqLocations)
  {
    // Create Rows
    rowItems.Add(new DataGridItems() { StepNumber = ReturnStepNumber(kvp.Value[0]), Requirement = kvp.Key, Description = "Loading Requirement..." });
  }

  // Add Items source to DataGrid
  dataGrid.ItemsSource = rowItems ;

During runtime I need to edit these rows to fill in the description column. how can I achieve this? thank you in advance.

EDIT:

To be clear I need to edit these rows through code not manually.

For the person that asked me to include more code, its just a small class here is the code:

public class DataGridItems
{
  public string StepNumber { get; set; }
  public string Requirement { get; set; }
  public string Description { get; set; }
  public ValidityState state { get; set; }
  public HUDI.IJMPSRequirement req { get; set; }

}

Well... First off you are binding to... something, but then immediately replacing that binding with a new source (your rowItems list). If you don't care to try and make your application MVVM, just throw away you data columns definition in the xaml (the grid will pick those up from your rowItems list) and then when you edit your description field in the data grid the rowItems in your code behind will be updated. From there what you do with that list is up to you in terms of persistence or what have you.

If you want to stay in MVVM land then keep the bindings and get rid of the code behind and instead set the datacontext of your data grid to the ViewModel that is holding the things you are binding to.

Maybe add some more code (what does your DataGridItems class look like? What does the ViewModel you are binding to look like?) or some more context?

在Datagrid的下方设置属性,完成后将其设置为True

datagrid.IsReadOnly = False;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM