DBGrid的使用学习

 

DBGrid使用学习:

Grid的属性设置:

Options: dgRowSelect设为true:即为:整行选中

ReadOnly 设为true

DBGrid的一些重要属性:

 

DBGrid控件进行初始化“

    if(this->GridSafety->DataSource->DataSet!=NULL)

    {

        this->GridSafety->DataSource->DataSet->Active = false;

    }

this->GridSafety->DataSource->DataSet = NULL;

//通过修改数据源的数据集

 

判断DBGrid中是否有数据值

    

void __fastcall Tfrm_Insure::popmnuPayPopup(TObject *Sender)

{

if(Grid1>DataSource->DataSet!=NULL&&Grid1->DataSource->DataSet->RecordCount>0)

     {

        this->N3->Enabled = true;//设置此子菜单为可用状态

     }

     else

     this->N3->Enabled = false;

}

注意:Popup属性为PopupMenu(弹出式菜单)的一个属性,当鼠标击时响应此事件

 

//DBGrid数据库相关操作

//定义一个函数对数据进行保存

//动态生成TADOCommand 对表GetPay中的数据进行保存或修改

//定义一个指针ptGetPayInfo

//单击“添加“按钮时,触发此函数

//通过定义一个Struct结构来传递参数

bool __fastcall Tfrm_Insure::SaveGetPayInfo(TADOCommand *cmd, GetPayInfo *ptGetPayInfo, int flg)

{

    //TODO: Add your source code here

    bool ret = true;

    AnsiString sql = "";

    if(flg == 1)

    {

        sql = "INSERT INTO P_GetPay (DepID,CarID,BeginTime,PayTime,PayItem,PayMoney,Remark)VALUES (:DepID,:CarID,:BeginTime,:PayTime,:PayItem,:PayMoney,:Remark)";

    }

    else

    {

        sql = "UPDATE P_GetPay SET PayMoney=:PayMoney,Remark=:Remark WHERE DepId=:DepId AND CarId=:CarId AND BeginTime=:BeginTime AND PayItem=:PayItem AND PayTime=:PayTime";

    }

    cmd->Parameters->Clear();

    cmd->CommandText = sql;

    cmd->Parameters->ParamByName("DepID")->Value     = ptGetPayInfo->DepID;

    cmd->Parameters->ParamByName("CarID")->Value     = ptGetPayInfo->CarId;

    cmd->Parameters->ParamByName("BeginTime")->Value = FormatDateTime("yyyy-mm-dd",ptGetPayInfo->BeginTime);

    cmd->Parameters->ParamByName("PayTime")->Value   = FormatDateTime("yyyy-mm-dd",ptGetPayInfo->PayTime);

    cmd->Parameters->ParamByName("PayItem")->Value   = ptGetPayInfo->PayItem;

    cmd->Parameters->ParamByName("PayMoney")->Value  = ptGetPayInfo->PayMoney;

    cmd->Parameters->ParamByName("Remark")->Value    = ptGetPayInfo->Remark;

    try

    {

        cmd->Execute();

    }

    catch(...)

    {

        ret = false;

    }

    return ret;

}

 

//当单击“添加“修改按钮时,触发上述函数

int flg = 0;

    AnsiString sql = "SELECT PayItem FROM P_GetPay WHERE DepId = '"+PartId+"' AND CarId = "+CarId+" "

                    " AND PayItem = '"+ADOQuerySafety->FieldByName("SafeItem")->AsString+"' AND PayTime =#"+FormatDateTime("yyyy-mm-dd",txtPayTime->Date)+"#" ;

if(g_Global.GetRecordCount(sql)<1)

//通过定义的一个全局函数来判断此数据是否存在

//若存在则修改,否则添加

           flg=1;

    else flg=2;

        pGetPayInfo.DepID = this->PartId;

        pGetPayInfo.CarId = this->CarId;

        pGetPayInfo.PayItem   = ADOQuerySafety->FieldByName("SafeItem")->AsString;

       // pGetPayInfo.BeginTime = GridSafety->DataSource->DataSet->FieldByName("BeginTime")->AsString;

        pGetPayInfo.BeginTime = ADOQuerySafety->FieldByName("BeginTime")->AsString;

        pGetPayInfo.PayMoney  = this->txtPayMoney->Text.ToDouble();

        pGetPayInfo.PayTime   = this->txtPayTime->Date;

        pGetPayInfo.Remark    = this->txtPayRemark->Text;

 

    TADOCommand *cmd = new TADOCommand(NULL);

    cmd->Connection = DataModule1->Connection;

    DataModule1->Connection->BeginTrans();

 

    try

    {

        if(this->SaveGetPayInfo(cmd,&pGetPayInfo,flg) == true)

        {

            DataModule1->Connection->CommitTrans();

            MessageBox(this->Handle,"保存成功" ,"提示",MB_OK|MB_ICONINFORMATION);

        }

        else

        {

            DataModule1->Connection->RollbackTrans();

            MessageBox(this->Handle,"保存失败" ,"提示",MB_OK|MB_ICONERROR);

        }

    }

    catch(...)

    {}

    delete cmd;

 

//在这里用到了动态创建TADOCmmand

动态创建TADOCommand

    TADOCommand *cmd = new TADOCommand(NULL);

    cmd->Connection = DataModule1->Connection;

DataModule1->Connection->BeginTrans();

    try

    {

if(this->SaveGetPayInfo(cmd,&pGetPayInfo,flg) == true)

    

DataModule1->Connection->CommitTrans();

MessageBox(this->Handle,"sssss_ICONINFORMATION);

……………………

…………………….

 

pGetPayInfo为一个struct结构对象

即:struct GetPayInfo

{

    AnsiString DepID;

    AnsiString CarId;

    AnsiString BeginTime;

    AnsiString PayTime;

    AnsiString PayItem;

    float PayMoney;

    AnsiString Remark;

};

在此,通过一个定义好的struct结构来进行传递参数。

 

DBGrid中显示数据

//查询数据并在DBGrid 中显示

void __fastcall Tfrm_Insure::QuerySafetyInfo()

{

    AnsiString cmd = " SELECT DepId,CarId,SafeItem,ItemMoney,BeginTime,EndTime,IsBack,Remark FROM P_Safety WHERE DepId =:DepId AND CarId =:CarId ";

    try

    {  

        ADOQuerySafety->Connection = DataModule1->Connection;

        ADOQuerySafety->Active = false;

        ADOQuerySafety->SQL->Clear();

        ADOQuerySafety->SQL->Add(cmd);

        ADOQuerySafety->Parameters->ParamByName("DepID")->Value = this->PartId;

        ADOQuerySafety->Parameters->ParamByName("CarID")->Value = this->CarId;

        ADOQuerySafety->Active = true;

        if (ADOQuerySafety->RecordCount > 0)

        {

            GridSafety->DataSource->DataSet = ADOQuerySafety;

            GridSafety->Columns->Items[0]->FieldName ="SafeItem";

            GridSafety->Columns->Items[1]->FieldName ="ItemMoney";

            GridSafety->Columns->Items[2]->FieldName ="BeginTime";

            GridSafety->Columns->Items[3]->FieldName ="EndTime";

            GridSafety->Columns->Items[4]->FieldName ="IsBack";

            GridSafety->Columns->Items[5]->FieldName ="Remark";

        }else

        {

            Application->MessageBoxA("´Ë³µÁ¾ÔÝʱÎÞ±£ÏÕÐÅÏ¢!","Ìáʾ",MB_OK|MB_ICONERROR);

            this->cmbSafeItem->SetFocus();

            this->btnAdd->Caption = "Ìí¼Ó";

            this->btnAdd->Enabled = true;

        }

    }

    catch(...){

    }

 

}

DBGrid中的一条数据

GridSafety->DataSource->DataSet->FieldByName("SafeItem")->AsString;

posted on 2005-08-30 16:55  痛苦并快乐着  阅读(836)  评论(0编辑  收藏  举报

导航