今天搞定的一个小例子程序

参考 http://community.csdn.net/Expert/topic/4892/4892997.xml?temp=5.430239E-02http://community.csdn.net/Expert/topic/4889/4889784.xml?temp=4.424685E-02

#ifndef Unit1H
#define Unit1H
//------------------------------------- --------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ADODB.hpp>
#include <Buttons.hpp>
#include <DB.hpp>
#include <DBGrids.hpp>
#include <Grids.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
        TComboBox *ComboBox1;
        TDBGrid *DBGrid1;
        TButton *Button1;
        TLabel *Label1;
        TDataSource *DS;
        TADOConnection *ADOConnection1;
        TADOQuery *ADOQuery1;
        TBitBtn *BitBtn1;
        TBitBtn *BitBtn2;
        TBitBtn *BitBtn3;
        TBitBtn *BitBtn4;
        TBitBtn *BitBtn5;
        TEdit *Edit1;
        void __fastcall BitBtn5Click(TObject *Sender);
        void __fastcall Button1Click(TObject *Sender);
        void __fastcall ComboBox1Change(TObject *Sender);
        void __fastcall ComboBox1Click(TObject *Sender);
        void __fastcall BitBtn1Click(TObject *Sender);
        void __fastcall BitBtn2Click(TObject *Sender);
        void __fastcall BitBtn3Click(TObject *Sender);
        void __fastcall BitBtn4Click(TObject *Sender);
        void __fastcall FormShow(TObject *Sender);
        void __fastcall Edit1KeyPress(TObject *Sender, char &Key);
private: // User declarations
public:  // User declarations
        __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
String Selstr,s_id,s_pwd;
int ItemCount,Count;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{

}
//---------------------------------------------------------------------------

void __fastcall TForm1::BitBtn5Click(TObject *Sender)
{
  Close();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
 // TLocateOptions Opts;
  AnsiString Sqlstr;

  if (Trim(Edit1->Text)=="" )
  {
      Application->MessageBox("输入要查的型号","提示!",0);
  }
  else
   {
    if (Selstr=="色带")
     {
      Sqlstr= "select * from " +Selstr + " where 商品名称 like '%"+Trim(Edit1->Text)+"%'"+" OR 通用于(架) like '%"+Trim(Edit1->Text)+"%'"+" OR 通用于(芯) like '%"+Trim(Edit1->Text)+"%'";
      ADOQuery1->Close();
      ADOQuery1->SQL->Clear();
      ADOQuery1->SQL->Add(Sqlstr);
      ADOQuery1->Open();

      for (int j = 0; j < DBGrid1->DataSource->DataSet->FieldCount; j++)
      {
         if ((j==1)||(j==2))
             DBGrid1->DataSource->DataSet->Fields->Fields[j]->DisplayWidth=20; //这条用于控制DBGrid列宽。
           else
             DBGrid1->DataSource->DataSet->Fields->Fields[j]->DisplayWidth=40;
      }
    
     }

    if (Selstr=="墨盒")
     {

      Sqlstr= "select * from " +Selstr + " where 墨盒编号 like '%"+Trim(Edit1->Text)+"%'"+" OR 适用机型 like '%"+Trim(Edit1->Text)+"%'";
      ADOQuery1->Close();
      ADOQuery1->SQL->Clear();
      ADOQuery1->SQL->Add(Sqlstr);
      ADOQuery1->Open();

      for (int j = 0; j < DBGrid1->DataSource->DataSet->FieldCount; j++)
      {
         if ((j==0)||(j==2)||j==3)
             DBGrid1->DataSource->DataSet->Fields->Fields[j]->DisplayWidth=20;
           else
            DBGrid1->DataSource->DataSet->Fields->Fields[j]->DisplayWidth=40;
      }

     }

    if (Selstr=="墨水")
     {
      //Opts.Clear();
      //Opts << loPartialKey;
      //ADOQuery1->Locate("墨水型号", Trim(Edit1->Text), Opts);

      Sqlstr= "select * from " +Selstr + " where 墨水型号 like '%"+Trim(Edit1->Text)+"%'"+" OR 适用机型 like '%"+Trim(Edit1->Text)+"%'";
      ADOQuery1->Close();
      ADOQuery1->SQL->Clear();
      ADOQuery1->SQL->Add(Sqlstr);
      ADOQuery1->Open();

      for (int j = 0; j < DBGrid1->DataSource->DataSet->FieldCount; j++)
      {
         if ((j==0)||(j==4))
             DBGrid1->DataSource->DataSet->Fields->Fields[j]->DisplayWidth=14;
           else
            DBGrid1->DataSource->DataSet->Fields->Fields[j]->DisplayWidth=30;
      }
     }

    if (Selstr=="碳粉")
     {

      Sqlstr= "select * from " +Selstr + " where 适用硒鼓 like '%"+Trim(Edit1->Text)+"%'"+" OR 适用机型 like '%"+Trim(Edit1->Text)+"%'";
      ADOQuery1->Close();
      ADOQuery1->SQL->Clear();
      ADOQuery1->SQL->Add(Sqlstr);
      ADOQuery1->Open();

      for (int j = 0; j < DBGrid1->DataSource->DataSet->FieldCount; j++)
      {
         if ((j==0)||(j==1)||j==2)
             DBGrid1->DataSource->DataSet->Fields->Fields[j]->DisplayWidth=14;  
           else
            DBGrid1->DataSource->DataSet->Fields->Fields[j]->DisplayWidth=40;
      }

     }

     if (Selstr=="硒鼓")
     {

      Sqlstr= "select * from " +Selstr + " where 德宝产品型号 like '%"+Trim(Edit1->Text)+"%'"+" OR 适用机型 like '%"+Trim(Edit1->Text)+"%'";
      ADOQuery1->Close();
      ADOQuery1->SQL->Clear();
      ADOQuery1->SQL->Add(Sqlstr);
      ADOQuery1->Open();

      for (int j = 0; j < DBGrid1->DataSource->DataSet->FieldCount; j++)
      {
         if ((j==0)||(j==1))
             DBGrid1->DataSource->DataSet->Fields->Fields[j]->DisplayWidth=20;
           else
            DBGrid1->DataSource->DataSet->Fields->Fields[j]->DisplayWidth=40;
      }

     }


   }

}
//---------------------------------------------------------------------------

void __fastcall TForm1::ComboBox1Change(TObject *Sender)
{
    AnsiString Sqlstr;
    //s_id=ComboBox1->Text;
    ItemCount=Form1->ComboBox1->ItemIndex;

  Selstr= Form1->ComboBox1->Items->Strings[ItemCount];
  if (ComboBox1->Text=="" || Selstr=="")
  {
      Application->MessageBox("没有选择品名 ","出错了!",0);
      Form1->ComboBox1->SetFocus();
      return;
   }
   else
     Sqlstr= "select * from " +Selstr;

    ADOQuery1->Close();
    ADOQuery1->SQL->Clear();
    ADOQuery1->SQL->Add(Sqlstr);
    ADOQuery1->Open();

    if (Selstr=="色带")
    {
     for (int j = 0; j < DBGrid1->DataSource->DataSet->FieldCount; j++)
      {
         if ((j==1)||(j==2))
             DBGrid1->DataSource->DataSet->Fields->Fields[j]->DisplayWidth=20;
           else
             DBGrid1->DataSource->DataSet->Fields->Fields[j]->DisplayWidth=40;
      }
    }
    if (Selstr=="墨盒")
    {
     for (int j = 0; j < DBGrid1->DataSource->DataSet->FieldCount; j++)
      {
         if ((j==0)||(j==2)||j==3)
             DBGrid1->DataSource->DataSet->Fields->Fields[j]->DisplayWidth=20;
           else
            DBGrid1->DataSource->DataSet->Fields->Fields[j]->DisplayWidth=40;
      }
     }

    if (Selstr=="墨水")
    {
     for (int j = 0; j < DBGrid1->DataSource->DataSet->FieldCount; j++)
      {
         if ((j==0)||(j==4))
             DBGrid1->DataSource->DataSet->Fields->Fields[j]->DisplayWidth=14;
           else
            DBGrid1->DataSource->DataSet->Fields->Fields[j]->DisplayWidth=30;
      }

    }

    if (Selstr=="碳粉")
    {
     for (int j = 0; j < DBGrid1->DataSource->DataSet->FieldCount; j++)
      {
         if ((j==0)||(j==1)||j==2)
             DBGrid1->DataSource->DataSet->Fields->Fields[j]->DisplayWidth=14;
           else
            DBGrid1->DataSource->DataSet->Fields->Fields[j]->DisplayWidth=40;
      }

    }

    if (Selstr=="硒鼓")
    {
     for (int j = 0; j < DBGrid1->DataSource->DataSet->FieldCount; j++)
      {
         if ((j==0)||(j==1))
             DBGrid1->DataSource->DataSet->Fields->Fields[j]->DisplayWidth=20;
           else
            DBGrid1->DataSource->DataSet->Fields->Fields[j]->DisplayWidth=40;
      }

    }

   
    //DS->DataSet=ADOQuery1;
   // DBGrid1->DataSource=DS;


}
//---------------------------------------------------------------------------
void __fastcall TForm1::ComboBox1Click(TObject *Sender)
{
     Selstr=ComboBox1->Text;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
    if (ADOQuery1->Bof)
        Application->MessageBox("到头了。 ","提示!",0);
    else
        ADOQuery1->Prior();

}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn2Click(TObject *Sender)
{
    if (ADOQuery1->Eof)
        Application->MessageBox("到尾了。 ","提示!",0);
    else
        ADOQuery1->Next();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn3Click(TObject *Sender)
{
       if (ADOQuery1->Bof==false)
           ADOQuery1->First();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn4Click(TObject *Sender)
{
       if (ADOQuery1->Eof==false)
        ADOQuery1->Last();
}
//---------------------------------------------------------------------------


void __fastcall TForm1::FormShow(TObject *Sender)
{

      Selstr="色带";
      ADOQuery1->Close();
      ADOQuery1->SQL->Clear();
      ADOQuery1->SQL->Add("select * from 色带");
      ADOQuery1->Open();
    //  DS->DataSet=ADOQuery1;
   //   DBGrid1->DataSource=DS;
      for (int j = 0; j < DBGrid1->DataSource->DataSet->FieldCount; j++)
      {
         if ((j==1)||(j==2))
             DBGrid1->DataSource->DataSet->Fields->Fields[j]->DisplayWidth=20;
           else
             DBGrid1->DataSource->DataSet->Fields->Fields[j]->DisplayWidth=40;
      }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Edit1KeyPress(TObject *Sender, char &Key)
{
    if (Key ==Char(13))                                           //在Edit1中按下回车键后,
      Form1->Button1->SetFocus();
}
//---------------------------------------------------------------------------

 

  • 0
    点赞
  • 0
    收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
评论

打赏作者

chpst

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值