[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Create New DBGrid Column at runtime

Posted on 2007-10-01
4
Medium Priority
?
9,463 Views
Last Modified: 2010-05-08
Hi again,

It's not my day today :o)

I am trying to insert a new Column to a database grid at runtime but get an error "Index out of bounds"

I need this column to replace the current first column. I guess I should be using Insert instead of Add ???

Here is the code:

procedure TForm1.Button4Click(Sender: TObject);
Var
ColumnA : TColumn;
Begin
If DBGrid1.Columns.Count>1 then
begin
DBGrid1.Columns.Insert(DBGrid1.Columns.Count );
ColumnA:=DBGrid1.Columns.Items[DBGrid1.Columns.Count-1];
ColumnA.Title.Caption:='OK';
end
else
begin
ColumnA:= DBGrid1.Columns.Add;
ColumnA.Title.Caption:='OK';
end;
end;

Any ideas?

Thanks

St3VO
0
Comment
Question by:ST3VO
  • 2
4 Comments
 

Author Comment

by:ST3VO
ID: 19994029
Sorry, I modified the code above already :o) I WAS using Insert...hehehe!
0
 
LVL 17

Expert Comment

by:mokule
ID: 19995069
You can simply replace all Your code with

  TColumn(DBGrid1.Columns.Insert(DBGrid1.Columns.Count-1)).Title.Caption := 'OK';
0
 
LVL 4

Accepted Solution

by:
spk2000ar earned 1500 total points
ID: 19996050
I use something like this...

If SOMEOPTIONISSELECTED then //insert a column in first place
    begin
      grDatos.Columns.Insert(0);
      grDatos.Columns[0].FieldName := 'NAMEOFFIELDTOSHOW';
      grDatos.Columns[0].Alignment := taCenter;
      grDatos.Columns[0].Width     := 60;
      grDatos.Columns[0].Title.Alignment := taCenter;
    end;
if SOMEOTHEROPTIONISSELECTED then //insert a column at the end
    begin
      grDatos.Columns.Add;
      grDatos.Columns[grDatos.Columns.Count-1].FieldName := 'OTHERFIELDTOSHOW';
      grDatos.Columns[grDatos.Columns.Count-1].Alignment := taCenter;
      grDatos.Columns[grDatos.Columns.Count-1].Width     := 60;
      grDatos.Columns[grDatos.Columns.Count-1].Title.Caption   := 'TITLEOFOTHERFIELDTOSHOW';
      grDatos.Columns[grDatos.Columns.Count-1].Title.Alignment := taCenter;
    end;

Hope this help!
0
 

Author Comment

by:ST3VO
ID: 19996843
Thanks!!!
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

830 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