Now I need to implement a function. Select one or more records in dbgrideh and click a button or something to modify a field of the selected record. How do I know which records the user chooses ???
Urgent !!!
Another function that processes strings should intercept the first part of a character in a string, such as The Tab character.
My score is not much. If I think it is too small, I can add more points!
[:(]
1: for I: = 0 to dbgrid1.selectedrows. Count-1 do
Begin
Adoqry_user.gotobookmark (pointer (dbgrid1.selectedrows [I]);
// Process;
End;
2: Can I explain it?
Is it true that when a user selects a record in DBGrid, the program automatically adds a bookmarks to the dataset? Then these Bookmarks can be accessed as other datasets?
3: For the second question, the tab's ASCII code is #9
STR: = copy (recordstr, 1, pos (#9, recordstr)-1 );
4 ::)
5: The selected records are automatically marked for the previous issue, such as nnhww.
Use copy (your string, 1, pos (a character, your string ))
6: To app2001
Received, trying :)
[:)]
7: [:(]
It seems no way!
For I: = 0 to dbgrid1.selectedrows. Count-1 do
Begin
Adoqry_user.gotobookmark (pointer (dbgrid1.selectedrows [I]);
// Processing; // is the processing here the same as that without bookmarks? Do I need to release bookmarks after processing?
// I encountered an error in the application! The information is "the updated or refreshed key column information is missing ."
// How can this problem be solved ???? [:(]
End;
8: The types selected in dbgrideh are: ststnon, gstrectangle, gstrecordbookmarks, ststcolumns, and ststall.
For more information, see the following code in demo1:
With agrid do begin
If selection. selectiontype = maid exit;
SS: = tstringstream. Create ('');
With datasource. dataset do
Try
// Bm: = bookmark;
Savebookmark;
Disablecontrols;
Try
Case Selection. selectiontype
Gstrecordbookmarks:
Begin
For I: = 0 to selection. Rows. Count-1 do
Begin
Bookmark: = selection. Rows [I];
For J: = 0 to visiblecolumns. Count-1 do
SS. writestring (stringtab (visiblecolumns [J]. displaytext, J, visiblecolumns. Count-1 ));
SS. writestring (#13 #10 );
End;
End;
Gstrectangle: Begin
Bookmark: = selection. rect. toprow;
While true do begin
For J: = selection. rect. leftcol to selection. rect. rightcol do
If columns [J]. Visible then
SS. writestring (stringtab (columns [J]. displaytext, J, selection. rect. rightcol ));
If comparebookmarks (pointer (selection. rect. bottomrow), pointer (bookmark) = 0 Then break;
Next;
If EOF then break;
SS. writestring (#13 #10 );
End;
End;
Ststcolumns: Begin
For J: = 0 to selection. Columns. Count-1 do
SS. writestring (stringtab (selection. Columns [J]. Title. Caption, J, selection. Columns. Count-1 ));
SS. writestring (#13 #10 );
First;
While EOF = false do begin
For J: = 0 to selection. Columns. Count-1 do
SS. writestring (stringtab (selection. Columns [J]. displaytext, J, selection. Columns. Count-1 ));
SS. writestring (#13 #10 );
Next;
End;
For I: = 0 to a FooterRowCount-1 do begin
For J: = 0 to selection. Columns. Count-1 do
SS. writestring (stringtab (getfootervalue (I, selection. Columns [J]), J, selection. Columns. Count-1 ));
SS. writestring (#13 #10 );
End;
End;
Ststall: Begin
For J: = 0 to visiblecolumns. Count-1 do
SS. writestring (stringtab (visiblecolumns [J]. Title. Caption, J, visiblecolumns. Count-1 ));
SS. writestring (#13 #10 );
First;
While EOF = false do begin
For J: = 0 to visiblecolumns. Count-1 do
SS. writestring (stringtab (visiblecolumns [J]. displaytext, J, visiblecolumns. Count-1 ));
SS. writestring (#13 #10 );
Next;
End;
For I: = 0 to a FooterRowCount-1 do begin
For J: = 0 to visiblecolumns. Count-1 do
SS. writestring (stringtab (getfootervalue (I, visiblecolumns [J]), J, visiblecolumns. Count-1 ));
SS. writestring (#13 #10 );
End;
End;
End;
Result: = ss. datastring;
Finally
// Bookmark: = bm;
Restorebookmark;
Enablecontrols;
End;
Finally
SS. Free;
End;
End;
9: To gunned HART:
These codes should be separated by various selection methods, such as selecting a row, such as selecting a column, such as selecting all... these codes do not involve the errors I want to solve!
See the following error message.
-----------------------
For I: = 0 to dbgrid1.selectedrows. Count-1 do
Begin
Adoqry_user.gotobookmark (pointer (dbgrid1.selectedrows [I]);
// Processing; // is the processing here the same as that without bookmarks? Do I need to release bookmarks after processing?
// I encountered an error in the application! The information is "the updated or refreshed key column information is missing ."
// How can this problem be solved ???? [:(]
End;
Now I want to solve the problem of changing the values of a field in these selected records (one row.
--------------------------------------
If anyone knows this, please kindly advise!
10: first check to clear all bookmarks. Remember to clear bookmarks after each use.
11: Look at this Code:
For I: = 0 to dbgrideh2.selectedrows. Count-1 do
Begin
Adoqgallet. gotobookmark (pointer (dbgrideh2.selectedrows [I]);
Adoqgallet. Edit;
Adoqgallet. fieldbyname ('yn _ saled '). asinteger: = 1;
Adoqgallet. fieldbyname ('to _ sale'). asdatetime: = Date ();
Adoqgallet. Post; // post is not allowed! Error message: the updated or refreshed key column information is missing.
End;
Dizzy, it cannot be solved for so long... [:(]
12: I have encountered such a problem.
Later, I had to filter out updates.
....
13: the problem has not been solved until now.
14: Is there any solution? Dizzy
15: As written upstairs, it seems that you need to savebookmark first;
16: To yanyandt2
For the time being, the Code provided by all of you has not been implemented. SQL Server will prompt me an error and cannot submit an update!
17: there is a way to change the space, which can be implemented using listview, because its first column has a flotation box.
If dbgrideh is used, I have never used dbgrideh. If it is similar to DBGrid, let's talk about DBGrid. A work und can be used to record one or more records selected by the user, for example, you can change the color of the selected row or font when you click it again to restore the original color. When you modify a field, you can retrieve the color from the front to the back and modify it, after modification, all rows are restored to the original color.
18: here is an example in Delphi's help:
Procedure tform1.copydataclick (Sender: tobject );
VaR
Saveplace: tbookmark; // It is the pointer.
Prevvalue: variant;
Begin
Withclientdataset1 do
Begin
{Get a bookmark so that we can return to the same record}
Saveplace: = getbookmark;
Try
{Move to prior record}
Findprior;
{Get the value}
Prevvalue: = Fields [0]. value;
{Move back to the bookmark
This may not be the next record anymore
If something else is changing the dataset asynchronously}
Gotobookmark (saveplace );
{Set the value}
Fields [0]. Value: = prevvalue;
{Free the bookmark}
Finally
Freebookmark (saveplace );
End;
End;
End;
The dataset does not have the edit parameter. You can remove the edit parameter.
19: The upstairs method is absolutely extinct [: d]
I am considering using checklistbox, but it is also very troublesome.
In this case, it translates #9 into a box. #9 is not a tab? Dizzy!
20: This is also helpful. It is not a class inherited directly from tcustomsqldataset, Bookmark, and other functions.
Invalid.
Note: unidirectional datasets do not support bookmarks.
Unidirectional datasets raise exceptions on all navigation methods should t for first and next. they do not support filters, bookmarks, lookup fields, or any other features that require an internal record buffer. you can use a unidirectional dataset as the source to a data-aware grid.
You don't belong to this problem, do you?
21: To yanyandt2
Thank you for your attention. You are trying...
22: No, because the exception was thrown from SQL Server. At runtime, there was no English Delphi error message and only the Chinese error message of SQL Server!
23: I tested it. I can do the following without making any errors,
All selected items are updated:
Procedure tform1.button1click (Sender: tobject );
VaR
I: integer;
Begin
For I: = 0 to dbgrid1.selectedrows. Count-1 do
Begin
Adoquery1.gotobookmark (pointer (dbgrid1.selectedrows [I]);
Adoquery1.edit;
Adoquery1.fields [1]. asinteger: = 10;
End;
End;
24: [: d]
Finally solved it!
It took a day yesterday to finally solve the problem. The following code is used:
For I: = 0 to dbgrideh2.selectedrows. Count-1 do
Begin
Adoqgallet. gotobookmark (pointer (dbgrideh2.selectedrows [I]);
Strwhichmaterial: = adoqgallet. fieldbyname ('stack _ no'). asstring;
Sqlstr: = 'Update gallet_output set yn_saled = 1, to_sale = '''
Datetimetostr (date () ''' where stack_no = '''strwhichmaterial '''; how to determine whether to select multiple chunks in DBGrid) how to determine the number of rows !!! Start row and end row
Tdbgridehselectiontype = (ststnon, gstrecordbookmarks, gstrectangle, ststcolumns, and ststall)
Gstrectangle: determines the start position of a row, that is, how to determine the start and end rows when selecting a rectangle block.
What else is tdatalink?
From:Ly_delphibbs,Time:14:17:34,ID:2908179 procedure tform1.button1click (Sender: tobject );
VaR
First, last, sum: integer;
Begin
First: = dbgrid1.selectedfield. fieldno + 1; // start line
Sum: = dbgrid1.selectedrows. Count; // select the number of rows
Last: = first + sum-1; // end row
End;
From:You white book,Time:15:24:46,ID:2908325 how to determine the number of rows when multiple DBGrid options are selected and when selecting a giant row block (rect !!! Start row and end row
Tdbgridehselectiontype = (ststnon, gstrecordbookmarks, gstrectangle, ststcolumns, and ststall)
Gstrectangle: determines the start position of a row, that is, how to determine the start and end rows when selecting a rectangle block.
========================
Gstrectanggle --> dbgrideh selected rectangular block status !!!!!!!!!!!!!!!!!
Gstrectangle: Begin
Qrygrsd. gotobookmark (pointer (dbgrideh1.selection. rect. toprow ));
While true do
Begin
Datachange (edit3.text, flag, dbgrideh1.selectedfield. fieldname); // Change Data
If datasetcomparebookmarks (dbgrideh1.datasource. dataset, point (dbgrideh1.selection. rect. bottomrow), the bookmark of the current row will not) = 0 Then break;
Qrygrsd. Next;
If qrygrsd. EOF then break;
End;
End;
I wrote one:
Function getselecteddatabyrow (dbgeh: tdbgrideh; qrydest: tquery;
Colname: string; colsize: integer; valname: string; fixedcols: tstrings): Boolean;
Function getfieldsqls: string;
Function getfieldsql (fieldname: string): string;
VaR
TMP: tfield;
N: integer;
Begin
TMP: = dbgeh. datasource. dataset. findfield (fieldname );
Result: = '[' + fieldname + '] = ';
If TMP = nil then
Begin
Result: = Result + ''' + strn ('', 100) + '''';
Exit;
End;
Case TMP. datatype
Ftdatetime:
Result: = Result + 'getdate ()';
Ftfloat, ftcurrency:
Result: = Result + '2014. 9999999999 ';
Ftsmallint, ftinteger, ftword:
Result: = Result + '123 ';
Ftboolean:
Result: = Result + 'convert (bit, 0) '; // only useful in SQL Server
Else
Begin
N: = TMP. size;
If n <= 0 Then N: = 30;
Result: = Result + ''' + strn ('', n) + '''';
End;
End;
End;
VaR
I: integer;
Tmpstr: string;
Begin
Result: = '';
If fixedcols <> nil then
For I: = 0 to fixedcols. Count-1 do
Begin
Tmpstr: = getfieldsql (fixedcols [I]);
If tmpstr = ''then continue;
If I = 0 then
Result: = 'select' + tmpstr
Else
Result: = Result + ',' + tmpstr;
End;
If (result <> '') and (colname <>'') then
Begin
Result: = Result + ', [' + colname + '] = ''' + strn ('', colsize) + '''';
Result: = Result + ', [' + colname + '_ d] = ''' + strn ('', colsize) + '''';
Result: = Result + ', [' + valname + '] = 999999999.99 ';
End;
End;
Procedure writerecord (collist: tcolumnsehlist );
VaR
I, J: integer;
TMP: string;
Begin
With dbgeh. datasource. dataset do
Begin
For I: = 0 to collist. Count-1 do
Begin
Qrydest. append;
For J: = fixedcols. Count-1 downto 0 do
Begin
TMP: = fixedcols [J];
If findfield (TMP) = nil then
Qrydest [TMP]: =''
Else
Qrydest [TMP]: = fieldbyname (TMP). asstring;
End;
TMP: = collist [I]. fieldname;
Qrydest [colname]: = TMP;
Qrydest [colname + '_ d']: = collist [I]. Title. Caption;
If findfield (TMP) = nil then
Qrydest [valname]: = 0
Else if fieldbyname (TMP). datatype in [ftfloat, ftinteger, ftsmallint, ftword, ftautoinc, ftcurrency] Then
Qrydest [valname]: = fieldbyname (TMP). asfloat
Else
Qrydest [valname]: = 0;
Qrydest. post;
End;
End;
End;
VaR
Tmpstr: string;
I: integer;
Collist: tcolumnsehlist;
Aselectiontype: tdbgridehselectiontype;
Begin
Result: = false;
Qrydest. SQL. Clear;
Tmpstr: = getfieldsqls;
If tmpstr = ''then exit;
Qrydest. SQL. Add (getfieldsqls );
Qrydest. Active: = true;
While not qrydest. EOF do
Qrydest. Delete;
Aselectiontype: = dbgeh. selection. selectiontype;
If aselectiontype = maid exit;
With dbgeh do
Begin
With datasource. dataset do
Begin
Disablecontrols;
Savebookmark;
Try
Case aselectiontype
Gstrecordbookmarks:
Begin
Collist: = visiblecolumns;
For I: = 0 to selection. Rows. Count-1 do
Begin
Bookmark: = selection. Rows [I];
Writerecord (collist );
End;
End;
Gstrectangle:
Begin
Collist: = tcolumnsehlist. Create;
Try
For I: = selection. rect. leftcol to selection. rect. rightcol do
If columns [I]. Visible then
Collist. Add (columns [I]);
Bookmark: = selection. rect. toprow;
While true do
Begin
Writerecord (collist );
If comparebookmarks (pointer (selection. rect. bottomrow), pointer (bookmark) = 0 Then break;
Next;
If EOF then break;
End;
Finally
Collist. Free;
End;
End;
Ststcolumns:
Begin
Collist: = selection. columns;
First;
While EOF = false do
Begin
Writerecord (collist );
Next;
End;
End;
Ststall:
Begin
Collist: = visiblecolumns;
First;
While EOF = false do
Begin
Writerecord (collist );
Next;
End;
End;
End;
Finally
Restorebookmark;
Enablecontrols;
End;
End;
End;
Result: = true;
End; analyze it yourself.
The general meaning is to copy the selected content.
There are three types: select a department, click Select n rows, and click Select n columns.
Gstrecordbookmarks are what you want.
Let's see for yourself. Gstrecordbookmarks:
Begin
Collist: = visiblecolumns;
For I: = 0 to selection. Rows. Count-1 do
Begin
Bookmark: = selection. Rows [I];
End;
End;