问题描述
- C++builder DBGrid双击获取记录问题
无论我双击001还是其他记录,该ADODataSet1->FieldByName(""员工号"")->AsString显示的就是001的值,不曾改变。代码如下
AnsiString Path;
AnsiString sqlstr = ""Select 头像 from Photo where 员工号='""+ADODataSet1->FieldByName(""员工号"")->AsString+""'"";
ADOQuery1->Close();
ADOQuery1->SQL->Clear();
ADOQuery1->SQL->Add(sqlstr);
ADOQuery1->Open();
Path = ADOQuery1->FieldByName(""头像"")->AsString;
Image1->Picture->LoadFromFile(Path);
解决方案
这个要看你的代码是写在什么地方的了,试试下面的:
void __fastcall TForm1::DBGrid1DblClick(TObject Sender)//双击
{
if(((TDBGrid)Sender)->DataSource->DataSet->Active==false || ((TDBGrid*)Sender)->DataSource->DataSet->RecordCount<=0)return;
AnsiString Path;
AnsiString sqlstr = ""Select 头像 from Photo where 员工号='""+((TDBGrid*)Sender)->DataSource->DataSet->FieldByName(""员工号"")->AsString+""'"";
ADOQuery1->Close();
ADOQuery1->SQL->Clear();
ADOQuery1->SQL->Add(sqlstr);
ADOQuery1->Open();
Path = ADOQuery1->FieldByName(""头像"")->AsString;
Image1->Picture->LoadFromFile(Path);
}
还有一点,你的员工号是文本类型的还是数字类型的,数字类型的话不要加''
解决方案二:
参考:http://download.csdn.net/detail/zhujianqiangqq/2325441
解决方案三:
这个要看你的代码是写在什么地方的了,试试下面的:
void __fastcall TForm1::DBGrid1DblClick(TObject Sender)//双击
{
if(((TDBGrid)Sender)->DataSource->DataSet->Active==false || ((TDBGrid*)Sender)->DataSource->DataSet->RecordCount<=0)return;
AnsiString Path;
AnsiString sqlstr = ""Select 头像 from Photo where 员工号='""+((TDBGrid*)Sender)->DataSource->DataSet->FieldByName(""员工号"")->AsString+""'"";
ADOQuery1->Close();
ADOQuery1->SQL->Clear();
ADOQuery1->SQL->Add(sqlstr);
ADOQuery1->Open();
Path = ADOQuery1->FieldByName(""头像"")->AsString;
Image1->Picture->LoadFromFile(Path);
}
还有一点,你的员工号是文本类型的还是数字类型的,数字类型的话不要加''