I am trying to built a Datagrid in Silverlight 2.0 and i am not able to hide a row in a DataGrid While i am able to hide a column in a Data Grid.Can any one tell me how to do that ?
Hello, first you should set the DataGrid's RowHeight to 0. This will make all rows (including the place holder) to become invisible. Then you can handle PreparingRow event, and set all rows but the desired one's Height to the default value 22, or any height
you want.
Note if you don't set DataGrid's RowHeight to 0, and simply set the desired row's Visibility to Collapsed, that row's data will be invisible, but the row's place holder will still be there.
shanaolanxing - I'll transfer to the Windows Azure team, and will have limited time to participate in the Silverlight forum. Apologize if I don't answer your questions in time.
In theory.. when you change the Visible property the row should show up... I tried this and it did not work. I had to handle the propertychanged event of the entity so that when Visibility would change, I could rebind the datagrid items source, e.g.
public grid_test()
{
InitializeComponent();
this.Setupdata();
}
List<Task> tasks = new List<Task>();
private void Setupdata()
{
tasks.Add(new Task() { Name = "one", Visible = true });
tasks.Add(new Task() { Name = "two", Visible = true });
tasks.Add(new Task() { Name = "three", Visible = true });
tasks.Add(new Task() { Name = "four", Visible = true });
tasks.Add(new Task() { Name = "five", Visible = true });
foreach (Task t in tasks)
{
t.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(t_PropertyChanged);
}
dg.ItemsSource = tasks.Where(t => t.Visible.Equals(true));
}
The problem here is obvious, I lose my selected rows, and any other relevant state on the datagrid when rebinding. Still, it seems to me that binding would be the best way to handle this... what about something like this (except I can't figure out how the
index helps me..):
void t_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName.Equals("Visible"))
{
Task t = sender as Task;
int index = tasks.IndexOf(t);
if (t.Visible)
{
// use index somehow to show row?
}
else
{
// use index somehow to collpase row?
}
}
}
well I figured something out so I guess I'll reply to myself in case this helps out someone else. Basically I borrowed from both ideas... if you want to get this to work using databinding you can do it through Converters...
1) create 2 converters, one to convert a boolean to Visibility enum, and one to convert boolean to Double (for height) - actually these converters can be whatever you want, in my case it was a boolean that dictates the display of a row.
Then in the row loading you set the binding for the Row .Visibility and .Height properties, e.g.:
void dg_LoadingRow(object sender, DataGridRowEventArgs e)
{
try
{
Task t = e.Row.DataContext as Task;
if (t != null)
{
Binding b = new Binding("Visible");
b.Converter=Resources["boolToVis"] as IValueConverter;
e.Row.SetBinding(DataGridRow.VisibilityProperty, b);
Binding b2 = new Binding("Visible");
b2.Converter = Resources["boolToHeight"] as IValueConverter;
e.Row.SetBinding(DataGridRow.HeightProperty, b2);
}
}
catch (Exception ex) { }
}
Now when you toggle the entity value (in my case Task.Visible) it will be hidden/displayed in the datagrid accordingly. The only issue I have now is that if the row is selected, and then set to not be visible, the user would not know there was a selected
row... but that's easy enough to handle I think.
Top.Coder
Member
4 Points
6 Posts
Hiding a Row in DataGrid
Mar 12, 2008 10:48 AM | LINK
Hello,
I am trying to built a Datagrid in Silverlight 2.0 and i am not able to hide a row in a DataGrid While i am able to hide a column in a Data Grid.Can any one tell me how to do that ?
Thanks in Advance !
Top.Coder
Member
4 Points
6 Posts
Re: Hiding a Row in DataGrid
Mar 12, 2008 01:56 PM | LINK
Yi-Lun Luo -...
All-Star
25149 Points
2759 Posts
Microsoft
Re: Hiding a Row in DataGrid
Mar 14, 2008 06:28 AM | LINK
Hello, first you should set the DataGrid's RowHeight to 0. This will make all rows (including the place holder) to become invisible. Then you can handle PreparingRow event, and set all rows but the desired one's Height to the default value 22, or any height you want.
void dg_PreparingRow(object sender, DataGridRowEventArgs e){
if (e.Row.Index == 1){
e.Row.Visibility = Visibility.Collapsed;}
else{
e.Row.Height = 22;
}
}
Note if you don't set DataGrid's RowHeight to 0, and simply set the desired row's Visibility to Collapsed, that row's data will be invisible, but the row's place holder will still be there.
leaf_fan
Member
72 Points
34 Posts
Re: Hiding a Row in DataGrid
Jul 28, 2008 11:29 PM | LINK
Yi-Lun Luo,
I thought it might be a better suggestion to actually do this using the ItemsSource / databinding and an expression.
For example, have a .Visible property on your entity, and then use something like:
dg.ItemsSource = dataitems.Where(item => item.Visible.Equals(true));
In theory.. when you change the Visible property the row should show up... I tried this and it did not work. I had to handle the propertychanged event of the entity so that when Visibility would change, I could rebind the datagrid items source, e.g.
public grid_test()
{
InitializeComponent();
this.Setupdata();
}
List<Task> tasks = new List<Task>();
private void Setupdata()
{
tasks.Add(new Task() { Name = "one", Visible = true });
tasks.Add(new Task() { Name = "two", Visible = true });
tasks.Add(new Task() { Name = "three", Visible = true });
tasks.Add(new Task() { Name = "four", Visible = true });
tasks.Add(new Task() { Name = "five", Visible = true });
foreach (Task t in tasks)
{
t.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(t_PropertyChanged);
}
dg.ItemsSource = tasks.Where(t => t.Visible.Equals(true));
}
void t_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName.Equals("Visible"))
{
dg.ItemsSource = tasks.Where(t => t.Visible.Equals(true));
}
}
The problem here is obvious, I lose my selected rows, and any other relevant state on the datagrid when rebinding. Still, it seems to me that binding would be the best way to handle this... what about something like this (except I can't figure out how the index helps me..):
void t_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName.Equals("Visible"))
{
Task t = sender as Task;
int index = tasks.IndexOf(t);
if (t.Visible)
{
// use index somehow to show row?
}
else
{
// use index somehow to collpase row?
}
}
}
leaf_fan
Member
72 Points
34 Posts
Re: Hiding a Row in DataGrid
Jul 29, 2008 02:25 PM | LINK
well I figured something out so I guess I'll reply to myself in case this helps out someone else. Basically I borrowed from both ideas... if you want to get this to work using databinding you can do it through Converters...
1) create 2 converters, one to convert a boolean to Visibility enum, and one to convert boolean to Double (for height) - actually these converters can be whatever you want, in my case it was a boolean that dictates the display of a row.
Then in the row loading you set the binding for the Row .Visibility and .Height properties, e.g.:
void dg_LoadingRow(object sender, DataGridRowEventArgs e)
{
try
{
Task t = e.Row.DataContext as Task;
if (t != null)
{
Binding b = new Binding("Visible");
b.Converter=Resources["boolToVis"] as IValueConverter;
e.Row.SetBinding(DataGridRow.VisibilityProperty, b);
Binding b2 = new Binding("Visible");
b2.Converter = Resources["boolToHeight"] as IValueConverter;
e.Row.SetBinding(DataGridRow.HeightProperty, b2);
}
}
catch (Exception ex) { }
}
Now when you toggle the entity value (in my case Task.Visible) it will be hidden/displayed in the datagrid accordingly. The only issue I have now is that if the row is selected, and then set to not be visible, the user would not know there was a selected row... but that's easy enough to handle I think.
Kevmeister
Member
249 Points
125 Posts
Re: Hiding a Row in DataGrid
Aug 22, 2008 07:19 AM | LINK
DataGridRow does not expose an Index property in S2B2 any more, so this code is now unusable.
tilliw
Member
4 Points
2 Posts
Re: Re: Hiding a Row in DataGrid
Jan 15, 2010 12:55 PM | LINK
there's a simpler and cleaner way of doing this:
see my answer here: http://forums.silverlight.net/forums/p/11359/36279.aspx
jon.borchardt
Member
2 Points
14 Posts
Re: Hiding a Row in DataGrid
Apr 21, 2011 07:17 PM | LINK