C++ Builder 6 - DBGrid


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 1 of 2 12 LastLast
Results 1 to 15 of 22

Thread: C++ Builder 6 - DBGrid

  1. #1
    Join Date
    Dec 2005
    Posts
    31

    C++ Builder 6 - DBGrid

    Hi,

    How to count the number of rows in a DBGrid?
    There are not such methods or properties as RowCount.

    Kind Regards,
    Rowin

  2. #2
    Join Date
    Apr 2006
    Location
    Zagreb, Croatia
    Posts
    152
    if your dbgrid is bound to a datasource->dataset try using the dataset's (TDataSet,TClientDataSet) RecordCount property. If dbgrid is unbound then try defaultrowcountproperty

  3. #3
    Join Date
    Dec 2005
    Posts
    31
    The dbgrid is bound to a dataset but I want to count the number of rows in the dbgrid after it has been populated.
    Can this be done?

    Kind Regards,
    Rowin

  4. #4
    Join Date
    Apr 2006
    Location
    Zagreb, Croatia
    Posts
    152
    sure it can be done, it wouldn't make much sense otherwise
    int rowcount = dataset1->RecordCount;

    where dataset1 is the dataset bound to your datasource component which is bound to
    your dbgrid control; as you can see the dbgrid's rowcount must match the dataset's record count

  5. #5
    Join Date
    Dec 2005
    Posts
    31
    I don't want to count the number of rows from the dataset but from the dbgrid itself.
    For eg: DBGrid->Rowcount ( but rowcount does not exist)

    Kind Regards,
    Rowin

  6. #6
    Join Date
    Apr 2006
    Location
    Zagreb, Croatia
    Posts
    152
    I think, unlike the TStringGrid control, TDBGrid cannot have a fixed number of unbound rows. It is designed to be used specifically with dataset components.

    regards Ivan

  7. #7
    Join Date
    Apr 2006
    Location
    Zagreb, Croatia
    Posts
    152
    One final note
    When you populate a dbgrid the number of rows in the grid must match the corresponding dataset. This is also true if you apply some kind of filtering to the dataset cuz the changes will be reflected in the dbgrid. If this doesn't help you then be more specific about what you're trying to accomplish

  8. #8
    Join Date
    Dec 2005
    Posts
    31
    I'm having a problem when I use the dataset.
    My code is as follows:
    AnsiString TempString = DataModule1->QueryRecordSet->RecordCount;
    EditRecordCount->Text = TempString;

    The problem is that the count returns me -1 when I have a large number of records. After some investigation I found that the result was dependent on the size of my DBGrid.
    For Example, if I have 3 rows in the dataset and all the 3 rows are displayed (without vertical scrollbar), I get the correct count(that is 3)
    But if get 10 records and there is now vertical scrollbar in the DBGrid, I get a count of -1.
    If I now make my DBGrid larger and all 10 records appear without the vertical scrollbar, I get the correct count of 10.

    Have you understood my problem?
    Do you know why I'm getting this? That's why I wanted to count the records from the DBGrid.

    Kind Regards,
    Rowin

  9. #9
    Join Date
    Apr 2006
    Location
    Zagreb, Croatia
    Posts
    152
    I don't think it should happen because I also have a dbgrid which is not large enough
    to display all the records without scrolling. I'using BCB 5 so maybe it's a bug in BCB 6 although I doubt it. Anyway I've used TDBGrid with 10000 records without a hitch. So maybe you could mail me your file and I'll try to fix it for you

  10. #10
    Join Date
    Dec 2005
    Posts
    31
    void __fastcall TOKBottomDlgSearch::ButtonSearchClick(TObject *Sender)
    {

    bool bMod = false;
    StrSearch = "";

    DataModule1->QueryRecordSet->SQL->SetText(StrQueryProjects.c_str());
    if (CheckBoxReportType->State == cbChecked) {
    if (!bMod) { StrSearch = " Where "; bMod = true; }
    else {StrSearch += " AND ";}
    StrSearch += "ReportType ";
    StrSearch += ComboBoxFReportType->Text + " (SELECT ID FROM Report_Type WHERE Report_Type.Description = '";
    StrSearch += DBLookupComboBoxRType->Text + "')";
    }
    if (CheckBoxPSID->State == cbChecked) {
    if (!bMod) { StrSearch = " Where "; bMod = true; }
    else {StrSearch += " AND ";}
    StrSearch += "PSID ";
    StrSearch += ComboBoxFPSID->Text + " '";
    StrSearch += EditPSID->Text + "'";
    }
    if (CheckBoxCompany->State == cbChecked) {
    if (!bMod) { StrSearch = " Where "; bMod = true; }
    else {StrSearch += " AND ";}
    StrSearch += "CompanyName ";
    StrSearch += ComboBoxFCompany->Text + " '";
    StrSearch += EditCompany->Text + "'";
    }
    if ( CheckBoxStatus->State == cbChecked) {
    if (!bMod) { StrSearch = " Where "; bMod = true; }
    else {StrSearch += " AND ";}
    StrSearch += "Status ";
    StrSearch += ComboBoxFStatus->Text + " '";
    StrSearch += ComboBoxStatus->Text + "'";
    }
    if (CheckBoxDeveloper->State == cbChecked) {
    if (!bMod) { StrSearch = " Where "; bMod = true; }
    else {StrSearch += " AND ";}
    StrSearch += "DeveloperID ";
    StrSearch += ComboBoxFDeveloper->Text + " (SELECT Developer_ID FROM Developer WHERE Developer.First_Name + ' ' + Developer.Last_Name = '";
    StrSearch += DBLookupComboBoxDeveloper->Text + "')";
    }

    if (CheckBoxEstimator->State == cbChecked) {
    if (!bMod) { StrSearch = " Where "; bMod = true; }
    else {StrSearch += " AND ";}
    StrSearch += "Estimator_Name ";
    StrSearch += ComboBoxFEstimator->Text + " (SELECT Developer_ID FROM Developer WHERE Developer.First_Name + ' ' + Developer.Last_Name = '";
    StrSearch += DBLookupComboBoxEstimator->Text + "')";
    }

    if (CheckBoxProjectNo->State == cbChecked) {
    if (!bMod) { StrSearch = " Where "; bMod = true; }
    else { StrSearch += " AND ";}
    StrSearch += "Project_ID ";
    StrSearch += ComboBoxFProjectNo->Text + " '";
    StrSearch += EditProjectNo->Text + "'";
    }
    if (!EditStringWhere->Text.IsEmpty()) {
    if (!bMod) { StrSearch = " Where "; bMod = true; }
    else {StrSearch += " AND ";}
    StrSearch += EditStringWhere->Text;
    }
    if (StrSearch.Length() <= 5 ) {
    StrSearch = "WHERE Project_ID <> Project_ID";
    } else {
    // Sharon requested to be sorted by company name
    StrSearch += "ORDER BY CompanyName";
    // End Request for sort
    }
    DataModule1->QueryRecordSet->Active = false;
    DataModule1->QueryRecordSet->SQL->Append(StrSearch);
    DataModule1->QueryRecordSet->Active = true;
    AnsiString TempString = DataModule1->QueryRecordSet->RecordCount;
    EditRecordCount->Text = TempString;
    }


    Is there anything wrong?

    Kind Regards,
    Rowin

  11. #11
    Join Date
    Apr 2006
    Location
    Zagreb, Croatia
    Posts
    152
    try replacing
    AnsiString TempString = DataModule1->QueryRecordSet->RecordCount;
    with
    AnsiString TempString = IntToStr(DataModule1->QueryRecordSet->RecordCount);
    If this doesn't help you then put a breakpoint on the above line and check the value of RecordCount. If the value is incorrect then verify that StrSearch contains a valid (expected) SQL statement.

  12. #12
    Join Date
    Apr 2006
    Location
    Zagreb, Croatia
    Posts
    152
    Rowin , I just saw one post on the internet describing similar problem to yours.
    The vertical scrollbar of a dbgrid seems to cause the problem so try turning it off. You can add a TDBNavigator control to navigate the dbgrid instead.

  13. #13
    Join Date
    Dec 2005
    Posts
    31
    The IntToStr doesn't work.

    How to turn off the vertical scroll bar?

    Kind Regards,
    Rowin

  14. #14
    Join Date
    Apr 2006
    Location
    Zagreb, Croatia
    Posts
    152
    the following code sample comes from http://community.borland.com/article...,17795,00.html


    Removing the Vertical Scroll Bar from a TDBGrid - by Borland Developer Support Staff

    Ratings: be the first! Rate It

    Technical Information Database

    TI2795C.txt Removing the Vertical Scroll Bar from a TDBGrid
    Category :Database Issues
    Platform :Win95/NT
    Product : C++Builder3.x

    Description:
    In order to remove the vertical scrollbar from a TDBGrid component,
    you must override its Paint method. Inside the Paint method you
    must call the SetScrollRange API procedure to set the min and max
    scroll values to zero (this disables the scrollbar), and then call
    the inherited Paint. The code below is a unit containing a new
    component called TNoVertScrollDBGrid that does this. You can copy
    the code into a file called NoScroll.cpp, and add it to the component
    library as a custom component.
    --------------------------------------------------
    // -------------------
    // In the header file:
    // -------------------
    class TNoVertScrollDBGrid : public TDBGrid
    {
    protected: void __fastcall Paint();
    };


    // -----------------
    // In the .cpp file:
    // -----------------

    //---------------------------------------------------------------------------

    void __fastcall TNoVertScrollDBGrid::Paint()
    {
    SetScrollRange(Handle, SB_VERT, 0, 0, false);
    TDBGrid::Paint();
    }

    //---------------------------------------------------------------------------
    namespace NoScroll
    {
    void __fastcall Register(TObject *Sender)
    {
    TComponentClass classes[1] = {__classid(TNoVertScrollDBGrid)};
    RegisterComponents("Data Controls", classes, 0);
    }
    }


    I' m not sure wheter the above will help you. Apparently TDBGrid behaviour also depends on the type of a dataset class (object) you're using. So if nothing else helps consider using TTable with filtering options to replace TQuery.

  15. #15
    Join Date
    Apr 2006
    Location
    Zagreb, Croatia
    Posts
    152
    I also tried using TQuery the way you did. You're right,TQuery->RecordCount does return -1. But I also used TTable which worked fine. TQuery->Last(); TQuery->RecNo also doesn't work. Surely this looks like a bug in TQuery but I can't be sure. You may also try using SELECT COUNT(*) sql statement.
    Last edited by Ivan**; 04-21-2006 at 02:35 AM.

Similar Threads

  1. ListBot Going Out of Business
    By Larry Rebich in forum vb.announcements
    Replies: 1
    Last Post: 06-28-2001, 01:22 PM
  2. Help with Power Builder 6.0
    By dave in forum Database
    Replies: 0
    Last Post: 03-12-2001, 02:57 AM
  3. Get info from a Power Builder DataWindows
    By Javier Vargas in forum VB Classic
    Replies: 0
    Last Post: 11-01-2000, 08:29 AM
  4. Get info from a Power Builder DataWindows
    By Javier Vargas in forum VB Classic
    Replies: 0
    Last Post: 11-01-2000, 08:29 AM
  5. Field limeted in DBGrid V5.0 or V6.0
    By Goran in forum VB Classic
    Replies: 0
    Last Post: 06-16-2000, 08:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center