用了这么久的DBGRID,如果不设定数据源,直接用循环一行一行赋值,该这么做?

解决方案 »

  1.   

    以下是一位VC高手实现的代码,可以只提取DBGRID显示区数据,这样大大加快显示速度。
    看了代码我很惭愧,学VC才能正直做到随心所欲,DELPHI还是太上层了**************************
    把DBGrid的DataMode属性设成Unbound,然后通过UnboundReadData消息显示数据,下面是例子,可以参考一下:
     
    void CSheetView::OnUnboundReadData(LPDISPATCH RowBuf, VARIANT FAR* StartLocation, BOOL ReadPriorRows) 
    {
     CTRSSelect& Select= m_pDoc->m_Select;
     if (!m_SheetCtrl.GetSafeHwnd() || !Select.HasSelect()) return;
     
     int Inc= ReadPriorRows ? -1 : 1;
     CDBGridBuffer Buffer(RowBuf);
     
        // If StartLocation is Null then start reading at the end or beginning of the data set.
     long CurRow, Fetched= 0;
     if (StartLocation->vt == VT_NULL)
      CurRow= ReadPriorRows ? Buffer.GetRowCount() - 1 : 0; 
     else 
      CurRow= CLng(*StartLocation) + Inc;
     
        // Transfer data from our data set array to the Buffer object which DBGrid uses to display the data
        TRY_TRS_OPERATION
     { for (int i= 0; i< Buffer.GetRowCount(); i++)
      { if (CurRow< 0 || CurRow>= m_RecCount) break;
     
       // Get database/view record information
       UINT RecordNo= m_pDoc->m_RecBounds.GetRecNo(CurRow);
       CTRSRecord RecInfo;
       BOOL RecExist= m_pDoc->GetRecordInfo(RecInfo, RecordNo);
           
       CString Prefix= IntToStr(RecordNo+1);
       if (RecInfo.CryptDenied)
        Prefix+= " (" + LoadString(IDS_CRYPTDENIED) + ")";
       else if (RecExist)
       { int Percent= (int)(RecInfo.Relevance*100);
        Prefix.Format ("%d (%d%%)", RecordNo+1, Percent);
       }
       Buffer.SetText (i, 0, Prefix);
     
       // Get database/view record from select handle
       for (int j= 0; j< m_pDoc->m_SummarySet.GetSize(); j++)
       {   int  ColNo= m_pDoc->m_SummarySet[j];
        LPCTSTR ColName= m_pDoc->m_Columns[ColNo]->Name;
     
        LPCTSTR Value= m_pDoc->m_RecBuffer.GetValue(RecordNo, ColName);
        if (Value==NULL && RecExist && !RecInfo.CryptDenied) 
         Value= Select.GetSummary(RecordNo, ColName);
     
        CString Str= Value;
        for (int k= 0; k< Str.GetLength(); k++)
         if (Str[k]> 0 && Str[k]< ' ') Str.SetAt(k, ' ');
        Buffer.SetText (i, j+1, Str);
       }
     
       // Set row book
             Buffer.SetBook (i, CurRow);
          CurRow+= Inc;
       Fetched++;
      }
     }
     CATCH_TRS_EXCEPTION
     Buffer.SetRowCount (Fetched); 
    }