C#调用水晶报表实例

 

using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Data;
using  System.Drawing;
using  System.Text;
using  System.Windows.Forms;
using  System.Collections;
using  System.IO;
using  System.Runtime.InteropServices;
using  System.Net;
using  System.Data.SqlClient;
using  CrystalDecisions.Shared;
using  CrystalDecisions.CrystalReports.Engine;

namespace  CrystalReport
ExpandedBlockStart.gifContractedBlock.gif
{
    
public partial class Main : Form
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
public Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            InitializeComponent();
        }


        
//当前选中的报表路径
        public string Path_Str = "";

        
//TabPage序列
        public int FI = 0;

        
//RadioButton序列
        public int FISUB = 0;

        
//RadioButton宽度
        public int FWidth = 0;

        
//RadioButton高度
        public int FHeight = 0;

        
//RadioButton行数
        public int FLines = 0;

        
//当前应用程序执行路径
        public string FExePath = "";

        
//IP地址
        public string FIP;

        
//连接字符串
        public string FConStr;

        
//主机名Hostname
        public string FHostname;

        
//数据库Dbname
        public string FDbname;

        
//username
        public string FUsername;

        
//password
        public string FPassword;

        
//创建TabPage 
        public TabPage[] mTabPage = new TabPage[500];

        
//创建GroupBox  
        public GroupBox[] mGroupBox = new GroupBox[500];

        
//创建RadioButton
        public RadioButton[] mRadioButton = new RadioButton[500];


        
public void CreateAll(DirectoryInfo source)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
//判断rpt路径是否存在            
                if (Directory.Exists(source.FullName) == false)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    MessageBox.Show(source.FullName 
+ " 不存在""提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    Application.Exit();
                }


                
//遍历目录
                foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
//MessageBox.Show(diSourceSubDir.Name, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                    
//实例TabPage                
                    mTabPage[FI] = new TabPage();
                    mTabPage[FI].Parent 
= tabControl1;
                    mTabPage[FI].Text 
= diSourceSubDir.Name;
                    mTabPage[FI].TabIndex 
= FI;

                    
//实例GroupBox      
                    mGroupBox[FI] = new GroupBox();
                    mGroupBox[FI].Parent 
= mTabPage[FI];
                    mGroupBox[FI].Dock 
= DockStyle.Fill;
                    mGroupBox[FI].Text 
= "选择报表";

                    
//行次
                    int MI = 0;

                    
int x = mGroupBox[FI].Location.X;
                    
int y = mGroupBox[FI].Location.Y;

                    
//遍历文件
                    foreach (FileInfo fi in diSourceSubDir.GetFiles())
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        
//换列显示
                        if (MI % FLines == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
{
                            
if (MI != 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
{
                                x 
= x + FWidth;
                                y 
= 0;
                                MI 
= 0;
                            }

                        }


                        
//实例RadioButton                 
                        mRadioButton[FISUB] = new RadioButton();
                        mRadioButton[FISUB].Parent 
= mGroupBox[FI];
                        mRadioButton[FISUB].Location 
= new System.Drawing.Point(x + 25, y + 15 + MI * FHeight);
                        mRadioButton[FISUB].Text 
= fi.Name;
                        mRadioButton[FISUB].AutoSize 
= true;

                        
//默认选择第一个
                        if (mRadioButton[FISUB].Location.Y == 15)
                            
if (mRadioButton[FISUB].Location.X == 25)
                                mRadioButton[FISUB].Checked 
= true;

                        
//行次累加
                        MI = MI + 1;

                        
//当前的RadioButton总Id累加
                        FISUB = FISUB + 1;
                    }


                    
//当前的TabPage总Id累加
                    FI = FI + 1;

                    
//递归
                    CreateAll(diSourceSubDir);
                }

            }

            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{

            }

        }


        
//调用kernel32.dll读写ini文件信息
        [DllImport("kernel32")]
        
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
        [DllImport(
"kernel32")]
        
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        
//写ini
        public void IniWriteValue(string Section, string Key, string Value, string filepath)//对ini文件进行写操作的函数
ExpandedSubBlockStart.gifContractedSubBlock.gif
        {
            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                WritePrivateProfileString(Section, Key, Value, filepath);
            }

            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{

            }

        }


        
//读ini
        public string IniReadValue(string Section, string Key, string filepath)             //对ini文件进行读操作的函数
ExpandedSubBlockStart.gifContractedSubBlock.gif
        {
            StringBuilder temp 
= new StringBuilder(255);
            
int i = GetPrivateProfileString(Section, Key, "无法读取对应的值", temp, 255, filepath);
            
return temp.ToString();
        }


        
//字符串转换为整型
        public static int IntParse(string objValue, int defaultValue)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
int returnValue = defaultValue;
            
if (!string.IsNullOrEmpty(objValue))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    returnValue 
= int.Parse(objValue);
                }

                
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    returnValue 
= defaultValue;
                }

            }


            
return returnValue;
        }


        
//reportDocument conn to DB
        private void SetDBLogonForReport(ConnectionInfo connectionInfo, ReportDocument reportDocument)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                Tables tables 
= reportDocument.Database.Tables;
                
foreach (CrystalDecisions.CrystalReports.Engine.Table table in tables)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    TableLogOnInfo tableLogonInfo 
= table.LogOnInfo;
                    tableLogonInfo.ConnectionInfo 
= connectionInfo;
                    table.ApplyLogOnInfo(tableLogonInfo);

                }

            }

            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{

            }

        }


        
private void Main_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
//取得当前路径
                FExePath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).Replace("file:\\""").Replace("\\"@"\");

                
//从ini读取RPT存放路径          
                Path_Str = IniReadValue("PathInfo""Path", FExePath + "\\CrystalReport.ini");

                
//DBInfo
                FHostname = IniReadValue("DBInfo""Host", FExePath + "\\CrystalReport.ini");
                FDbname 
= IniReadValue("DBInfo""Dbname", FExePath + "\\CrystalReport.ini");
                FUsername 
= IniReadValue("DBInfo""User", FExePath + "\\CrystalReport.ini");
                FPassword 
= IniReadValue("DBInfo""Pass", FExePath + "\\CrystalReport.ini");

                
//从ini读取RPT显示信息
                FWidth = IntParse(IniReadValue("ViewInfo""Width", FExePath + "\\CrystalReport.ini"), 160);
                FHeight 
= IntParse(IniReadValue("ViewInfo""Height", FExePath + "\\CrystalReport.ini"), 25);
                FLines 
= IntParse(IniReadValue("ViewInfo""Lines", FExePath + "\\CrystalReport.ini"), 8);

                
//IP地址
                IPAddress[] ips = Dns.GetHostAddresses(Dns.GetHostName());
                FIP 
= ips[0].ToString();

                
//连接字符串
                FConStr = @"Data Source=" + FHostname + ";Database=" + FDbname + ";User ID=" + FUsername + ";Password=" + FPassword;
            }

            
catch (Exception)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                MessageBox.Show(
"CrystalReport.ini配置错误""提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                Application.Exit();
            }

        }


        
private void Main_Shown(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
//判断rpt路径是否存在            
                if (Directory.Exists(Path_Str) == false)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    MessageBox.Show(Path_Str 
+ " 不存在""提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    Application.Exit();
                }


                
//窗体定位
                Main.ActiveForm.Width = IntParse(IniReadValue("FormInfo""Width", FExePath + "\\CrystalReport.ini"), 540); ;
                Main.ActiveForm.Height 
= IntParse(IniReadValue("FormInfo""Height", FExePath + "\\CrystalReport.ini"), 310);
                Main.ActiveForm.Left 
= IntParse(IniReadValue("FormInfo""Left", FExePath + "\\CrystalReport.ini"), 310);
                Main.ActiveForm.Top 
= IntParse(IniReadValue("FormInfo""Top", FExePath + "\\CrystalReport.ini"), 310);

                
//delpage1
                tabControl1.TabPages.Clear();

                
//遍历RPT路径并动态新增页签及radio报表名称       
                DirectoryInfo diSource = new DirectoryInfo(Path_Str);
                CreateAll(diSource);

                
//addpage1
                tabControl1.Controls.Add(mtabPageX);

                
//条件一 --------------------------------------------------------------------------
                cbo10.SelectedIndex = 0;

                DataSet DataSet1 
= new DataSet();
                SqlDataAdapter SqlDataAdapter1 
= new SqlDataAdapter();
                SqlCommand sqlSelectCommand1 
= new SqlCommand();
                SqlConnection sqlConnection1 
= new SqlConnection();

                
//Set the variables.
                sqlSelectCommand1.CommandText = @"SELECT MA001,RTRIM(MA001)+'-'+RTRIM(MA002) AS MA001C FROM COPMA ORDER BY MA001";
                sqlSelectCommand1.Connection 
= sqlConnection1;
                sqlConnection1.ConnectionString 
= FConStr;
                SqlDataAdapter1.SelectCommand 
= sqlSelectCommand1;

                
// Fill the data set with the result of the query.
                
// The results are stored in the data set called "Table1".
                SqlDataAdapter1.Fill(DataSet1, "Table1");
                SqlDataAdapter1.Fill(DataSet1, 
"Table2");

                
//绑定
                cbo11.DataSource = DataSet1.Tables["Table1"];
                cbo11.DisplayMember 
= "MA001C";
                cbo11.ValueMember 
= "MA001";
                cbo12.DataSource 
= DataSet1.Tables["Table2"];
                cbo12.DisplayMember 
= "MA001C";
                cbo12.ValueMember 
= "MA001";

                
//条件二 --------------------------------------------------------------------------
                cbo13.SelectedIndex = 0;

                DataSet DataSet2 
= new DataSet();
                SqlDataAdapter SqlDataAdapter2 
= new SqlDataAdapter();
                SqlCommand sqlSelectCommand2 
= new SqlCommand();
                SqlConnection sqlConnection2 
= new SqlConnection();

                
//Set the variables.
                sqlSelectCommand2.CommandText = @"SELECT MA001,RTRIM(MA001)+'-'+RTRIM(MA002) AS MA001C FROM COPMA ORDER BY MA001";
                sqlSelectCommand2.Connection 
= sqlConnection2;
                sqlConnection2.ConnectionString 
= FConStr;
                SqlDataAdapter2.SelectCommand 
= sqlSelectCommand2;

                
// Fill the data set with the result of the query.
                
// The results are stored in the data set called "Table1".
                SqlDataAdapter2.Fill(DataSet2, "Table1");
                SqlDataAdapter2.Fill(DataSet2, 
"Table2");

                
//绑定
                cbo14.DataSource = DataSet2.Tables["Table1"];
                cbo14.DisplayMember 
= "MA001C";
                cbo14.ValueMember 
= "MA001";
                cbo15.DataSource 
= DataSet2.Tables["Table2"];
                cbo15.DisplayMember 
= "MA001C";
                cbo15.ValueMember 
= "MA001";

                
//依据 --------------------------------------------------------------------------
                cbo07.SelectedIndex = 0;

                DataSet DataSet3 
= new DataSet();
                SqlDataAdapter SqlDataAdapter3 
= new SqlDataAdapter();
                SqlCommand sqlSelectCommand3 
= new SqlCommand();
                SqlConnection sqlConnection3 
= new SqlConnection();

                
//Set the variables.
                sqlSelectCommand3.CommandText = @"SELECT MA001,RTRIM(MA001)+'-'+RTRIM(MA002) AS MA001C FROM COPMA ORDER BY MA001";
                sqlSelectCommand3.Connection 
= sqlConnection3;
                sqlConnection3.ConnectionString 
= FConStr;
                SqlDataAdapter3.SelectCommand 
= sqlSelectCommand3;

                
// Fill the data set with the result of the query.
                
// The results are stored in the data set called "Table1".
                SqlDataAdapter3.Fill(DataSet3, "Table1");
                SqlDataAdapter3.Fill(DataSet3, 
"Table2");

                
//绑定
                cbo08.DataSource = DataSet3.Tables["Table1"];
                cbo08.DisplayMember 
= "MA001C";
                cbo08.ValueMember 
= "MA001";
                cbo09.DataSource 
= DataSet3.Tables["Table2"];
                cbo09.DisplayMember 
= "MA001C";
                cbo09.ValueMember 
= "MA001";

            }

            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{

            }

        }


        
private void button1_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
//获取当前页签及当前页签选中的报表
                string mClass = tabControl1.SelectedTab.Text;
                
string mRpt = "";
                
string mRTPPath = "";

                
if (mClass == "动态环比同比报表")
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
//执行存储过程
                    String mTYPE, mTIME1_BEGIN, mTIME1_END, mTIME2_BEGIN, mTIME2_END, mBASE;
                    
int mCBO07;


                    
if (rdo01.Checked == true)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        mTYPE 
= "1";
                    }

                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        mTYPE 
= "2";
                    }


                    mTIME1_BEGIN 
= ed03.Text;
                    mTIME1_END 
= ed04.Text;
                    mTIME2_BEGIN 
= ed05.Text;
                    mTIME2_END 
= ed06.Text;

                    mCBO07 
= cbo07.SelectedIndex + 1;
                    mBASE 
= mCBO07.ToString();

                    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        SqlConnection sqlConnection1 
= new SqlConnection(FConStr);
                        sqlConnection1.Open();

                        SqlCommand SqlCommand1 
= new SqlCommand();
                        SqlCommand1.CommandText 
= "SALE_SEARCH_TWO_TIMES";
                        SqlCommand1.CommandType 
= CommandType.StoredProcedure;
                        SqlCommand1.Connection 
= sqlConnection1;

                        SqlCommand1.Parameters.Add(
"@TYPE", SqlDbType.VarChar);
                        SqlCommand1.Parameters.Add(
"@TIME1_BEGIN", SqlDbType.VarChar);
                        SqlCommand1.Parameters.Add(
"@TIME1_END", SqlDbType.VarChar);
                        SqlCommand1.Parameters.Add(
"@TIME2_BEGIN", SqlDbType.VarChar);
                        SqlCommand1.Parameters.Add(
"@TIME2_END", SqlDbType.VarChar);
                        SqlCommand1.Parameters.Add(
"@WHERE1", SqlDbType.VarChar);
                        SqlCommand1.Parameters.Add(
"@WHERE1_BEGIN", SqlDbType.VarChar);
                        SqlCommand1.Parameters.Add(
"@WHERE1_END", SqlDbType.VarChar);
                        SqlCommand1.Parameters.Add(
"@WHERE2", SqlDbType.VarChar);
                        SqlCommand1.Parameters.Add(
"@WHERE2_BEGIN", SqlDbType.VarChar);
                        SqlCommand1.Parameters.Add(
"@WHERE2_END", SqlDbType.VarChar);
                        SqlCommand1.Parameters.Add(
"@BASE", SqlDbType.VarChar);
                        SqlCommand1.Parameters.Add(
"@IP", SqlDbType.VarChar);

                        SqlCommand1.Parameters[
0].Value = mTYPE;
                        SqlCommand1.Parameters[
1].Value = mTIME1_BEGIN;
                        SqlCommand1.Parameters[
2].Value = mTIME1_END;
                        SqlCommand1.Parameters[
3].Value = mTIME2_BEGIN;
                        SqlCommand1.Parameters[
4].Value = mTIME2_END;
                        SqlCommand1.Parameters[
5].Value = Convert.ToString(cbo10.SelectedIndex + 1);

                        
if (cbo11.SelectedValue == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
{
                            SqlCommand1.Parameters[
6].Value = "";
                        }

                        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
{
                            SqlCommand1.Parameters[
6].Value = cbo11.SelectedValue;
                        }


                        
if (cbo12.SelectedValue == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
{
                            SqlCommand1.Parameters[
7].Value = "";
                        }

                        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
{
                            SqlCommand1.Parameters[
7].Value = cbo12.SelectedValue;
                        }


                        SqlCommand1.Parameters[
8].Value = Convert.ToString(cbo13.SelectedIndex + 1);

                        
if (cbo14.SelectedValue == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
{
                            SqlCommand1.Parameters[
9].Value = "";
                        }

                        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
{
                            SqlCommand1.Parameters[
9].Value = cbo14.SelectedValue;
                        }


                        
if (cbo15.SelectedValue == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
{
                            SqlCommand1.Parameters[
10].Value = "";
                        }

                        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
{
                            SqlCommand1.Parameters[
10].Value = cbo15.SelectedValue;
                        }


                        SqlCommand1.Parameters[
11].Value = mBASE;
                        SqlCommand1.Parameters[
12].Value = FIP;

                        Int32 mRowsAffected 
= SqlCommand1.ExecuteNonQuery();
                        sqlConnection1.Close();
                    }

                    
catch (Exception)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        MessageBox.Show(
"SALE_SEARCH_TWO_TIMES执行失败""提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }

                }


                
//新建实例
                Report_View RPT = new Report_View();

                
//获得RPT文件路径
                if (mClass == "动态环比同比报表")
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    mRpt 
= "动态环比同比报表.rpt";

                    mRTPPath 
= Path_Str + "\\" + "动态环比同比报表.rpt";
                }

                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
for (int MI = 0; MI <= FISUB - 1; MI++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        
if (mRadioButton[MI].Parent.Parent.Text == mClass)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
{
                            
if (mRadioButton[MI].Checked == true)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
{
                                mRpt 
= mRadioButton[MI].Text;
                                
break;
                            }

                        }

                    }


                    mRTPPath 
= Path_Str + "\\" + mClass + "\\" + mRpt;
                }


                
//预览窗口显示
                RPT.Text = "预览_" + mRpt;

                RPT.Show();

                
//Load rpt
                RPT.RDT1.Load(mRTPPath);

                
//trans Param 
                if (mClass == "动态环比同比报表")
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    ParameterField paramField 
= new ParameterField();
                    ParameterFields paramFields 
= new ParameterFields();
                    ParameterRangeValue rangeVal 
= new ParameterRangeValue();
                    ParameterDiscreteValue discreteVal 
= new ParameterDiscreteValue();

                    
//1
                    paramField = new ParameterField();
                    paramField.ParameterFieldName 
= "依据编号";

                    
if (Convert.ToInt32(cbo08.SelectedValue.ToString()) < Convert.ToInt32(cbo09.SelectedValue.ToString()))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        rangeVal.StartValue 
= cbo08.SelectedValue;
                        rangeVal.EndValue 
= cbo09.SelectedValue;
                    }

                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        rangeVal.StartValue 
= cbo09.SelectedValue;
                        rangeVal.EndValue 
= cbo08.SelectedValue;
                    }


                    paramField.CurrentValues.Add(rangeVal);
                    paramFields.Add(paramField);

                    
//2
                    paramField = new ParameterField();
                    paramField.ParameterFieldName 
= "IP";
                    discreteVal.Value 
= FIP;
                    paramField.CurrentValues.Add(discreteVal);
                    paramFields.Add(paramField);

                    RPT.RPT1.ParameterFieldInfo 
= paramFields;
                }


                
//conn to DB
                ConnectionInfo ConnectionInfo1 = new ConnectionInfo();
                ConnectionInfo1.DatabaseName 
= FDbname;
                ConnectionInfo1.UserID 
= FUsername;
                ConnectionInfo1.Password 
= FPassword;
                ConnectionInfo1.ServerName 
= FHostname;
                SetDBLogonForReport(ConnectionInfo1, RPT.RDT1);

                
//Add to Report View
                RPT.RPT1.ReportSource = RPT.RDT1;
            }

            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{

            }

        }


        
private void cbo07_SelectedIndexChanged(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
//开始、结束两下拉框填值
                DataSet DataSet1 = new DataSet();
                SqlDataAdapter SqlDataAdapter1 
= new SqlDataAdapter();
                SqlCommand sqlSelectCommand1 
= new SqlCommand();
                SqlConnection sqlConnection1 
= new SqlConnection();

                
//Set the variables.
                if (cbo07.SelectedIndex == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    sqlSelectCommand1.CommandText 
= @"SELECT MA001 AS MA001,RTRIM(MA001)+'-'+RTRIM(MA002) AS MA001C FROM COPMA ORDER BY MA001";
                }

                
else if (cbo07.SelectedIndex == 1)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    sqlSelectCommand1.CommandText 
= @"SELECT MR002 AS MA001,RTRIM(MR002)+'-'+RTRIM(MR003) AS MA001C FROM CMSMR WHERE MR001='1' ORDER BY MR002";
                }

                
else if (cbo07.SelectedIndex == 2)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    sqlSelectCommand1.CommandText 
= @"SELECT MR002 AS MA001,RTRIM(MR002)+'-'+RTRIM(MR003) AS MA001C FROM CMSMR WHERE MR001='3' ORDER BY MR002";
                }

                
else if (cbo07.SelectedIndex == 3)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    sqlSelectCommand1.CommandText 
= @"SELECT MR002 AS MA001,RTRIM(MR002)+'-'+RTRIM(MR003) AS MA001C FROM CMSMR WHERE MR001='4' ORDER BY MR002";
                }

                
else if (cbo07.SelectedIndex == 4)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    sqlSelectCommand1.CommandText 
= @"SELECT ME001 AS MA001,RTRIM(ME001)+'-'+RTRIM(ME002) AS MA001C FROM CMSME ORDER BY ME001";
                }

                
else if (cbo07.SelectedIndex == 5)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    sqlSelectCommand1.CommandText 
= @"SELECT MV001 AS MA001,RTRIM(MV001)+'-'+RTRIM(MV002) AS MA001C FROM CMSMV ORDER BY MV001";
                }

                
else if (cbo07.SelectedIndex == 6)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    sqlSelectCommand1.CommandText 
= @"SELECT MV001 AS MA001,RTRIM(MV001)+'-'+RTRIM(MV002) AS MA001C FROM CMSMV ORDER BY MV001";
                }

                
else if (cbo07.SelectedIndex == 7)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    sqlSelectCommand1.CommandText 
= @"SELECT MB001 AS MA001,RTRIM(MB001)+'-'+RTRIM(MB002) AS MA001C FROM INVMB ORDER BY MB001";
                }


                sqlSelectCommand1.Connection 
= sqlConnection1;
                sqlConnection1.ConnectionString 
= FConStr;
                SqlDataAdapter1.SelectCommand 
= sqlSelectCommand1;

                
// Fill the data set with the result of the query.
                
// The results are stored in the data set called "Table1".
                SqlDataAdapter1.Fill(DataSet1, "Table1");
                SqlDataAdapter1.Fill(DataSet1, 
"Table2");

                
//绑定
                cbo08.DataSource = DataSet1.Tables["Table1"];
                cbo08.DisplayMember 
= "MA001C";
                cbo08.ValueMember 
= "MA001";
                cbo09.DataSource 
= DataSet1.Tables["Table2"];
                cbo09.DisplayMember 
= "MA001C";
                cbo09.ValueMember 
= "MA001";
            }

            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{

            }

        }


        
private void cbo10_SelectedIndexChanged(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
//开始、结束两下拉框填值
                if (cbo10.SelectedIndex != 8)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    DataSet DataSet1 
= new DataSet();
                    SqlDataAdapter SqlDataAdapter1 
= new SqlDataAdapter();
                    SqlCommand sqlSelectCommand1 
= new SqlCommand();
                    SqlConnection sqlConnection1 
= new SqlConnection();

                    
//Set the variables.
                    if (cbo10.SelectedIndex == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        sqlSelectCommand1.CommandText 
= @"SELECT MA001 AS MA001,RTRIM(MA001)+'-'+RTRIM(MA002) AS MA001C FROM COPMA ORDER BY MA001";
                    }

                    
else if (cbo10.SelectedIndex == 1)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        sqlSelectCommand1.CommandText 
= @"SELECT MR002 AS MA001,RTRIM(MR002)+'-'+RTRIM(MR003) AS MA001C FROM CMSMR WHERE MR001='1' ORDER BY MR002";
                    }

                    
else if (cbo10.SelectedIndex == 2)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        sqlSelectCommand1.CommandText 
= @"SELECT MR002 AS MA001,RTRIM(MR002)+'-'+RTRIM(MR003) AS MA001C FROM CMSMR WHERE MR001='3' ORDER BY MR002";
                    }

                    
else if (cbo10.SelectedIndex == 3)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        sqlSelectCommand1.CommandText 
= @"SELECT MR002 AS MA001,RTRIM(MR002)+'-'+RTRIM(MR003) AS MA001C FROM CMSMR WHERE MR001='4' ORDER BY MR002";
                    }

                    
else if (cbo10.SelectedIndex == 4)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        sqlSelectCommand1.CommandText 
= @"SELECT ME001 AS MA001,RTRIM(ME001)+'-'+RTRIM(ME002) AS MA001C FROM CMSME ORDER BY ME001";
                    }

                    
else if (cbo10.SelectedIndex == 5)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        sqlSelectCommand1.CommandText 
= @"SELECT MV001 AS MA001,RTRIM(MV001)+'-'+RTRIM(MV002) AS MA001C FROM CMSMV ORDER BY MV001";
                    }

                    
else if (cbo10.SelectedIndex == 6)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        sqlSelectCommand1.CommandText 
= @"SELECT MV001 AS MA001,RTRIM(MV001)+'-'+RTRIM(MV002) AS MA001C FROM CMSMV ORDER BY MV001";
                    }

                    
else if (cbo10.SelectedIndex == 7)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        sqlSelectCommand1.CommandText 
= @"SELECT MB001 AS MA001,RTRIM(MB001)+'-'+RTRIM(MB002) AS MA001C FROM INVMB ORDER BY MB001";
                    }


                    sqlSelectCommand1.Connection 
= sqlConnection1;
                    sqlConnection1.ConnectionString 
= FConStr;
                    SqlDataAdapter1.SelectCommand 
= sqlSelectCommand1;

                    
// Fill the data set with the result of the query.
                    
// The results are stored in the data set called "Table1".
                    SqlDataAdapter1.Fill(DataSet1, "Table1");
                    SqlDataAdapter1.Fill(DataSet1, 
"Table2");

                    
//绑定
                    cbo11.DataSource = DataSet1.Tables["Table1"];
                    cbo11.DisplayMember 
= "MA001C";
                    cbo11.ValueMember 
= "MA001";
                    cbo12.DataSource 
= DataSet1.Tables["Table2"];
                    cbo12.DisplayMember 
= "MA001C";
                    cbo12.ValueMember 
= "MA001";
                }

                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    cbo11.DataSource 
= null;
                    cbo11.DisplayMember 
= "";
                    cbo11.ValueMember 
= "";
                    cbo12.DataSource 
= null;
                    cbo12.DisplayMember 
= "";
                    cbo12.ValueMember 
= "";
                }


            }

            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{

            }

        }


        
private void cbo13_SelectedIndexChanged(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
//开始、结束两下拉框填值
                if (cbo13.SelectedIndex != 8)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    DataSet DataSet1 
= new DataSet();
                    SqlDataAdapter SqlDataAdapter1 
= new SqlDataAdapter();
                    SqlCommand sqlSelectCommand1 
= new SqlCommand();
                    SqlConnection sqlConnection1 
= new SqlConnection();

                    
//Set the variables.
                    if (cbo13.SelectedIndex == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        sqlSelectCommand1.CommandText 
= @"SELECT MA001 AS MA001,RTRIM(MA001)+'-'+RTRIM(MA002) AS MA001C FROM COPMA ORDER BY MA001";
                    }

                    
else if (cbo13.SelectedIndex == 1)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        sqlSelectCommand1.CommandText 
= @"SELECT MR002 AS MA001,RTRIM(MR002)+'-'+RTRIM(MR003) AS MA001C FROM CMSMR WHERE MR001='1' ORDER BY MR002";
                    }

                    
else if (cbo13.SelectedIndex == 2)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        sqlSelectCommand1.CommandText 
= @"SELECT MR002 AS MA001,RTRIM(MR002)+'-'+RTRIM(MR003) AS MA001C FROM CMSMR WHERE MR001='3' ORDER BY MR002";
                    }

                    
else if (cbo13.SelectedIndex == 3)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        sqlSelectCommand1.CommandText 
= @"SELECT MR002 AS MA001,RTRIM(MR002)+'-'+RTRIM(MR003) AS MA001C FROM CMSMR WHERE MR001='4' ORDER BY MR002";
                    }

                    
else if (cbo13.SelectedIndex == 4)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        sqlSelectCommand1.CommandText 
= @"SELECT ME001 AS MA001,RTRIM(ME001)+'-'+RTRIM(ME002) AS MA001C FROM CMSME ORDER BY ME001";
                    }

                    
else if (cbo13.SelectedIndex == 5)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        sqlSelectCommand1.CommandText 
= @"SELECT MV001 AS MA001,RTRIM(MV001)+'-'+RTRIM(MV002) AS MA001C FROM CMSMV ORDER BY MV001";
                    }

                    
else if (cbo13.SelectedIndex == 6)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        sqlSelectCommand1.CommandText 
= @"SELECT MV001 AS MA001,RTRIM(MV001)+'-'+RTRIM(MV002) AS MA001C FROM CMSMV ORDER BY MV001";
                    }

                    
else if (cbo13.SelectedIndex == 7)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        sqlSelectCommand1.CommandText 
= @"SELECT MB001 AS MA001,RTRIM(MB001)+'-'+RTRIM(MB002) AS MA001C FROM INVMB ORDER BY MB001";
                    }


                    sqlSelectCommand1.Connection 
= sqlConnection1;
                    sqlConnection1.ConnectionString 
= FConStr;
                    SqlDataAdapter1.SelectCommand 
= sqlSelectCommand1;

                    
// Fill the data set with the result of the query.
                    
// The results are stored in the data set called "Table1".
                    SqlDataAdapter1.Fill(DataSet1, "Table1");
                    SqlDataAdapter1.Fill(DataSet1, 
"Table2");

                    
//绑定
                    cbo14.DataSource = DataSet1.Tables["Table1"];
                    cbo14.DisplayMember 
= "MA001C";
                    cbo14.ValueMember 
= "MA001";
                    cbo15.DataSource 
= DataSet1.Tables["Table2"];
                    cbo15.DisplayMember 
= "MA001C";
                    cbo15.ValueMember 
= "MA001";
                }

                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    cbo14.DataSource 
= null;
                    cbo14.DisplayMember 
= "";
                    cbo14.ValueMember 
= "";
                    cbo15.DataSource 
= null;
                    cbo15.DisplayMember 
= "";
                    cbo15.ValueMember 
= "";
                }

            }

            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{

            }

        }


        
private void button2_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
this.Close();//关闭
        }


        
private void Main_FormClosed(object sender, FormClosedEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
//保存当前窗口宽高
                string mWidth = Main.ActiveForm.Width.ToString();
                
string mHeight = Main.ActiveForm.Height.ToString();
                
string mLeft = Main.ActiveForm.Left.ToString();
                
string mTop = Main.ActiveForm.Top.ToString();

                IniWriteValue(
"FormInfo""Width", mWidth, FExePath + "\\CrystalReport.ini");
                IniWriteValue(
"FormInfo""Height", mHeight, FExePath + "\\CrystalReport.ini");
                IniWriteValue(
"FormInfo""Left", mLeft, FExePath + "\\CrystalReport.ini");
                IniWriteValue(
"FormInfo""Top", mTop, FExePath + "\\CrystalReport.ini");
            }

            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{

            }

        }


    }

}
C

转载于:https://www.cnblogs.com/safezone/articles/1251142.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好!要在C#使用DataTable和Crystal Reports来生成报表,可以按照以下步骤进行操作: 1. 首先,确保已经添加了Crystal Reports插件到你的项目。你可以通过右键点击项目文件,选择"添加"->"新项"->"报表"来添加。 2. 创建一个DataTable对象,并向其添加数据。你可以使用Add方法来逐行添加数据,或者使用Load方法从其他数据源加载数据。 ```csharp DataTable dataTable = new DataTable(); dataTable.Columns.Add("Column1"); dataTable.Columns.Add("Column2"); dataTable.Rows.Add("Value1", "Value2"); // 添加更多的行和数据... // 或者从其他数据源加载数据 // dataTable.Load(dataReader); ``` 3. 创建一个Crystal Reports的报表对象,并设置其数据源为刚刚创建的DataTable。 ```csharp using CrystalDecisions.CrystalReports.Engine; using CrystalDecisions.Shared; ReportDocument report = new ReportDocument(); report.Load("YourReportFile.rpt"); // 替换为你的报表文件路径 report.SetDataSource(dataTable); ``` 4. 最后,将报表对象显示在Crystal Reports查看器或导出为其他格式。 ```csharp using CrystalDecisions.Windows.Forms; CrystalReportViewer crystalReportViewer = new CrystalReportViewer(); crystalReportViewer.ReportSource = report; crystalReportViewer.Refresh(); // 刷新查看器 // 或者导出为PDF等其他格式 // report.ExportToDisk(ExportFormatType.PortableDocFormat, "YourExportFile.pdf"); ``` 这些是基本的步骤,你可以根据自己的需求进行进一步的定制和调整。希望对你有所帮助!如果你有任何其他问题,请随时提问。

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值