Hi there,
I hope you can help me with this, attached a screenshot that shows what I am looking for.
Thanks,
Bahram Afsharipoor
4 Answers, 1 is accepted
Hi Bahram,
Thank you for the provided image.
If you know the width of the parent container (the one with the scrollbar in the image, let's say called LayoutRoot) you can determine which are the columns currently visible in the viewport in the following manner:
var width = this.LayoutRoot.Width;
double totalWidth = 0;
var visibleColumns = new List<GridViewDataColumn>();
for (int i = 0; i < this.GridView.Columns.Count; i++)
{
var column = this.GridView.Columns[i] as GridViewDataColumn;
var columnWidth = column.ActualWidth;
totalWidth += columnWidth;
if (totalWidth > width)
{
break;
}
else
{
visibleColumns.Add(column);
}
}
MessageBox.Show(string.Join(", ", visibleColumns.Select(x => x.Header.ToString())));
Regards,
Dilyan Traykov
Progress Telerik
Hi Dilyan,
Thank you for your help, I tested this method and unfortunately that does not work for me. This method works when I am changing the width of the gridview, but it does not work when I am scrolling the content of the gridview horizontally. Please correct me if I am wrong.
Thanks,
Bahram Afsharipoor
Hi Dilyan,
Actually I just made some changes into your code in order to accommodate it with my requirements. Here is the new version, just in case someone need it:
public
List<GridViewDataColumn> GetVisibleColumns()
{
double
width =
this
.LayoutRoot.ActualWidth + scrollViewer.HorizontalOffset;
double
nFrznTotalWidth= 0;
double
frznTotalWidth= 0;
List<GridViewDataColumn> visibleColumns =
new
List<GridViewDataColumn>();
for
(
int
i = 0; i <
this
.GridView.Columns.Count; i++)
{
GridViewDataColumn column =
this
.GridView.Columns[i]
as
GridViewDataColumn;
if
(!column.IsFrozen)
{
nFrznTotalWidth += column.ActualWidth;
if
((frznTotalWidth > width) || (nFrznTotalWidth > (width - frznTotalWidth)))
{
break
;
}
else
if
(nFrznTotalWidth > scrollViewer.HorizontalOffset)
{
visibleColumns.Add(column);
}
}
else
{
frznTotalWidth += column.ActualWidth;
}
}
return
visibleColumns;
}
Thanks again for your help!
Regards,
Bahram Afsharipoor
Hi Bahram,
I'm happy to hear that you managed to find a viable solution and thank you for sharing it with our community.
Please let me know if I can further assist you in any way.
Regards,
Dilyan Traykov
Progress Telerik