Solved

Adjusting DBGrid columns width in c++ builder

Posted on 2011-02-13
33
3,602 Views
Last Modified: 2013-12-14
When the user resizes the DBGrid component at run-time the column widths are not resized. Thus, if the width of the DBGrid is larger than the total width of all the columns, you'll get an empty area right after the last column. On the other hand, if the total width of all the columns is larger than the width of the DBGrid a horizontal scroll bar will appear. I want only some of the columns to resize,
but shrink only to a certein width, below that point a horizontal scrollbar should appear.
I've been looking for solution all over and all I could find was written in delphi, some of the code I translated to c++, but not all.
Thank you in advance.
0
Comment
Question by:Albatros77
  • 14
  • 8
  • 8
33 Comments
 
LVL 45

Expert Comment

by:Kdo
ID: 34885342
Hi Albatros,

The TDBGrid (DBGrid) object doesn't have an OnResize event handler.

But a TPanel does.  

Place a TPanel on the form and place the TDBGrid on top of the TPanel.  Set the Align property of the DBGrid object to alClient.

Then program the OnResize event handler of the TPanel to resize the grid.



Good Luck,
Kent
0
 

Author Comment

by:Albatros77
ID: 34885360
I can resize dbgrid just fine, the problem is that when dbgrid is resized columns inside it aren't.
0
 
LVL 45

Accepted Solution

by:
Kdo earned 500 total points
ID: 34885396

Nope.  You have to resize each column.  They don't resize automatically when the DBGrid resizes.


__fastcall TForm1::DBGridReSize (...)
{
  DBGrid1->Columns->Items[0]->Width = ParentObject->Width / Somesize;
  DBGrid1->Columns->Items[1]->Width = ParentObject->Width / Somesize + constant1;
  DBGrid1->Columns->Items[2]->Width = ParentObject->Width / Somesize + constant2;
  DBGrid1->Columns->Items[3]->Width = ParentObject->Width / Somesize + constant3;
...
}

0
 

Author Comment

by:Albatros77
ID: 34995935

void __fastcall TfrmMain::FormResize(TObject *Sender)

{
      double m,i,y,k,z;
      i = 0;
      k = 0;
      m = 0;
      y = 277;
      z = 280;
      i = frmMain->Width;
            k = i - 1324;
            m = k / 3;
            DBGrid1->Columns->Items[7]->Width = (z + m);
            DBGrid1->Columns->Items[8]->Width = (y + m);
            DBGrid1->Columns->Items[9]->Width = (y + m);
}

This was a solution I came up with, maybe it's not the cleanest code, but it works.
0
 
LVL 45

Expert Comment

by:Kdo
ID: 34997828
The solution that Albatross77 (the OP) came up with is virtually identical to the one that was offered to him two weeks earlier.

  http://www.experts-exchange.com/Programming/Editors_IDEs/C_CPP_CS/CPP_Builder/Q_26818849.html?cid=1572#a34885396

This should be in the PAQ.
0
 

Author Comment

by:Albatros77
ID: 35003133
No he did not. Just because code is similar, doesn't mean that it was a hint.
I was on it before,and mainly I used
http://delphi.about.com/od/usedbvcl/l/aa050404a.htm
http://edn.embarcadero.com/article/27548
Besides
1. DBGridReSize doesn't exist
2 . Syntax error ParentObject->Width
3. With all due respect Kdo answer didn't lead me to solution, and would not lead anyone, as a matter of fact it got me back to translating delphi code, and that's what brought me to solution.
 I wrote it here, because I felt like sharing the solution, and marked it as a right solution.

If you feel that Kdo answer was right, than go ahead and mark his answer.

PS. Sorry for that, and please delete everything posted by Alb..7
0
 

Author Closing Comment

by:Albatros77
ID: 35003220
As stated above
0
 
LVL 45

Expert Comment

by:Kdo
ID: 35003668
Hi Albatros,

Experts Exchange strives to be the best of the technology assist sites on the web.  Its experts are all volunteers that donate their time and efforts to help others with their technology issues.  While we're helping you, we're also keeping our skills current.

To get you a quality answer, we need to know exactly what is being asked and to acquire an understanding of what you need and/or expect.  Since this was your first post, a lot of assumptions had to go into formulating an answer.  The topic of your post involves the C++ Builder IDE, databases, and VCL objects.  These aren't normally considered "entry level" skills, so my answer skipped the details that would be offered to a novice and went straight to a solution.

The offered solution was a guideline.  It was impossible to give you a solution that would plug into your project since your post gave no details about your project, just that you needed to resize a grid.


Here's another look at what I posted.  

__fastcall TForm1::DBGridReSize (...)
{
  DBGrid1->Columns->Items[0]->Width = ParentObject->Width / Somesize;
  DBGrid1->Columns->Items[1]->Width = ParentObject->Width / Somesize + constant1;
  DBGrid1->Columns->Items[2]->Width = ParentObject->Width / Somesize + constant2;
  DBGrid1->Columns->Items[3]->Width = ParentObject->Width / Somesize + constant3;
...
}


-- TForm1 is the default name for the first form object.  You renamed it in your project to TFrmMain.
-- DBGridReSize is the name of the method that I chose to show the functionality.  The TGrid and TDBGrid do not have an OnReSize method so the resize is driven by the underlying object.  (TDataGrid objects do have an OnReSize method, but that's applicable only to show that these similar objects had a different foundation class.)  The IDE will create an empty method with a default name of OnReSize by double clicking the empty grid item next to the OnReSize name in the Object Inspector.  You are free to change it.
-- The IDE will create the method prototype and header when the OnReSize method is created.  The only parameter is Sender, which your application won't need.  I abbreviated that to ...
-- Every object in a GUI is owned by another object.  That is, it has a parent object.  ParentObject refers to the object that owns the DBGrid object.  Based on your solution, it appears that your DBGrid was placed directly on the main form.  This works fine, but isn't very flexible.  A better technique would be to place a TPanel on the form, place the DBGrid on the panel, set the DBGrid alignment to client, and resize the DBGrid from the panel's OnReSize method.  This would allow you to more easily control the size of the Grid from a form resize, or a panel resize (typically caused by a splitter being moved).
-- With no hint as to what sizes you needed for the columns, I gave a generic solution.  "somesize + constant".    Your solution is ((Width - 1234) / 3) + constant.

Based on the limited information available to me, I gave you the answer that I thought to be most appropriate.


If you come back to Experts Exchange with another question, just stay engaged in your question(s).  Had you responded with a simple "I don't understand what you mean" I'd have gladly followed up with at least as much explanation as I just gave and had that not been enough I'd have gladly given you even more detail.  Since you didn't respond, I had no choice but to assume that you were satisfied with the response.

Kent
0
 

Author Comment

by:Albatros77
ID: 35004621

According to your code:
DBGrid1->Columns->Items[1]->Width = ParentObject->Width / Somesize + constant 

Open in new window

is the same as
DBGrid2->Columns->Items[1]->Width = (Form1->Width / 3) + 180;

Open in new window

Where does that lead us???

So, in what way this solution was helpful, even as a generic one.

With given solution "((Width - 1234) / 3) + constant." you justified correctness of your code.
It's like justifying correctness of a prophecy after a fact.                                                        

0
 
LVL 16

Expert Comment

by:George Tokas
ID: 35027317
Hi there all,
First of all what Kent proposed is right...
Second let me propose my point of view:
When placing a DBGrid  to a form this has fixed size.
When resizing the form the DBGrid will NOT resize so you have to resize it using code at runtime.
First of all you have to declare the minimum size of the window to have it as a base and not "shrink" more than that to avoid further problems...
After resizing your form calculate the width and height ratio from original to final. This will be a deximal number (float, double) larher than 1.
Multiply the DBGrid size and column width as needed.

AGAIN KENT's SUGGESTION IS RIGHT!!

George Tokas.
0
 
LVL 16

Expert Comment

by:George Tokas
ID: 35027363
By the way also:
All other controls inside the form has to be resized...
And MAYBE you have to think if and the positions of the controls (top,left) have to change also in the new size to produce a better visual result.

George Tokas.
0
 

Author Comment

by:Albatros77
ID: 35030439
First read my 1 post carefully
Second, your "AGAIN KENT's SUGGESTION IS RIGHT!!" is based on what again???

0
 
LVL 16

Expert Comment

by:George Tokas
ID: 35030925
>>First read my 1 post carefully
You have a point there but that doesn't change anything important...
Consider also and resizing the form...
>>Second, your "AGAIN KENT's ... what again???
Here:
>First of all what Kent proposed is right...

Aditional:
Post the working code for Delphi and we (at least me and Kent) will translate it...
By the way for me the greatest problem was resizing the form not cell sizes...
That can be also calculated by adding all column widths and set the DBGrid width as a fast solution...
A bit more complicated one is the way Kent proposed for a fixed total DBGrid size...
But imagine to resize the height of the form only.
What will you do to change the height of DBGrid?
Based on my suggestion you are just multiplying the original height with the resize ratio...

And an idea:
Design time:
Declare the size you want for the DBGrid to have in the form.
I suppose that you will declare also  the sizes of the columns...

RunTime:
Add all column sizes and compare with the declared size.
You will get the resize ratio. Multiply each original size with the ratio and set as needed...
I don't thonk that will fail...

George Tokas.
P.S. If I made any grammar, syntax or "meaning" mistake please forgive me. I am Greek and English happens NOT to be my maternal language.
0
 

Author Comment

by:Albatros77
ID: 35033806
>>First read my 1 post carefully
You have a point there but that doesn't change anything important...
Consider also and resizing the form...
>>Second, your "AGAIN KENT's ... what again???
Here:
>First of all what Kent proposed is right...
First: IT CHANGES EVERYTHING, my question was about resizing columns inside DBGrid, what is the problem in resizing dbgrid. You both made the same mistake by NOT READING MY POST CAREFULLY.
A bit more complicated one is the way Kent proposed for a fixed total DBGrid size...
But imagine to resize the height of the form only.
What will you do to change the height of DBGrid?  
Yet again, how many times??? what height of DBGrid has to do with columns width.
RunTime:
Add all column sizes and compare with the declared size.
You will get the resize ratio. Multiply each original size with the ratio and set as needed...
I don't thonk that will fail...
Could you please share working example
0
 
LVL 16

Expert Comment

by:George Tokas
ID: 35034093
Place a DBGrid with total width larger than the sum all all column sizes.
If you run the project you will see the empty area.
Lets say that we have 4 columns with 200px width inside a DBGrid of 1000px width. There will be a 100px empty area.
It is needed to resize the columns to 250px and that is a ratio of 1.25 (total width/column sum 1000/800)
Multiply each column width with the ratio and it is done...
Same applies when the total width is lower than the sum of column widths. The ratio will be less than 1...

>>what height of DBGrid has to do with columns width.
For simplicity I didn't add and width change...
My point was - and is - that when resizing a form resize and controls because maybe someone - like me - have trouble reading due to old age....
By setting a resize ratio limit when increasing height you expand the visible rows...
I hope I made my point.

George Tokas.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:Albatros77
ID: 35039240
Could you please post a working code example ?
0
 
LVL 45

Expert Comment

by:Kdo
ID: 35039380

Hi Albatros,

The code below is extracted from one of my applications.  It is probably more complex than you need, but shows the kinds of things that you might want to do.

When the form is resized, or the Tpanel underneath the grid object is resized, the FormReSize method is fired.  The TPanel underneath the grid can be resized horizontally or vertically by the movement of a splitter.  The movement of one of the splitters fires FormResize(), which calls methods to handle the splitter movement.  You could just as easily fire a separate method for the splitter movements.

The method also has two methods to calculate the column widths.  One controls a narrow (less than 400 pixel) object, the other a wide (over 400 pixel) object.

There are multiple objects on the Tpanel, a TGrid and a TList.  Both are controlled by the same resize method.


Kent

void __fastcall TMainForm::FormResize(TObject *Sender)
{
  ResizeDrawingGrid ();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ResizeDrawingGrid (void)
{
  Splitter1Moved (this);
  Splitter2Moved (this);
  if (DrawingGrid->Width > 400)
  {
    DrawingGrid->ColWidths[0] = 60;
    DrawingGrid->ColWidths[1] = (DrawingGrid->Width - 80) / 2;
    DrawingGrid->ColWidths[2] = (DrawingGrid->Width - 80) / 2;
  }
  else
  {
    DrawingGrid->ColWidths[0] = (DrawingGrid->Width * 3 / 20);
    DrawingGrid->ColWidths[1] = (DrawingGrid->Width * 8 / 20);
    DrawingGrid->ColWidths[2] = (DrawingGrid->Width * 8 / 20);
  }
  ResultsListView->Columns->Items[0]->Width = ResultsListView->Width * 150 / 755;
  ResultsListView->Columns->Items[1]->Width = ResultsListView->Width * 50 / 755;
  ResultsListView->Columns->Items[2]->Width = ResultsListView->Width * 90 / 755;
  ResultsListView->Columns->Items[3]->Width = ResultsListView->Width * 90 / 755;
  ResultsListView->Columns->Items[4]->Width = ResultsListView->Width * 100 / 755;
  ResultsListView->Columns->Items[5]->Width = ResultsListView->Width * 100 / 755;
  ResultsListView->Columns->Items[6]->Width = ResultsListView->Width * 100 / 755;
  ResultsListView->Columns->Items[7]->Width = ResultsListView->Width * 60 / 755;
}

Open in new window

0
 

Author Comment

by:Albatros77
ID: 35039742
So, posting solution based on your generic code isn't that easy after all.
Now I hope you see my point.
0
 
LVL 45

Expert Comment

by:Kdo
ID: 35039827
I don't see it.  

What I see is someone that asked a question, didn't understand the answer, and instead of asking for a clarification he walked away from the issue.

Simplifying my example to do EXACTLY what you asked, a solution is below.  It's remarkable how much that resembles my original solution as well as the one the you "discovered".


Kent

void __fastcall TMainForm::FormResize(TObject *Sender)
{
    DrawingGrid->ColWidths[0] = 60;
    DrawingGrid->ColWidths[1] = (DrawingGrid->Width - 80) / 2;
    DrawingGrid->ColWidths[2] = (DrawingGrid->Width - 80) / 2;
}

Open in new window

0
 

Author Comment

by:Albatros77
ID: 35041529
So, tell me what exactly did you do to profit out of it. Even now you cannot post a right solution instead you're posting another off subject or completely worthless code.
And what was to understand, I understood just fine that you didn't bother to read the question and didn't know the solution. That why I walk away from continuing this farce, and just started to look elsewhere.
About the damn point, I didn't grant it to you at first, because I didn't use your code, and it didn't lead me to my solution, my code was the right answer, so I marked my post as a solution, not because I wanted to grab some lousy points, but to help the community, after I posted the solution, I canceled my membership, so clearly I'm not into this BS. Suddenly you objected to my decision. So I opened another account as Albatros7(I didn't know that my old account is still active till the end of trial period, while during cancellation, message stated that my account will be suspended immediately ) to clarified my decision, when it occur to me that since I don't care about it, I let you have the credit for it, if it was so important to you . But now I think that I made a mistake, because seems like that's all you care about, and you represent all that I dislike, the damn corrupting everything profit. So don't bother to brag about volunteers donating their time, the knowledge you acquired, is thanks to people who really donated their time, coming back from work, and continue work on open source projects, selflessly spending hours on forums helping others, just to repay for knowledge they acquired from guys who donated theitr time before them. And that's why we are where we are, but lately I see that it's getting worse and worse. And it all starts from small things, like lousy points, then it's get's to selling deliberately half baked products. So it'll lives them some space to make it "better" and sell it again, not to mention everlasting service subscriptions. So selling crap became a business decision. And that's what's getting on my nerves, but not as bad as some people with corrupted judgment implying that, I'm some sort of a crook trying to pull a scam on them, it's funny how fast you came to conclusion.
0
 
LVL 16

Expert Comment

by:George Tokas
ID: 35043581
Hi Kent,
First of all - and forgive me about this - a display of power doesn't needed...
I was tempted to post after:
>>Could you please post a working code example ?
the following:
Do you want US to do the job for you?
But I followed an old teacher's of mine saying: "Think TWICE and say NOTHING!!"
And from me also:
Sorry that the site didn't work out for you.
And:
I hope you have better luck in other communities and find better help....

George Tokas.
0
 
LVL 45

Expert Comment

by:Kdo
ID: 35043642
Hi George,

One of the moderators has already cleaned up this thread once.  (I don't know if you can see the hidden items.  I'll be glad to send you the complete thread if you want it.)  The OP asked a fair question that, by our estimation, was answered.  The question and answer belong in the PAQ without all of the distractions.

Kent
0
 
LVL 16

Expert Comment

by:George Tokas
ID: 35043753
Hi again Kent,
No problem here anyway...
The area of resizing controls is hell and depending on design multiple ways have to be examined...
I cover one way using 3D graphics, combination of Direct3D and VCL controls in the magazine a year or more ago, and maybe I have to do that again to cover it in full, or maybe to add some more I can think right now, given the oportunity of this question....
Anyway I'm not a fan of displaying power where it is not needed.
On the other hand when "authorities" trying to display power on me making me mad as - I hope - you saw in one of my sites...
The story doesn't ended from my side and I'm using another saying of my old teacher goes like that:
"Patience my dear boy. In time all will go your way"
Assuming that you follow the teaching rules...

George.
0
 

Author Comment

by:Albatros77
ID: 35045765
Do you want US to do the job for you?
No, and as you can see, I post my own solution, I simply wanted you to post another solution so others
may choose, which way they prefer, but it looks like that's too much to ask.
One of the moderators has already cleaned up this thread once.
Yes he did, because I asked him to do so, 2 posts were deleted because they were posted as Alba...7(my other account, as I mentioned before) .
(I don't know if you can see the hidden items.  I'll be glad to send you the complete thread if you want it.)
Ough,Come on. That's too much :)
The OP asked a fair question that, by our estimation, was answered.  The question and answer belong in the PAQ without all of the distractions
You mean, my question, and your 500 point answer, everything below that, should be cleaned up.
Well, I'd say everything should stay.
0
 
LVL 16

Expert Comment

by:George Tokas
ID: 35046026
@:Albatros77
You are are making an already bad situation worse.
I don't know who you are or what experience and skills you have.
Maybe you are better from both me and Kent.
But the point is that we - at least - proven that we are trying to help people here and we paying to do that just like you.
Remember that I proposed to post working code from Delphi to - try - translate it to a working solution.
As you can see I don't ignore anybody...
I don't want to use my profile and rank to make my point or links to articles of mine.

By the way if you ever post again a question in this topic area BE SURE that if I can help I will respond...

George Tokas.
0
 

Author Comment

by:Albatros77
ID: 35048249
You are are making an already bad situation worse.
I don't know who you are or what experience and skills you have.
Maybe you are better from both me and Kent.
But the point is that we - at least - proven that we are trying to help people here and we paying to do that just like you.
I know, usualy I don't respond that way, exccept when being called a liar, and that gentlemen simply will not do.
Remember that I proposed to post working code from Delphi to - try - translate it to a working solution.
As you can see I don't ignore anybody...
I don't want to use my profile and rank to make my point or links to articles of mine.

By the way if you ever post again a question in this topic area BE SURE that if I can help I will respond...
I didn't question your knowledge or area of expertize, and your help will be appreciated, thanks.
Thanks to both of you for your help, hopefully, we might be able to avoid complications in the future.
0
 
LVL 45

Expert Comment

by:Kdo
ID: 35048289
Hi Albatros,

I don't believe that anyone called you a liar anywhere in this thread.  But certainly something in the thread has triggered a hostile response.  Can you provide some insight?  What more did you expect in a response?


Kent
0
 

Author Comment

by:Albatros77
ID: 35048323
To be honest, nothing after post 9.
0
 
LVL 16

Expert Comment

by:George Tokas
ID: 35048407
Excuse me again,
Can we call it a missunderstanding?
Back in 2003 I had an almost similar case with Dan Rollins and I got MAD!!
When someone call you a "wannabe" hacker - cracker then triggers some nerve especially when I was trying to protect my own software products...
Anyway I left it behind me since I saw who is he and one time I did almost the same thing later in another case...
Details and links available if you like...
Also I'M NOT FORGETTING that in my time of need except Kent we - lets say - know each other and Dan Rollins supported me...

George Tokas.
0
 

Author Comment

by:Albatros77
ID: 35048906
Sure thing.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org (http://seleniumhq.org) Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now