W
WAMeer
Guest
I am fairly new to WPF and trying to learn it. I have a scenario where I am trying to assign an ArrayList of custom objects to DataGrid control. I have three data grids on the form, for the first one I implemented something like this:
Datagrid_Items.ItemsSource = arraylist_items;
this automatically assigned all the properties of that object in the arraylist to be the columns in the data grid and then I iterate through the columns and hide the unwanted columns like this:
foreach (DataGridColumn dgCol in dgItems.Columns)
{
switch (dgCol.Header.ToString())
{
case "ItemId":
dgCol.Header = "Id";
dgCol.Width = new DataGridLength(15, DataGridLengthUnitType.Star);
break;
case "Description":
dgCol.Header = "Item Description";
dgCol.Width = new DataGridLength(85, DataGridLengthUnitType.Star);
break;
default:
dgCol.Visibility = System.Windows.Visibility.Hidden;
break;
}
}
This code worked perfectly and did what I was expecting it to do, however when I tried to implement the similar logic in other Datagrid it did not work.
I then tried using the Datagrid_AutoGeneratingColumn event to filter out the unwanted colums after binding it to an array of objects like this:
private void dgNotes_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
switch (e.PropertyName)
{
case "Category":
e.Column.Header = "Category";
e.Column.Width = new DataGridLength(15, DataGridLengthUnitType.Star);
break;
case "Note":
e.Column.Header = "Note";
e.Column.Width = new DataGridLength(60, DataGridLengthUnitType.Star);
break;
case "CreatedBy":
e.Column.Header = "Created by";
e.Column.Width = new DataGridLength(15, DataGridLengthUnitType.Star);
break;
default:
e.Cancel = true;
break;
}
}
The first column "Category" is an object so when the datagrid renders it displays the object name rather than value.
Can anyone please tell me how to map the column value to a string property of an object?
Continue reading...
Datagrid_Items.ItemsSource = arraylist_items;
this automatically assigned all the properties of that object in the arraylist to be the columns in the data grid and then I iterate through the columns and hide the unwanted columns like this:
foreach (DataGridColumn dgCol in dgItems.Columns)
{
switch (dgCol.Header.ToString())
{
case "ItemId":
dgCol.Header = "Id";
dgCol.Width = new DataGridLength(15, DataGridLengthUnitType.Star);
break;
case "Description":
dgCol.Header = "Item Description";
dgCol.Width = new DataGridLength(85, DataGridLengthUnitType.Star);
break;
default:
dgCol.Visibility = System.Windows.Visibility.Hidden;
break;
}
}
This code worked perfectly and did what I was expecting it to do, however when I tried to implement the similar logic in other Datagrid it did not work.
I then tried using the Datagrid_AutoGeneratingColumn event to filter out the unwanted colums after binding it to an array of objects like this:
private void dgNotes_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
switch (e.PropertyName)
{
case "Category":
e.Column.Header = "Category";
e.Column.Width = new DataGridLength(15, DataGridLengthUnitType.Star);
break;
case "Note":
e.Column.Header = "Note";
e.Column.Width = new DataGridLength(60, DataGridLengthUnitType.Star);
break;
case "CreatedBy":
e.Column.Header = "Created by";
e.Column.Width = new DataGridLength(15, DataGridLengthUnitType.Star);
break;
default:
e.Cancel = true;
break;
}
}
The first column "Category" is an object so when the datagrid renders it displays the object name rather than value.
Can anyone please tell me how to map the column value to a string property of an object?
Continue reading...