Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DBGrid color row if doubleclicked? 2

Status
Not open for further replies.

lespaul

Programmer
Feb 4, 2002
7,083
US
I have a doubleclick procedure for my DBGrid. I would like to also color the doubleclicked row until the user is finished with the form. So the user doubleclicks on row 4 I perform my process and color the row red, then they click on row 8 and I process and now rows 4 & 8 are red, etc. Is there any way to do this?
Thanks! Leslie
landrews@metrocourt.state.nm.us

SELECT * FROM USERS WHERE CLUE > 0
No Rows Returned
 
Hi,

a wrote a Faq on this form about coloring grid rows. Perhaps that will solve partial your question. In the Faq it will color every second row with the same color. The only thing you have to do is to color the selected row.

Steph [Bigglasses]
 
Thanks Steph,

I have used that code already and while that will color the row when I double click, when I double click another row, the first one changes back. How can I keep the row colored if it was previously selected? Leslie
landrews@metrocourt.state.nm.us

SELECT * FROM USERS WHERE CLUE > 0
No Rows Returned
 
So is there no way to do this? Leslie
landrews@metrocourt.state.nm.us

SELECT * FROM USERS WHERE CLUE > 0
No Rows Returned
 
For what it's worth:

I'm new at this game, but it seems to me that you should be able to use an array to track which rows need to be colored and reference that array within the OnColumnDrawCell event to set the Canvas.Brush and redraw the TRect.

I've never tried it so I don't know what other traps there may be in the process.

 
Ok, I think the array idea is probably a good one. Now I need help trying to figure out a way to do it!

I know before the form opens how many records are returned in the dataset, so I can set the length of the array by that. If I make an array of type boolean initialized to False (can I do that?) then when the row is doubleclicked I can take the index of the selected row, and for that array value change the value to true. Then in the draw event, have it check the value of each array element to determine whether or not the row should be colored? Does that sound doable? If there is a draw event, is it automatically called after the double click event or will I have to call it separately?

Thanks for any assistance!

Leslie
landrews@metrocourt.state.nm.us

SELECT * FROM USERS WHERE CLUE > 0
No Rows Returned
 
Hi everyone.

Something like this could work if your dataset supports use of RecNo(like Paradox). We use a TStringList to hold the list of selected/colored records.

{Set as a global variable}
var
NewItems: TStringList;

OnFormCreate
NewItems := TStringList.Create;

OnFormDestroy
NewItems.Free;


On the DoubleClick event of the DBGrid you add the Record Numbers of the records you want to color

var
i : integer;

...

begin

i:=NewItems.IndexOf(IntToStr(Table1.RecNo));

{If not in the list then add}
if i=-1 then begin

NewItems.Add(IntToStr(Table1.RecNo));

end;


On the OnDrawColumnCell event of the DBGrid you do the coloring,


var
i : integer;

...

begin

i:=NewItems.IndexOf(IntToStr(Table1.RecNo));

{So the cell that is currently in focus has a different color than the record selected}

if (i<>-1) then begin {That means the record is in the list}
DBGrid1.Canvas.Brush.Color := clRed;
DBGrid1.Canvas.Font.Color := clYellow;
end

{Give the selected record a different color, if you want}
else if (gdFocused in State) then begin
DBGrid1.Canvas.Brush.Color := clYellow;
DBGrid1.Canvas.Font.Color := clBlack;
end;


DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);
end;

If you want to prevent a certain record from being colored red just remove the item from the TStringList.

var
i : integer;
...
begin

{Removes record #3}
i:=NewItems.IndexOf(IntToStr(3));

if i<>-1 then begin

NewItems.Delete(i);

end;






Look in for TStringList in Delphi help for more info.

 
Nope, we have an IBM AS400 that the RecNo doesn't work on! Do you not think the array idea would work? Leslie
landrews@metrocourt.state.nm.us

SELECT * FROM USERS WHERE CLUE > 0
No Rows Returned
 
So, since I don't have access to the record number, is there any other way to identify the rows that have been clicked?

Thanks!

Leslie
landrews@metrocourt.state.nm.us

SELECT * FROM USERS WHERE CLUE > 0
No Rows Returned
 
I am assuming that one of the columns in the grid contains a unique key value identifying the row. (Or at least some combination of columns that could be concatenated together.)

If so, then when processing the OnDrawColumnCell event, you should be able to access the fields associated with the current row and select the one which contains the unique value. By using a TStringList, you can keep track of which keys are to be highlighted.

TStringList provides Add, Delete and Find methods that should make the job relatively easy to do.

If the data are sorted, the record numbers can change, but the key field values would still correspond to the rows that should be highlighted.
 
So, each time the row is doubleclicked I add the UniqueID (JurNum) to the string list, and when drawing check the value of JurNum to see if it's in the list, if it is, then color the row, if it's not then don't?

I'll give it a try!!!

Thanks for the insight!

Leslie
landrews@metrocourt.state.nm.us

SELECT * FROM USERS WHERE CLUE > 0
No Rows Returned
 
Ah good idea if your database doesn't support RecNO have each record have an ID. I would use a AutoInc field for that. Then you can use the snippet I posted, and modify it a bit ;) Should work!


 
Thanks for keeping in touch. I have tried the TStrings/TStringList and I can't get it to work!! It creates the TSTringList, but nothing ever gets added! I have in my doubleclick procedure

AttendanceList.Add(JMSData.qryJurorSearch.FeildByName('JURNUM').AsString);

it compiles, it reads the code, it just never adds anything to the list!!

Help!

la Leslie
landrews@metrocourt.state.nm.us

SELECT * FROM USERS WHERE CLUE > 0
No Rows Returned
 
lespaul:

Here is the implementation from a working demo:

procedure TForm1.FormCreate(Sender: TObject);
begin
AttendanceList := TStringList.Create;
AttendanceList.Sorted := True;
AttendanceList.Duplicates := dupIgnore;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
AttendanceList.Free;
end;

procedure TForm1.DBGrid1DblClick(Sender: TObject);
var
sJuror:string;
begin
{The following could be done in one statement without the variable, but this allows easier debug:}
sJuror := DataSOurce1.Dataset.Fields.FieldByName('JURNUM').AsString;
AttendanceList.Add( sJuror );
end;

{Click the button to show current contents of string list.}
procedure TForm1.Button1Click(Sender: TObject);
begin
ListBox1.Clear;
ListBox1.Items.Assign(AttendanceList);
end;
 
Thanks for all your help & input. Thanks to you I have been able to implement this attendance function like I wanted from the beginning!

When the Attendance form opens, I fill a TStringList (ProcessList) with the JurNum for all records returned. If the user doubleclicks on a row, I remove the person from ProcessList and add them to AbsentList. In the drawdatacell event, if the DBGrid column for JurNum is in the AbsentList then color the row red! If the user doubleclicks a red row, I reverse the process and remove the number from Absent and add to Process. Then when the user presses OK I run the update query for all the JurNum in ProcessList!

Thanks!!! Cudos and stars to you both!!
Leslie
landrews@metrocourt.state.nm.us

SELECT * FROM USERS WHERE CLUE > 0
No Rows Returned
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top