We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

GridControl only show scrollbars when needed

Hello,

I'd like to have a GridControl that fills an entire form (using Dock fill). 
The GridControl will have two columns: one for input names, and one for values.
The columns widths should be stretched so that the second column fills any extra space if the form is wide.
I also want to have an extra row at the bottom where I set its height to fill any extra vertical space in the form.


I have pasted sample code for my form below, but it is not behaving as I would expect. As I resize the form, the GridControl occasionally shows the vertical or horizontal scrollbar when it should not be shown. I only want to show the horizontal scrollbar if the form width is less than the width of my two columns. Likewise I only want to show the vertical srollbar if my form height is less than the height of my first 10 rows.

Can you please take a look and see why the scrollbars are occasionally appearing when the form is resized?

Also, I'd like to know what I need to do so that the cells cannot be highlighted (for example dragging from the top leftmost cell down and to the right, highlights a bunch of cells).

Thanks for your help.


using System.Drawing;
using System.Windows.Forms;
using Syncfusion.Windows.Forms.Grid;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            gridControl1.BeginUpdate();
            InitGrid();
            InitGridData();
            ResizeGrid();
            gridControl1.EndUpdate();
            this.Resize += Form1_Resize;
        }

        void Form1_Resize(object sender, EventArgs e)
        {
            gridControl1.BeginUpdate();
            ResizeGrid();
            gridControl1.EndUpdate();
        }

        private const int NumInputs = 10;

        private void InitGrid()
        {
            gridControl1.CurrentCellActivating += GridControl_CurrentCellActivating;

            gridControl1.Dock = DockStyle.Fill;
            gridControl1.HScrollPixel = true;
            gridControl1.VScrollPixel = true;
            gridControl1.ShowColumnHeaders = false;
            gridControl1.ShowRowHeaders = false;
            gridControl1.ColCount = 2;
            gridControl1.RowCount = NumInputs + 1;
            gridControl1.TableStyle.ReadOnly = true;
            gridControl1.TableStyle.WrapText = false;
            
            gridControl1.BorderStyle = BorderStyle.None;
            gridControl1.ResizeColsBehavior = GridResizeCellsBehavior.None;
            gridControl1.ResizeRowsBehavior = GridResizeCellsBehavior.None;
            gridControl1.BackColor = Color.FromArgb(187, 212, 246);

            for (int i = 1; i <= gridControl1.RowCount; i++)
                for (int j = 1; j <= gridControl1.ColCount; j++)
                    gridControl1[i, j].Borders.All = new GridBorder(GridBorderStyle.None);
            
        }

        private void InitGridData()
        {
            for (int i = 1; i <= NumInputs; i++)
            {
                gridControl1[i, 1].Text = "Input " + i + ":";
                gridControl1[i, 1].Font.Bold = true;
                gridControl1[i, 2].Text = "Sample Value " + i;
            }
        }

        private void ResizeGrid()
        {
            //get the columns to fill the entire grid. 
            //horizontal scrollbar should only appear if gridControl1.Width < gridControl1.ColWidths[1] + gridControl1.ColWidths[2])
            gridControl1.Model.ColWidths.ResizeToFit(GridRangeInfo.Cols(1, 2));
            if (gridControl1.Width > gridControl1.ColWidths[1] + gridControl1.ColWidths[2])
                gridControl1.ColWidths[2] = gridControl1.Width - gridControl1.ColWidths[1];

            //get the rows to fill the entire grid by adjusting the height of the filler row
            //vertical scrollbar should only appear if gridControl1.Height < rowHeight
            int rowHeight = 0;
            for (int i = 1; i <= NumInputs; i++)
                rowHeight += gridControl1.RowHeights[i];

            if (gridControl1.Height <= rowHeight)
                gridControl1.RowHeights[NumInputs + 1] = 0;
            else
                gridControl1.RowHeights[NumInputs + 1] = gridControl1.Height - rowHeight;
        }

        private void GridControl_CurrentCellActivating(object sender, GridCurrentCellActivatingEventArgs e)
        {
            if (e.ColIndex == 1 || e.RowIndex == gridControl1.RowCount)
                e.Cancel = true;
        } 
    }
}

What the form should look like when there is extra vertical and horizontal space (no scrollbars)



After resizing the form with my sample code, I see both horizontal and vertical scrollbars despite there being extra vertical and horizontal space



3 Replies

AG Anish George Syncfusion Team June 3, 2015 10:45 AM UTC

Hi Taylour,

Thank you for your interest in Syncfusion products.

We suggest you to make use of the HScroll and Vscroll properties for disabling and enabling the Horizontal and Vertical ScrollBar to your needs. By seeing the code you have given I suggest you to use disable these properties when the width and height is less than that of the GridControl. Please refer the below code.

C#:

private void ResizeGrid()

{

    //get the columns to fill the entire grid.

    //horizontal scrollbar should only appear if gridControl1.Width < gridControl1.ColWidths[1] + gridControl1.ColWidths[2])

    gridControl1.Model.ColWidths.ResizeToFit(GridRangeInfo.Cols(1, 2));

    if (gridControl1.Width >= gridControl1.ColWidths[1] + gridControl1.ColWidths[2])

    {

        gridControl1.ColWidths[2] = gridControl1.Width - gridControl1.ColWidths[1];

        this.gridControl1.HScroll = false;

        this.gridControl1.HScrollBehavior = GridScrollbarMode.Disabled;

    }

    else

    {

        this.gridControl1.HScroll = true;

        this.gridControl1.HScrollBehavior = GridScrollbarMode.Enabled;

    }


    //get the rows to fill the entire grid by adjusting the height of the filler row

    //vertical scrollbar should only appear if gridControl1.Height < rowHeight

    int rowHeight = 0;

    for (int i = 1; i <= NumInputs; i++)

        rowHeight += gridControl1.RowHeights[i];

    if (gridControl1.Height <= rowHeight)

    {

        gridControl1.RowHeights[NumInputs + 1] = 0;

        this.gridControl1.VScroll = true;

        this.gridControl1.VScrollBehavior = GridScrollbarMode.Enabled;

    }

    else

    {

        this.gridControl1.VScroll = false;

        this.gridControl1.VScrollBehavior = GridScrollbarMode.Disabled;

        gridControl1.RowHeights[NumInputs + 1] = gridControl1.Height - rowHeight;

    }
}


And for disabling the highlight option of the cells we suggest you to use the Allow Selection property as none. Please check the following code for you reference.

C#:
this.gridControl1.AllowSelection = GridSelectionFlags.None;


Please let us know If you need any further assistance.

Regards,
Anish.


TT tttmack June 3, 2015 05:10 PM UTC

Thank you for your help Anish!


AG Anish George Syncfusion Team June 4, 2015 10:45 AM UTC

Hi Taylour,

We are glad that the reported query has been resolved. Please let us know if you need any further assistance.

Regards,
Ansih.

Loader.
Live Chat Icon For mobile
Up arrow icon