Atozed Forums

Full Version: Using C++Builder to access columns of TIWDBGrid
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi!
I followed this Delphi sample for accessing columns of a TIWDBGrid using C++Builder, but it does not work. 
Below is my code:

Code:
__fastcall TForm1::TForm1(TComponent* Owner) : TFrame(Owner)
{//
 TRESTClient *REST1Client = new TRESTClient(this);
 TRESTRequest *REST1Request = new TRESTRequest(this);
 TRESTResponse *REST1Response = new TRESTResponse(this);
 TClientDataSet *ClientDataSet1 = new TClientDataSet(this);
 TRESTResponseDataSetAdapter *REST1ResponseDataSetAdapter = new TRESTResponseDataSetAdapter(this);

 ClientDataSet1->AfterOpen = &ClientDataSet1AfterOpen;

 REST1Client->BaseURL = "my.URL.php";
 REST1Request->Client = REST1Client;
 REST1Request->Response = REST1Response;
 REST1Response->ContentType = "application/json";
 REST1ResponseDataSetAdapter->Dataset = ClientDataSet1;
 REST1ResponseDataSetAdapter->Response = REST1Response;
 REST1ResponseDataSetAdapter->Active = true;
 REST1Request->Execute();

 TDataSource *DataSource1 = new TDataSource(this);
 DataSource1->DataSet = ClientDataSet1;
 IWDBGrid1->DataSource = DataSource1;

// dynamic_cast<TIWDBGridColumn*>(IWDBGrid1->Columns->Items[0])->Width = (IWDBGrid1->Width/20) * 02; // => Argument out of range
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ClientDataSet1AfterOpen(TDataSet *DataSet)
{//
 dynamic_cast<TIWDBGridColumn*>(IWDBGrid1->Columns->Items[0])->Width = (IWDBGrid1->Width/10) * 2; // => Argument out of range
 dynamic_cast<TIWDBGridColumn*>(IWDBGrid1->Columns->Items[1])->Width = (IWDBGrid1->Width/10) * 1;
 dynamic_cast<TIWDBGridColumn*>(IWDBGrid1->Columns->Items[2])->Width = (IWDBGrid1->Width/10) * 4;
 dynamic_cast<TIWDBGridColumn*>(IWDBGrid1->Columns->Items[3])->Width = (IWDBGrid1->Width/10) * 3;

 dynamic_cast<TIWDBGridColumn*>(IWDBGrid1->Columns->Items[0])->Title->Text = "AFirstTitle";
 dynamic_cast<TIWDBGridColumn*>(IWDBGrid1->Columns->Items[1])->Title->Text = "ASecondTitle";
 dynamic_cast<TIWDBGridColumn*>(IWDBGrid1->Columns->Items[2])->Title->Text = "AThirdTitle";
 dynamic_cast<TIWDBGridColumn*>(IWDBGrid1->Columns->Items[3])->Title->Text = "AFourthTitle";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::IWDBGrid1Render(TObject *Sender)
{//
 // dynamic_cast<TIWDBGridColumn*>(IWDBGrid1->Columns->Items[0])->Width = (IWDBGrid1->Width/10) * 2; // => Argument out of range
}
//---------------------------------------------------------------------------

I am having the error message "Argument out of range" beginning from the first line and for each line.
Any idea of how to get rid of this error?
First off, you are using dynamic_cast incorrectly.  Use static_cast instead.

Second, I'm no IW expert (or even a user!), but it looks like your code may be trying to access the IWDBGrid columns before the ClientDataSet has populated the grid with any data.  Have you tried hooking up the ClientDataSet to the IWDBGrid before executing the REST request?
(12-20-2022, 08:45 PM)rlebeau Wrote: [ -> ]First off, you are using dynamic_cast incorrectly.  Use static_cast instead.

Second, I'm no IW expert (or even a user!), but it looks like your code may be trying to access the IWDBGrid columns before the ClientDataSet has populated the grid with any data.  Have you tried hooking up the ClientDataSet to the IWDBGrid before executing the REST request?

@ Remy, thank you so much for the suggestion. I tried, but no success.
(12-20-2022, 09:49 PM)Ibouka Wrote: [ -> ]
(12-20-2022, 08:45 PM)rlebeau Wrote: [ -> ]First off, you are using dynamic_cast incorrectly.  Use static_cast instead.

Second, I'm no IW expert (or even a user!), but it looks like your code may be trying to access the IWDBGrid columns before the ClientDataSet has populated the grid with any data.  Have you tried hooking up the ClientDataSet to the IWDBGrid before executing the REST request?

@ Remy, thank you so much for the suggestion. I tried, but no success.

[font=Consolas, "Courier New", monospace]Greetings!
Finally, I used a TIWTimer as workaround for going ahead. The result is not perfect, so don’t hesitate to share I you have another solution.
Bellow is the code.
PS : From the beginning it was question of TIWFrame. "TForm1" was a typo.[/font]

Code:
__fastcall TIWFrame1::TIWFrame1(TComponent* Owner) : TFrame(Owner)
{//
TRESTClient *REST1Client = new TRESTClient(this);
TRESTRequest *REST1Request = new TRESTRequest(this);
TRESTResponse *REST1Response = new TRESTResponse(this);
TClientDataSet *ClientDataSet1 = new TClientDataSet(this);
TRESTResponseDataSetAdapter *REST1ResponseDataSetAdapter = new TRESTResponseDataSetAdapter(this);

TDataSource *DataSource1 = new TDataSource(this);
DataSource1->DataSet = ClientDataSet1;
IWDBGrid1->DataSource = DataSource1;

REST1Client->BaseURL = "my.URL.php";
REST1Request->AddParameter("IdField", "IdValue");
REST1Request->Client = REST1Client;
REST1Request->Response = REST1Response;
REST1Response->ContentType = "application/json";
REST1ResponseDataSetAdapter->Dataset = ClientDataSet1;
REST1ResponseDataSetAdapter->Response = REST1Response;
REST1ResponseDataSetAdapter->Active = true;
REST1Request->Execute();

IWTimer1->Interval = 100;
IWTimer1->Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TIWFrame1::IWTimer1Timer(TObject *Sender)
{//
IWTimer1->Enabled = false;

static_cast<TIWDBGridColumn*>(IWDBGrid1->Columns->Items[0])->Width = (IWDBGrid1->Width/20) * 02;
static_cast<TIWDBGridColumn*>(IWDBGrid1->Columns->Items[1])->Width = (IWDBGrid1->Width/20) * 02;
static_cast<TIWDBGridColumn*>(IWDBGrid1->Columns->Items[2])->Width = (IWDBGrid1->Width/20) * 13;
static_cast<TIWDBGridColumn*>(IWDBGrid1->Columns->Items[3])->Width = (IWDBGrid1->Width/20) * 03;

static_cast<TIWDBGridColumn*>(IWDBGrid1->Columns->Items[0])->Title->Text = "AFirstTitle";
static_cast<TIWDBGridColumn*>(IWDBGrid1->Columns->Items[1])->Title->Text = "ASecondTitle";
static_cast<TIWDBGridColumn*>(IWDBGrid1->Columns->Items[2])->Title->Text = "AThirdTitle";
static_cast<TIWDBGridColumn*>(IWDBGrid1->Columns->Items[3])->Title->Text = "AFourthTitle";
}
//---------------------------------------------------------------------------