winform DataGridView virtual padding, event trigger condition CellValueNeeded

Virtual padding used to process large amounts of data, a field display problems.

DataGridView .net 2.0 is the new table data editing and display controls, simple data display and editing, just a direct and binding to data sources on it. 
For some special cases, we need to fill their own DataGridView, this time only need to follow the order of the ranks, first obtain a line, and then through the Cells property line, get a cell, you can set its Value property. However, this model has a problem, that is, data for tens or hundreds of lines of the line, showing efficiency is not a problem, when the amount of data increases gradually, efficiency has become a very important issue. 
So, how to solve this problem then, read or written similar Grid control friends will think, load data on demand. That is, when we moved in the data table, part of the data to display data on the screen but the actual need, then, that only part of the data can be loaded. For DataGridView, everything is ready, as long as we open VirtualMode, then implement several events (save for loading data), other things DataGridView will do well for us. 
Add a WinForm DataGridView control in a form, set VirtualMode property to True.

CellValueNeeded, the refresh control events, needs to occur when the cell stuffing data, which returns the current cell parameters e rows and columns, the rows and columns according to obtain required values, e is assigned to Value property. 
If only the display data, the event is sufficient, regardless of the amount of data from the data theoretically, by such a method, the DataGridView display, even if the amount of data is bigger, the refresh can be done in constant time. 

------------------------- above quote from: https://blog.csdn.net/qq_30122639/article/details/53739607 --- -------------------------------------------------- ----------------------

CellValueNeeded event triggers There are two conditions:

1, datagridview of VirtualMode property to be set to true .

 

2, CellValueNeeded event trigger must be datagridview datagridview column is a non-bound columns (that is, you can not set the datagridview DataPropertyName property) , and this column must be visible

 

 

 

===============================================================================

Case method

Private  void dgvRequisitionDetail_CellValueNeeded ( Object SENDER, DataGridViewCellValueEventArgs E) 
        { 
            IF (e.RowIndex < 0 ) return ; 
            the DataView DV = (the DataView) dgvRequisitionDetail.DataSource;
             IF (DV == null ) return ;
             // RequisitionStatus: Please lead single Status: 0 pending, one has passed, not by 2, 3, has been completed (already library) 
            IF (e.ColumnIndex == CStatus.Index) 
            { 
                int Status = DbHelper.GetInt (DV [e.RowIndex], " Status " ) ;
                if ((status == (int)RequisitionStatus.TO_AUDIT))
                    e.Value = CConst.SetRequisitionStatus(RequisitionStatus.TO_AUDIT);
                if ((status == (int)RequisitionStatus.NOTPASS))
                    e.Value = CConst.SetRequisitionStatus(RequisitionStatus.NOTPASS);
                if ((status == (int)RequisitionStatus.FINISHED))
                    e.Value = CConst.SetRequisitionStatus(RequisitionStatus.FINISHED);
                if ((status == (int)RequisitionStatus.AUDITED))
                    e.Value = CConst.SetRequisitionStatus(RequisitionStatus.AUDITED);
                int flag = DbHelper.GetInt(dv[e.RowIndex], "flag");
                if (flag == (int)StockNumAndRequisitionNumEnum.STOCKNUM_LACK)//库存数量<请领数量
                {
                    dgvRequisitionDetail.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Tomato;
                }
                if (flag == (int)StockNumAndRequisitionNumEnum.STOCKNUM_ENOUGH)//库存数量>=请领数量
                {
                    dgvRequisitionDetail.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.LightGreen;
                }
            }
        }

Renderings

 

Guess you like

Origin www.cnblogs.com/wsn1203/p/11579032.html