delphi中DBGridEh的更新区域

我现在需要实现一个功能,在DBGridEh中选择一条或者多条记录,然后点击某个按钮什么的就把选中的记录的某个字段进行一些修改。我要怎么样知道用户是选择哪几条记录啊???

急急急!!!

另想请问处理字符串的函数,应该有截取字符串某个字符的前面一部分,比如TAB字符。

我的分值不多了,如果认为太少,还可以加分!

[:(]

1:for i := 0 to DBGrid1.SelectedRows.Count - 1 do

begin

ADOQry_User.GotoBookmark(pointer(DBGrid1.SelectedRows[i]));

//处理;

end;

2:可以解释一下原理吗?

是不是当用户在DBGrid中选择了一条记录,程序就自动在数据集中增加了一个书签?然后这些书签又可以当作另外的数据集来访问?

3:关于第二个问题 ,tab的ascii码是#9

str := Copy(recordstr, 1, Pos(#9, recordstr) - 1);

4::)

5:前一问题如nnhww,选中的记录是自动标记的。

用Copy(你的字符串,1,Pos(某字符,你的字符串))

6:to app2001

收到,正在尝试中:)

[:)]

7:[:(]

好像不行啊!

for i := 0 to DBGrid1.SelectedRows.Count - 1 do

begin

ADOQry_User.GotoBookmark(pointer(DBGrid1.SelectedRows[i]));

//处理;//这里的处理和没有使用书签的处理是一样的吗?处理完毕以后要释放书签吗?

//我在应用中出现错误!信息为“缺少更新或刷新的键列信息。”

//怎么解决????[:(]

end;

8:DbGridEh中选择类型有:gstnon,gstRectangle,gstRecordBookmarks,gstColumns,gstAll

等几种,要分别处理,可参考demo1中如下代码:

with AGrid do begin

if Selection.SelectionType = gstNon then Exit;

ss := TStringStream.Create('');

with Datasource.Dataset do

try

// BM := Bookmark;

SaveBookmark;

DisableControls;

try

case Selection.SelectionType of

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;

gstColumns: 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 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;

gstAll: 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 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 枪手哈特:

这些代码要分开的是各种选择方式,比如选择一行,比如选择一列,比如全选...这些代码没有涉及我要解决的错误!

请看下面的错误信息

-----------------------

for i := 0 to DBGrid1.SelectedRows.Count - 1 do

begin

ADOQry_User.GotoBookmark(pointer(DBGrid1.SelectedRows[i]));

//处理;//这里的处理和没有使用书签的处理是一样的吗?处理完毕以后要释放书签吗?

//我在应用中出现错误!信息为“缺少更新或刷新的键列信息。”

//怎么解决????[:(]

end;

现在我要解决的是改变这些被选择的记录(一行)的某个字段的值。

--------------------------------------

哪位仁兄知道的,请不吝赐教!

10:首先看看把所有书签清空,每次用过后记得清空书签

11:看看这段代码:

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!错误信息:缺少更新或刷新的键列信息。

end;

晕啦,这么久都解决不了....[:(]

12:我也遇到过这样的问题

后来不得不筛选出来更新

关注中。。。。

13:惨,问题到现在都没有解决。

14:没人有办法吗?晕

15:按照楼上写的,好象需要先 SaveBookmark;

16:to yanyandt2

暂时按照各位好心提供的代码都没有实现。sql server会给我错误提示,没法提交更新!

17:办法有,换一个空间,用listview可以实现,因为它的第一列克带有浮选框。

如果用DBGridEh,我没用过DBGridEh,想可能和DBGrid差不多,就按DBGrid说吧,记录用户选择的一条或者多条记录可以使用一个变通的方法,如点选时将选中行的颜色,或字体颜色改变,再次点选时恢复本来的颜色,在修改字段的时候从前向后检索颜色,然后修改,修改完毕后所有行都恢复本来颜色。

18:这是delphi 帮助里的例子:

procedure TForm1.CopyDataClick(Sender: TObject);

var

SavePlace: TBookmark;//就是 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;

这里的 dataset 没有 edit,你把 edit 去掉看看

19:楼上的方法真是绝了[:D]

我正考虑用checklistbox做,只是也很麻烦。

这个东东它把#9“翻译”成方框,#9不是TAB吗?晕!

20:这也是帮助里的话,说不是直接从TCustomSQLDataSet继承的类,bookmark等功能

无效。

Note: Unidirectional datasets do not support bookmarks.

Unidirectional datasets raise exceptions on all navigation methods except 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.

楼主,你不会是属于这个问题吧?

21:to yanyandt2

多谢关注,正在尝试...

22:应该不是,因为异常是从SQL server 抛出来的,运行时没有Delphi的英文错误信息,只有Sql Server的中文错误信息!

23:楼主,我测试过的,我下面这个可以完成,不会出错误,

而且选择的都更新了:

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]

终于解决了!

昨天弄了一天,回到家里才终于解决。以下是代码:

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 '''';

怎样判断dbGrid 多选的情况下,选中巨行块(RECT)时 怎么样确定行数!!! 开始行和结束行

TDBGridEhSelectionType = (gstNon, gstRecordBookmarks, gstRectangle, gstColumns, gstAll)

gstRectangle:确定行的开始位置 即选中矩形块时,怎么样确定 起始行和结束行。

还有哪个TdataLink 是什么东东呀?

来自:ly_delphibbs, 时间:2004-11-25 14:17:34, ID:2908179

procedure TForm1.Button1Click(Sender: TObject);

var

first,last,sum:integer;

begin

first:=DBGrid1.SelectedField.FieldNo +1; //开始行

sum:=DBGrid1.SelectedRows.Count ; //选中行数

last:=first+sum-1; //结束行

end;

来自:幽悠白书, 时间:2004-11-25 15:24:46, ID:2908325

怎样判断dbGrid 多选的情况下,选中巨行块(RECT)时 怎么样确定行数!!! 开始行和结束行

TDBGridEhSelectionType = (gstNon, gstRecordBookmarks, gstRectangle, gstColumns, gstAll)

gstRectangle:确定行的开始位置 即选中矩形块时,怎么样确定 起始行和结束行。

=================

gstRectANGgLE-->dbgrideh 的 选中 矩形块状态!!!!!!!!!!!!!!!!!

gstRectangle:begin

QryGrsd.GotoBookmark(pointer(DBGridEh1.Selection.Rect.TopRow));

while True do

begin

Datachange(Edit3.Text,flag,DBGridEh1.SelectedField.FieldName);// 改变数据

if DataSetCompareBookmarks(DBGridEh1.DataSource.Dataset, point(DBGridEh1.Selection.Rect.BottomRow), 当前行的bOOKMARK不会) = 0 then Break;

QryGrsd.Next;

if QryGrsd.Eof then Break;

end;

end;

我写了一个:

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 of

ftDateTime:

Result:=Result+ 'GetDate() ';

ftFloat, ftCurrency:

Result:=Result+ '9999999999.999 ';

ftSmallInt, ftInteger, ftWord:

Result:=Result+ '9999999999 ';

ftBoolean:

Result:=Result+ 'CONVERT(BIT,0) ';//仅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 = gstNon then Exit;

with DBGEh do

begin

with DataSource.Dataset do

begin

DisableControls;

SaveBookmark;

try

case ASelectionType of

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;

gstColumns:

begin

ColList := Selection.Columns;

First;

while Eof = False do

begin

WriteRecord(ColList);

Next;

end;

end;

gstAll:

begin

ColList := VisibleColumns;

First;

while Eof = False do

begin

WriteRecord(ColList);

Next;

end;

end;

end;

finally

RestoreBookmark;

EnableControls;

end;

end;

end;

Result:=True;

end;

你自己去分析吧。

大概的意思就是把选择的内容拷贝出来。

有三种:框选一部门,单击选择N行,单击选择N列。

gstRecordBookmarks块是你要的。

自己看吧。

gstRecordBookmarks:

begin

ColList := VisibleColumns;

for i := 0 to Selection.Rows.Count-1 do

begin

Bookmark := Selection.Rows[I];

end;

end;