This is a migrated thread and some comments may be shown as answers.

How can I get first and last columns that are currently visible(in sight)?

4 Answers 101 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Bahram
Top achievements
Rank 1
Bahram asked on 07 Nov 2019, 07:41 PM

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

Sort by
0
Accepted
Dilyan Traykov
Telerik team
answered on 12 Nov 2019, 11:50 AM

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())));
        
Please give this a try and let me know if such an approach would work for you.

Regards,
Dilyan Traykov
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Bahram
Top achievements
Rank 1
answered on 14 Nov 2019, 04:26 AM

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

0
Bahram
Top achievements
Rank 1
answered on 14 Nov 2019, 07:38 PM

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

0
Dilyan Traykov
Telerik team
answered on 15 Nov 2019, 12:53 PM

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

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
GridView
Asked by
Bahram
Top achievements
Rank 1
Answers by
Dilyan Traykov
Telerik team
Bahram
Top achievements
Rank 1
Share this question
or