Home

DevExpress GridView Populate In Code

Blog Date 30 August 2023

The DevExpress GridView is complex... here's some basics.

Page Side...

    <dx:ASPxGridView ID="myGrid" OnDataBinding="myGrid_DataBinding" runat="server" >
        <Settings ShowFilterRow="True" ShowFilterRowMenu="True" ShowTitlePanel="True"></Settings>
        <SettingsExport EnableClientSideExportAPI="true" />
        <SettingsDataSecurity AllowEdit="False" AllowInsert="False" AllowDelete="False"></SettingsDataSecurity>
        <Toolbars>
            <dx:GridViewToolbar>
                <Items>
                    <dx:GridViewToolbarItem Command="ExportToXlsx" />
                </Items>
            </dx:GridViewToolbar>
        </Toolbars>
    </dx:ASPxGridView>

Code Side...

       protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                FillGrid();
                myGrid.DataSource = Session["dt"] as DataTable;
                myGrid.DataBind();
            }
        }

        protected void FillGrid()
        {
            string qwe = "SELECT * FROM xxx";
            Session["dt"] = RC.SQLResultsNoImp(qwe, RC.LocalConn(), new string[] { }, new object[] { }); 
        }

        protected void myGrid_DataBinding(object sender, EventArgs e)
        {
            if (Session["dt"]  == null)
            {
                FillGrid();
            }
            myGrid.DataSource = Session["dt"] as DataTable;
        }

Explanation...

Fill a datatable with your data - FIRST! Then you have a datatable filled with data. Store this in the session server side. Now, say for example there's 10,000 records in your datatable good 'ole DevExpress's GridView will show - say - 100 of them depending on your pageination size - great! But then you click on the "show me the next 100 items". This is posted back to the server in the ..._Databinding function. Rather than collecting all the data again from the server use the session datatable and DevExpress will work the rest out for you.

NOW! This is OK if your datatable is of a tolerable size. But what if you're connecting directly to a database table with 2.6 million records? 

EITHER 
you can sort of "smart connect" to the database (google "Bind Grid View to Large Data DevExpress"). This allowed DevExpress to sort out the "SELECT TOP 100" and "WHERE X = 'abc'" stuff itself. But this is more complex than my example and you need Entity Framework or similar.

OR
you will have to "prefilter". For example force the user to choose dates, or show only certain values from a field etc. Essentially you need to ensure the SELECT statement produces a tolerable amount of data for your setup etc.

Reader's Comments

Post Your Comment Posts/Links Rules

Name

Comment

Add a RELEVANT link (not required)

Upload an image (not required)

No uploaded image
Real person number
Please enter the above number below




Home
Admin Ren's Biking Blog