Delphi Displays and edits memo fields in the DBGrid component

Source: Internet
Author: User


Delphi can be in. NET and Java "Two mountains" under the pressure, still loved by the vast number of programming enthusiasts, support, the reason, in addition to the VCL framework design exquisite, strong database program development capacity is also one of the key factors. When it comes to database development, we cannot fail to mention the powerful data-aware components in Delphi, which are most commonly used in DBGrid components, which provide the data display of two-dimensional tables, which provide a large amount of information and a clear structure at a glance. Although it has so many advantages, however, "No silver bullet" (its effect is not a form to solve all the problems) This is a well-known maxim in the IT industry has come true, and it has many shortcomings, one of which is: when you develop a database application, If you are using a field with MEMO (memo type) in the datasheet, you will notice that this type of field will appear as "(MEMO)" in the default display mode of DBGrid (as shown in Figure 1). The main function of a data table in a Memo field type is to hold a relatively large amount of text or a combination of text and numbers, and in most databases this type of data has a specified number of limits.



Only Show "(MEMO)" This form is obviously not able to meet the needs of users, in order to be able to make the data is actually displayed, we need to write some code to DBGrid do a little functional enhancements. First, we need to create a table in the database named TestTable, which should have at least one memo type of field named data. Then we'll write a code for the Ongettext event of the field to achieve the above requirements. Action steps are as follows:



1. Connect your Tdataset component to the TestTable table in the newly created database.



2. Double-click the Tdataset component to open the field editor (Fields editor).



3. Add Memo field data.



4. Select this field to establish an event handle in the Object Inspector (objects Inspector) by double-clicking the Ongettext event.



Write the code as follows:



procedure TForm1.DBTableDataGetText(Sender: TField;
var Text: String;
DisplayText: Boolean);
begin
Text := Copy(DBTableData.AsString, 1, 50);
end;



The name of the Tdataset object is "DBTable" and the name of the Memo field is "Data", so the Tmemofield name connected to the Memo field by default is "Dbtabledata". We tell DBGrid in the code to display the Memo field as text, which is to show the actual content. Here is a noteworthy place, because the memo field can accommodate more text, in DBGrid all the words, DBGrid will be propped up very large, so we made a limit, but the memo of the first 50 characters displayed. (Figure 2)



After the work is done, we also want to edit the displayed text. This is not possible by default, we are going to build another form, put a dbmemo component on it, use it to edit the text, think there is another problem to be solved, how to trigger an editing process? Just use the keyboard to enter the key, when the record pointer points to this record, press ENTER, then pops up a form where the Dbmemo component can display and edit the text. The implementation code is as follows:



procedure TForm1.DBGrid1KeyDown(
  Sender: TObject;
  var Key: Word;
  Shift: TShiftState);
  begin
   //If the enter key is pressed, the following code will be triggered
   if Key = VK_RETURN then
   begin
    if DBGrid1.SelectedField = DBTableData then
     //Create a new form
     with TMemoEditorForm.Create(nil) do
     try
      //Read the data in the database and use DBMemo to display
      DBMemoEditor.Text := DBTableData.AsString;
      ShowModal;
      DBTable.Edit;
      DBTableData.AsString := DBMemoEditor.Text;
     finally
      Free;
     end;
   end;
  end;



After the above steps are implemented, the actual program runs like this (Figure 3).



Move your brain, clear ideas, list algorithms, a lot of well-known software in the cool function we can come to realize, you readers friends, open Delphi, experience it.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.