## wxDbGridTableBase

You can view a database table in a grid using this class.

If you are deriving your own wxDbTable subclass for your table , then you may consider overriding GetCol() and SetCol() to provide calculated fields. This does work but care should be taken when using wxDbGridTableBase in this way.

The constructor and AssignDbTable() call allows you to specify the ownership if the wxDbTable object pointer. If you tell wxGridTableBase to take ownership , it will delete the passed wxDbTable when an new on is assigned or wxGridTableBase's destructor is called. However no checks for aliasing are done so Assign(table,..,true); Assign(table,..,true); is an error. If you need to requery an table object the preferred way is that the client keeps ownership.

Derived From

wxGridTableBase

Include files

<wx/dbgrid.h>

Example

    	// First step, let's define wxDbTable
    	int numColumns = 2;
    	wxDbTable *table = new wxDbTable (db, tblName, numColumns);
    	int int_var;
    	wxChar string_name[255];
    	table->SetColDef (0, "column 0", DB_DATA_TYPE_INTEGER, &int_var,
    			SQL_C_LONG, sizeof(int_var), true);
    	table->SetColDef (1, "column 1", DB_DATA_TYPE_VARCHAR, &string_name,
    			SQL_C_LONG, sizeof(string_name), false);

        // now let's define columns in the grid

        // first way to do it
        wxDbGridColInfo *columns;
        columns = new wxDbGridColInfo(0, wxGRID_VALUE_LONG, "first column",
                  new wxDbGridColInfo(1, wxGRID_VALUE_STRING, "second column",
                  NULL);

        // second way to do it
        wxDbGridColInfo *columns;
        // first column is special
        columns = new wxDbGridColInfo(0, wxGRID_VALUE_LONG, "first column", NULL);
        // all the rest
        columns->AddColInfo (1, wxGRID_VALUE_STRING, "second column");

        // second way may be better when columns are not known at compile time

        // now, let's open the table and make a Query()
        table->Open();
        // this step is very important
        table->SetRowMode (wxDbTable::WX_ROW_MODE_QUERY);
        // in the grid we will see only the rows of the result query
        m_dbTable->Query();

        wxDbGridTableBase *dbgrid = new wxDbGridTableBase(table, columns, wxUSE_QUERY, true);
        delete columns;  // not needed anymore
        wxGrid *grid = new wxGrid ( ... );
        grid->SetTable(dbgrid, true);
        grid->Fit();

Include files

<wx/dbgrid.h>

Helper classes and data structures

wxDbGridTableBase::wxDbGridTableBase
wxDbGridTableBase::ValidateRow
wxDbGridTableBase::UpdateRow
wxDbGridTableBase::AssignDbTable

### wxDbGridTableBase::wxDbGridTableBase

wxDbGridTableBase(wxDbTable *tab, wxDbGridColInfo *ColInfo, int count = wxUSE_QUERY, bool takeOwnership = true)

Constructor.

Parameters

tab

The database table you want to display. Must be opened and queried before display the grid. See the example above.

ColInfo

Columns titles, and other values. See wxDbGridColInfo.

count

You can use a query result set (wxUSE_QUERY, to use wxDbTable::Count(wxDbTable::Count() or you can fix the total number of rows (count >= 0) to display, or specify it if you already know the size in avoid calling

takeOwnership

If true, this class deletes wxDbTable when it stops referring to it, if false application must take care of deleting it.

### wxDbGridTableBase::ValidateRow

void ValidateRow(int row)

It ensures that the row data is fetched from the database, and it the wxDbTable local buffer, the row number passed should be the grid row.

Parameters

row

Row where validation must be done.

### wxDbGridTableBase::UpdateRow

bool UpdateRow(int row)

If row has changed it forces that row to be written back to the database, however support for detecting whether insert/update is required is currently not in wxDbTable, so this function is currently unsupported.

Parameters

row

Row you want to update.

### wxDbGridTableBase::AssignDbTable

bool AssignDbTable(wxDbTable *tab,int count = wxUSE_QUERY, bool takeOwnership = true)

Resets the grid for using with a new database table, but using the same columns definition. This can be useful when re-querying the database and want to see the changes.

Parameters

tab

Database table you want to assign to the grid.

count

Number of rows you want to show or wxUSE_QUERY for using a query.

takeOwnership

If false, user must take care of deleting tab after deleting the wxDbGridTableBase. If true, deletion is made by destructor class.