1. 获取数据库表头字段名称
方法1:
DBGridEh2.Columns.Count //有多少个表头. 计数从0开始.
DBGridEh2.Columns[i].Field.FieldName; //获取表头字段名称, i 为某列
方法2:
var i:integer;
s:TStringList ;
begin
s:=tstringlist.Create ;
for i := 0 to dbgridEh2.Columns.Count -1 do
s.Add ( DBGridEh2.Fields[i].FullName );
end;
2. 获取某字段的所有行内容
DBGridEh2.DataSource.DataSet.First ;
repeat
Result.Add(DBGridEh2.DataSource.DataSet.FieldByName('CourseName').Value );
until (DBGridEh2.DataSource.DataSet.FindNext =False);
-----------
对应SQL语句: Select CourseName from 表名
3. DBGridEh动态增加列
DBGridEh2.Columns.Add; //先执行增加,再添加其它属性.
DBGridEh2.Columns[0].FieldName :='id';
DBGridEh2.Columns[0].Width :=30;
DBGridEh2.Columns[0].Title.Alignment :=taCenter;
DBGridEh2.Columns.add;
DBGridEh2.Columns[1].FieldName :='CourseName';
DBGridEh2.Columns[1].Width :=100;
DBGridEh2.Columns[1].Title.Alignment :=taCenter;
DBGridEh2.Columns.add;
DBGridEh2.Columns[2].FieldName :='Memo';
DBGridEh2.Columns[2].Width :=60;
DBGridEh2.Columns[2].Title.Alignment :=taCenter;
4. delphi 如何删除dbgrideh中选中的行 2014-11-20 15:49:57
方法1: DBGridEh2.DataSource.DataSet.Delete ;
那要看dbgrid中数据来自什么控件:如果是 table: table控件名.delete;
如果是 query系列的控件: 让query控件执行如下语句: delete from 表名 where 条件
方法2:
DBGridEh2.OptionsEh.dghShowRecNo:=True; //显示最左边行标号
DBGridEh2.Options.dgRowSelect:=True; // 允许选中单行,设置该参数后,表格内容无法编辑,可以只设下面多选参数.
DBGridEh2.Options.dgMultiSelect:=True; //允许多选
DBGridEh2.SelectedRows.Delete ;
该方法选中行是要点击行标号,取消是按ESC键.
5.dbgrideh中允许选择多行
在dbgrideh中允许选择多行,如何知道哪些行被选中?是个BOOKMARK类型的属性。
SelectedRows: TBookmarkList
procedure TForm1.Button1Click(Sender: TObject);
var
i, j: Integer;
s: string;
begin
if DBGrid1.SelectedRows.Count>0 then
with DBGrid1.DataSource.DataSet do
for i:=0 to DBGrid1.SelectedRows.Count-1 do
begin
GotoBookmark(pointer(DBGrid1.SelectedRows.Items[i]));
for j := 0 to FieldCount-1 do
begin
if (j>0) then s:=s+', ';
s:=s+Fields[j].AsString;
end;
Listbox1.Items.Add(s);
s:= '';
end;
end;
2003-12-10 15:06:19 dbgrideh 上选择多行之后,要求把所选择的记录全部添加入SQL数据库的一个固定表中。dbgrideh.selectedrows记载了所有被选择行的bookmark;利用SelectedRows和GotoBookmark完成。代码如下:
procedure TForm1.Button1Click(Sender: TObject);
var
i, j: Integer;
s: string;
begin
if DBGrid1.SelectedRows.Count>0 then
with DBGrid1.DataSource.DataSet do
for i:=0 to DBGrid1.SelectedRows.Count-1 do
begin
GotoBookmark(pointer(DBGrid1.SelectedRows.Items[i]));
for j := 0 to FieldCount-1 do
begin
if (j>0) then s:=s+', ';
s:=s+Fields[j].AsString;
end;
Listbox1.Items.Add(s);
s:= '';
end;
end;