populate dynamic datagrid column and binding with editable using mVVm
-
Thursday, July 28, 2011 4:15 PM
Hi Rockz,
I have few clarification first. I have to create dynamic datagrid and it needs to be editable. I have tried hard but i have no luck. here i can create the dynamic datagrid but i am unable to bind those control in viewmodel properties. Is it possible to bind the viewmodel properties with dynamically created data column? Here i have given the code below. please anyone give the better soultuion to acheive my goal.
View : Xaml file
<Grid>
<DataGrid AutoGenerateColumns="True" ItemsSource="{Binding StudentList}" Height="200" Name="dataGrid1" Width="500"></DataGrid>
</Grid>Xaml.cs file
public partial class Overview : Window
{
public StudentViewModel vm { get; set; }
public Overview()
{
InitializeComponent();
GenerateColumn();
vm = new StudentViewModel();
this.DataContext = vm;
}
public ObservableCollection<double> DataList { get; set; }
public ObservableCollection<string> TitleList { get; set; }
private void GenerateColumn()
{
ObservableCollection<double> m_DataList = new ObservableCollection<double>();
m_DataList.Add(1);
m_DataList.Add(2);
m_DataList.Add(3);
DataList = m_DataList;
ObservableCollection<string> m_TitleList = new ObservableCollection<string>();
m_TitleList.Add("Suman");
m_TitleList.Add("Lobo");
m_TitleList.Add("Thiru");
TitleList = m_TitleList;
int n = 0;
DataGridCheckBoxColumn chk = new DataGridCheckBoxColumn();
Binding bindings = new Binding("isCheked");
chk.Binding = bindings;
bindings.Mode = BindingMode.TwoWay;
dataGrid1.Columns.Add(chk);
foreach (string title in TitleList)
{
DataGridTextColumn col = new DataGridTextColumn();
col.Header = title;
//Binding binding = new Binding(string.Format("DataList[{0}]", n++));
Binding binding = new Binding(title) { StringFormat = "as {0}" };
binding.Mode = BindingMode.TwoWay;
col.Binding = binding;
dataGrid1.Columns.Add(col);
}
}
}ViewModel :
public event PropertyChangedEventHandler PropertyChanged;
private ObservableCollection<Student> _studentList;
public ObservableCollection<Student> StudentList
{
get
{
return _studentList;
}
set
{
if (_studentList != value)
{
_studentList = value;
OnPropertyChanged("StudentList");
}
}
}private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this,
new PropertyChangedEventArgs(propertyName));
}
public void PopulateStudents()
{
var itemList = new ObservableCollection<Student>()
{new Student(){StudentName="Frodo Baggins",
Project1Score = 89M,
Project2Score=93M,
Project3Score=88M},
new Student(){StudentName="Rosie Cotton",
Project1Score = 97M,
Project2Score=93M,
Project3Score=94M},
new Student(){StudentName="Samwise Gamgee",
Project1Score = 83M,
Project2Score=90M,
Project3Score=85M},
new Student(){StudentName="Peregrin Took",
Project1Score = 69M,
Project2Score=72M,
Project3Score=75M}};
StudentList = itemList;
}Please help me where i made a mistake?
clarification:
1. In MVVM, Is it possible to populate the dynamic DataGrid in WPF?
2. in MVVM, Is it possible to access the View element to Viewmodel?
3. In Mvvm, If we created the dynamic column in datagrid, how should i bind the value for those controls?
Please reply ASAP.
All Replies
-
Thursday, July 28, 2011 9:01 PM
I wrote a post on this a while back. It is targeted towards the Silverlight DataGrid, but the concept is the same:
- Marked As Answer by Yves.ZModerator Tuesday, August 09, 2011 9:24 AM
-
Friday, July 13, 2012 6:53 PM
But it's not MVVM.
You do everything in codebehind.
I don't know everything, but most of the time I know where to look.
-
Monday, July 16, 2012 6:49 PM
Feel free to move the population of the ItemsSource into the ViewModel. Everything else related to the creation of the columns does not belong in a ViewModel. If anything you could create a custom Behavior that would manage the creation of dnamic columns for you.

