Hi,
I want to create XamWebGrid dynamically in code behind.I tried to create it but when i give itemsource it shows null.
So please give solution for creating xamwebgrid on code and how to assign itemsource.
Thanks,
Nandu
Hi Nandkishor,
You can try use this sample:
XAML:
<Grid x:Name="GridPanel"> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="25"/> </Grid.RowDefinitions> <Button x:Name="BtnCodeGrid" Click="BtnCodeGrid_Click" Grid.Row="1" Content="Generate" /></Grid> C#
private void BtnCodeGrid_Click(object sender, RoutedEventArgs e){ XamWebGrid grid1 = new XamWebGrid { HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch, VerticalAlignment = System.Windows.VerticalAlignment.Stretch, AutoGenerateColumns = true, KeyboardNavigation = Infragistics.Silverlight.KeyboardNavigation.AllLayouts }; GridDataGenerator generator = new GridDataGenerator(); grid1.ItemsSource = generator.Patients; this.GridPanel.Children.Add(grid1);}
There si all code for sample GridCodeGenerator class:
using System;using System.Collections.Generic;using System.Collections.ObjectModel;using System.ComponentModel;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Ink;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;
namespace XOBTestApp{ public abstract class BaseViewModel : INotifyPropertyChanged { public BaseViewModel() { }
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
protected void OnPropertyChanged(string propertyName) { PropertyChangedEventHandler handler = this.PropertyChanged; if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
} }
public class PatientAdmittance : BaseViewModel { public PatientAdmittance() { }
private string admittanceId; public string AdmittanceId { get { return this.admittanceId; } set { if (this.admittanceId != value) { this.admittanceId = value; this.OnPropertyChanged("AdmittanceId"); } } }
private string firstName; public string FirstName { get { return this.firstName; } set { if (this.firstName != value) { this.firstName = value; this.OnPropertyChanged("FirstName"); } } }
private string lastName; public string LastName { get { return this.lastName; } set { if (this.lastName != value) { this.lastName = value; this.OnPropertyChanged("LastName"); } } }
private DateTime dateOfBirth; public DateTime DOB { get { return this.dateOfBirth; } set { if (this.dateOfBirth != value) { this.dateOfBirth = value; this.OnPropertyChanged("DOB"); } } }
private string gender; public string Gender { get { return this.gender; } set { if (this.gender != value) { this.gender = value; this.OnPropertyChanged("Gender"); } } }
private DateTime admittanceDate; public DateTime AdmittanceDate { get { return this.admittanceDate; } set { if (this.admittanceDate != value) { this.admittanceDate = value; this.OnPropertyChanged("AdmittanceDate"); } } }
private string location; public string Location { get { return this.location; } set { if (this.location != value) { this.location = value; this.OnPropertyChanged("Location"); } } }
private string severity; public string Severity { get { return this.severity; } set { if (this.severity != value) { this.severity = value; this.OnPropertyChanged("Severity"); } } }
private IEnumerable<Illness> history; public IEnumerable<Illness> History { get { return this.history; } set { if (this.history != value) { this.history = value; this.OnPropertyChanged("History"); } } }
}
public class Illness : BaseViewModel { public Illness() { }
private string illnessName; public string IllnessName { get { return this.illnessName; } set { if (this.illnessName != value) { this.illnessName = value; this.OnPropertyChanged("IllnessName"); } } }
private DateTime illnessDate; public DateTime IllnessDate { get { return this.illnessDate; } set { if (this.illnessDate != value) { this.illnessDate = value; this.OnPropertyChanged("IllnessDate"); } } } }
public class GridDataGenerator : BaseViewModel { public GridDataGenerator() { this._patients = new ObservableCollection<PatientAdmittance>(); for (int i = 0; i < 10; i++) { PatientAdmittance instance = new PatientAdmittance { Severity = "Severity" + i.ToString(), AdmittanceId = i.ToString(), FirstName = "Name" + i.ToString(), Gender = "Female", LastName = "LastName" + i.ToString(), Location = "Sofia" }; instance.AdmittanceDate = new DateTime(1, 1, 1); instance.DOB = new DateTime(1, 1, 1);
ObservableCollection<Illness> hist = new ObservableCollection<Illness>();
instance.History = hist;
for (int j = 1; j <= 3; j++) { Illness illness = new Illness { AdmittanceId = i.ToString(), IllnessName = "Illness" + j.ToString(), IllnessDate = new DateTime(1, 1, 1) }; hist.Add(illness); } this._patients.Add(instance); } }
private ObservableCollection<PatientAdmittance> _patients; public ObservableCollection<PatientAdmittance> Patients { get { return this._patients; } set { if (this._patients != value) { this._patients = value; this.OnPropertyChanged("Patients"); } } } }}
I hope that can help .
Best Wishes!
Mihail
I am having a similar problem - I create the xamwebgrid in code and set the itemsource to my data source, but the itemsource remains null & I cannot set the active cell because my row collection is null as well.
The xamWebGrid won't apply the ItemSource until it has Loaded.
So, if you're setting it before it has loaded, you need to wait until it's LoadedEvent to access it's rows.
-SteveZ