Dharshini

Dharshini

  • NA
  • 24
  • 24.9k

set the value for wpf datagrid columns dynamically iterating

Apr 16 2013 7:51 AM
I am displaying a datagrid which represents a table from my entity model. It already has 2 columns (with values) and I would like to add a new column to the existing datagrid and update the values of this column by looping through the datagrid rows.
what i have tried. 
  public class ServerMapper
  {
 
  private string _name;
  private string _ip;
  // Following property is not part of the database table and will be only used in the UI.
  private string _status;
  public ServerMapper(string Name, string IP)
  { 
  this.Name = Name;
  this.IP = IP;
  public string Name
  {
  get { return this._name; }
  set { this._name = value; }
  }
  public string IP
  {
  get{ return this._ip; }
  set{ this._ip = value; }
  }
  }
 MainViewModel class
  private void InitServerState(List<ServerMapper> serverMapCol)
  {
  //foreach (DataGridRow row in dgServer)
  //{ I have to iterate here
  //
  foreach (ServerMapper item in serverMapCol)
  {
  {
  var get_count = (from a in this.db.Servers
  from component in this.db.Components
  where a.ServerID == component.ServerID && component.State == "Stopped"
  select a).Count();
  var serverstate = (from a in this.db.Servers
  where a.PingStatus == "online" && a.ServerAgentStatus == "running"
  select a).Any();

  if (get_count == 0 && serverstate)
  {
  item.Status = "ok";
  }
  else if (get_count == 2 && serverstate)
  {
  item.Status = "critical";
  }
  else
  {
  item.Status = "warning";
  }
  }
  }
  }

and I will bind this value of `Status` to my datagrid as
  <DataGridTextColumn x:Name="stateColumn" Header="State">
  <DataGridTextColumn.ElementStyle>
  <!--Style to implement the datagrid cell animation for ServerStatus-->
  <Style TargetType="{x:Type TextBlock}">
  <Style.Triggers>
  <DataTrigger Binding="{Binding Status}" Value="ok">
  <Setter Property="Background" Value="#FF97DE91" />
  </DataTrigger>
  <DataTrigger Binding="{Binding Status}" Value="critical">
  <Setter Property="Background" Value="#FFEFE956" />
  </DataTrigger>
  <DataTrigger Binding="{Binding Status}" Value="warning">
  <Setter Property="Background" Value="#E6F85050" />
  </DataTrigger>
  </Style.Triggers>
  </Style>
  </DataGridTextColumn.ElementStyle>
  </DataGridTextColumn>
but this above code computes the value of Status for all the datagrid rows instead for each row. how to over come this ?