SSMS中的网格控制

KMC

我注意到在SSMS(SQL Server Management Studio 2016)中,查询结果在瞬间(超过10k +行)内返回。结果表/网格滚动完美流畅,并且在SSMS上具有极低的内存占用(〜80MB)。这种类似于网格/视图的控制方式可以执行ListView(〜200MB,2-3秒)和DataGrid(〜600MB,8-10秒)。即使我关闭了所有可视化或调整cancententscroll或固定其高度以优化速度,它们在SSMS中的性能仍然远远落后于网格,并且仍然具有缓慢的滚动和GUI操作。

使SSMS如此平滑的网格控件背后是什么?

在此处输入图片说明

西蒙·莫里尔

SSMS grid is not C++, it's not a ListView nor a DataGrid, it does not uses Windows native controls, it's "just" a custom .NET control named GridControl (in a Microsoft.SqlServer.Management.UI.Grid namespace) that belongs to an assembly named Microsoft.SqlServer.GridControl.dll.

You can find it in various places: in the GAC, in %ProgramFiles(x86)%\Common Files\Microsoft Shared\SQL Server Developer Tools, in Visual Studio files, etc.

It's not a redistributable binary AFAIK, so you're not supposed to ship it, it's not documented, and it's not a full-featured grid like others. However, as you found out, it's lightweight and it can be fast, as fast as your underlying data access.

If you want to play with it, here's a small Winforms C# sample (a 10000 x 256 grid, which is 2,5 million cells that opens instantly) that demonstrates how to use it:

using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.SqlServer.Management.UI.Grid;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        private GridControl _control = new GridControl();

        public Form1()
        {
            InitializeComponent();

            for (int i = 0; i < 256; i++)
            {
                _control.AddColumn(new GridColumnInfo { HeaderType = GridColumnHeaderType.Text, IsUserResizable = true });
                _control.SetHeaderInfo(i, "Column " + i, null);
            }

            _control.Dock = DockStyle.Fill;
            _control.GridStorage = new GridStorage();
            Controls.Add(_control);
        }
    }

    // represents a datasource
    public class GridStorage : IGridStorage
    {
        public long EnsureRowsInBuf(long FirstRowIndex, long LastRowIndex)
        {
            return NumRows(); // pagination, dynamic load, virtualization, could happen here
        }

        public void FillControlWithData(long nRowIndex, int nColIndex, IGridEmbeddedControl control)
        {
            // for cell edition
            control.SetCurSelectionAsString(GetCellDataAsString(nRowIndex, nColIndex));
        }

        public string GetCellDataAsString(long nRowIndex, int nColIndex)
        {
            // get cell data
            return nRowIndex + " x " + nColIndex;
        }

        public int IsCellEditable(long nRowIndex, int nColIndex)
        {
            return 1; // 1 means yes, 0 means false
        }

        public long NumRows()
        {
            return 10000;
        }

        public bool SetCellDataFromControl(long nRowIndex, int nColIndex, IGridEmbeddedControl control)
        {
            // when a cell has changed, you're supposed to change your data here
            return true;
        }

        public Bitmap GetCellDataAsBitmap(long nRowIndex, int nColIndex) => throw new NotImplementedException();
        public void GetCellDataForButton(long nRowIndex, int nColIndex, out ButtonCellState state, out Bitmap image, out string buttonLabel) => throw new NotImplementedException();
        public GridCheckBoxState GetCellDataForCheckBox(long nRowIndex, int nColIndex) => throw new NotImplementedException();
    }
}

这是它的样子。您可以在像样的计算机上滚动而不会降低速度。

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章