简体   繁体   English

如何突出显示DBgrid(SMDBGrid)中单击的单元格中的数据?

[英]How to highlight data in clicked cell in a DBgrid (SMDBGrid)?

I'm using SMDBGrid in a Delphi application to show and edit data as as result of a query from MySQL. 我在Delphi应用程序中使用SMDBGrid来显示和编辑数据,这是来自MySQL查询的结果。

I would like some help understanding how to make the grid behave the way I want when editing the data. 我希望获得一些帮助,以帮助您理解如何在编辑数据时使网格表现出我想要的方式。

The data in the cells is initially shown in a black font on a white background. 单元格中的数据最初以白色背景上的黑色字体显示。

  1. If I LEFT click once in a cell containg data, the cell changes to a white font on a white background (or maybe the data just vanishes) and I can't see the data to edit it. 如果我在包含数据的单元格中单击一次,则该单元格将变为白色背景上的白色字体(或者数据可能消失了),而我看不到要编辑的数据。

  2. If I LEFT click once again in the same cell I get a white font on a blue background. 如果我在同一单元格中再次单击,我会在蓝色背景上得到白色字体。 I can see the data and I can edit it. 我可以看到数据,也可以编辑它。

  3. If I LEFT click a third time in the same cell I get a black font on a white background (just like the other cells). 如果我在同一个单元格中第三次单击,我会在白色背景上得到黑色字体(就像其他单元格一样)。 I can see the data and I can edit it. 我可以看到数据,也可以编辑它。

  4. DOUBLE clicking a cell once behaves like (1), double clicking thereafter always behaves like (2) 双击单元格一次就像(1),然后双击总是像(2)

Question

How do I set it up so that left clicking once in a cell simply allows me to edit that cell without changing the colours? 如何设置它,以便在单元格中单击一次仅使我可以在不更改颜色的情况下编辑该单元格? ie behave like in (3) above, continue showing a black font on whatever background colour the cell had orginally. 也就是说,其行为类似于上面的(3),在单元格最初具有的任何背景颜色上继续显示黑色字体。

Other info that might be relevant 其他可能相关的信息

After running the query to populate the grid I put it into edit mode with 运行查询以填充网格后,我将其置于编辑模式

  • Query1.edit Query1.edit

I do have a OnMouseDown event in the Grid that detect a RIGHT click and show a PopUpMenu which offers choices and sets values in other fields of the record. 我确实在Grid中有一个OnMouseDown事件,该事件检测到右键单击并显示一个PopUpMenu,该菜单提供选择并在记录的其他字段中设置值。 There is also a OnGetCellParams event that sets the background colour of cells depending upon the value in these other fields. 还有一个OnGetCellParams事件,它根据这些其他字段中的值设置单元格的背景颜色。 To get the popup menu to work properly and not show the (built in ?) editing menu when right clicked, by trial and error I set 为了使弹出菜单正常工作,并且右键单击时不显示(内置?)编辑菜单(通过反复试验,我设置了该菜单)

  • PopUpMenu = MyPopUpMenu PopUpMenu = MyPopUpMenu
  • dgEditing = true dgEditing = true
  • dgAlwaysShowEditor = true dgAlwaysShowEditor = true
  • dgAlwaysShowSelection = false dgAlwaysShowSelection = false

but I'm not really sure what I am doing there. 但我不确定自己在那做什么。

Anyway, removing all that by removing the OnMouseDown and OnGetCellParams events and setting PopUpMenu to '' seemed to make no difference to the what happened when a cell was LEFT clicked. 无论如何,通过删除OnMouseDown和OnGetCellParams事件并将PopUpMenu设置为''来删除所有内容,似乎与单击单元格时发生的情况没有任何区别。 The data still went white. 数据仍然变白。

I have found one way to do this now, but not sure if this is the correct/best way to do it as it seems I have to do by code something that I thought would have happened automatically. 我现在找到了一种执行此操作的方法,但是不确定这是否是正确/最佳的执行方法,因为似乎我必须通过编写我认为会自动发生的代码来执行此操作。 Anyway I got the behaviour by using the OnDrawColumnCell Event. 无论如何,我通过使用OnDrawColumnCell事件获得了行为。

procedure TFrmDataEntry.SMDBGrid1DrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
//when they click in a cell, temporarily set the background to white and the font to bold black
if (gdSelected in State) then
    begin
    TDBGrid(Sender).Canvas.Brush.Color := clwhite;
    TDBGrid(Sender).Canvas.Font.Style := Font.Style + [fsBold];
    TDBGrid(Sender).Canvas.Font.Color := clblack;
    end;
 TDBGrid(Sender).DefaultDrawColumnCell(Rect, DataCol, Column, State); //Update the grid so it takes effect
 end; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果日期格式不正确或空白,如何突出显示单元格? - How to highlight cell if date format is incorrect or blank? 如何通过dbgrid更改mysql数据库的值? - How to change values of a mysql database via dbgrid? 添加搜索以搜索DBGrid中的数据并临时更改DBGrid显示的内容-Delphi - Adding a search that searches data in a DBGrid and temporarily changes what that DBGrid displays- Delphi 如何存储单击的表格单元格的变量并将其转换为另一种形式? - How to store the variable of the clicked table cell and transfer it to another form? 如何使用DBGrid从第3列的第1行,第2行,第3行获取值? - How to take a value from row 1, row 2, row 3 in column 3 using DBGrid? DBGrid-OnCellClick和OnDblClick,返回包含单元格/列值/秒的表单/ TBMemo - DBGrid - OnCellClick & OnDblClick, return a form/TBMemo containing cell/column value/s 单击其他单元格后,如何在文本框中获取gridview的单元格值? - How to get the cell value of a gridview in text box when other cell is clicked? 单击按钮后,保存到MySQL,然后显示CSS高亮 - After button clicked, save to MySQL then display CSS highlight 单击按钮时如何在文本框中显示不同的sql数据 - How to display different sql data in a text box when a button is clicked 单击照片时如何在MySQL中插入一些数据 - How to insert some data into MySQL when a photo is clicked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM