Dim ConnectionString As String = ""
Dim sqlString As String = ""
'保持字段的字符串
Dim FieldString As String = ""
'保持字段名称的数组
Dim FieldArray() As String
Dim i As Integer, j As Integer
'参数字段集合
Dim ParamFields As New ParameterFields
'参数字段
Dim ParamField As ParameterField
'离散值
Dim DiscreteVal As New ParameterDiscreteValue
'---------------------------------------------------------------------
'获取选取的字段并进行处理
'此处写的比较笨拙,用控件数组更方便
If CheckBox1.Checked = True Then
FieldString = CheckBox1.Text
End If
If CheckBox2.Checked = True Then
FieldString = FieldString & "," & CheckBox2.Text
End If
If CheckBox3.Checked = True Then
FieldString = FieldString & "," & CheckBox3.Text
End If
If CheckBox4.Checked = True Then
FieldString = FieldString & "," & CheckBox4.Text
End If
If CheckBox5.Checked = True Then
FieldString = FieldString & "," & CheckBox5.Text
End If
If CheckBox6.Checked = True Then
FieldString = FieldString & "," & CheckBox6.Text
End If
If FieldString = "" Then
MessageBox.Show("请选择要显示的字段")
Exit Sub
End If
'删除第一个逗号,使字串最终显示为 "字段1,字段2,字段3"
If FieldString.Substring(0, 1) = "," Then
FieldString = FieldString.Substring(1, FieldString.Length - 1)
End If
'分割成数组,每个元素为一个字段名称
FieldArray = FieldString.Split(",")
'---------------------------------------------------------------------
'数据获取与组织部分
'以下为数据获取段,你可以根据自己的实际情况任意更改数据获取方式
ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "\BBT_Crystal.mdb"
''打开数据库联接
adoOleDbConnection = New OleDbConnection(ConnectionString)
''Sql语句
'sqlString = "Select * From 材料采购明细"
sqlString = "Select " & FieldString & " From 材料采购明细 where 编号=7"
''执行语句
adoOleDbDataAdapter = New OleDbDataAdapter(sqlString, adoOleDbConnection)
''创建Dataset实例
dataSet = New DataSet
''填充dataSet
adoOleDbDataAdapter.Fill(dataSet, "材料采购明细")
'---------------------------------------------------------------------
''创建一个强类型报表实例
crReportDocument = New CrystalReport1
''crReportDocument.Database.Tables("材料采购明细").SetDataSource(dataSet)
'开始以数组为依据依次顺序设置表头和公式字段
For i = 0 To UBound(FieldArray)
'设置第一个离散值并将其传递给该参数
'传递参数做为表头
'--------------------------------------
'参数设置部分
ParamField = New ParameterField
ParamField.ParameterFieldName = "myParaField" & (i + 1).ToString
'离散值
DiscreteVal = New ParameterDiscreteValue
DiscreteVal.Value = FieldArray(i)
MsgBox(FieldArray(i))
'赋值
ParamField.CurrentValues.Add(DiscreteVal)
'将该字段加如到字段集合中
ParamFields.Add(ParamField)
'不允许该字段弹出提示
'ParamField.AllowCustomValues = False
'将公式绑定到具体字段
crReportDocument.DataDefinition.FormulaFields("myField" & (i + 1).ToString).Text = "{材料采购明细." & FieldArray(i) & "}"
MsgBox("{材料采购明细." & FieldArray(i) & "}")
Next
'设置剩余的未使用到的参数,防止出现提示
'具体操作同上,只是给一个空值
能给解释一下离散值的解释吗??????????????
For j = i + 1 To 6
'设置第一个离散值并将其传递给该参数
'传递参数做为表头
ParamField = New ParameterField
ParamField.ParameterFieldName = "myParaField" & (j).ToString
ParamFields.Add(ParamField)
DiscreteVal = New ParameterDiscreteValue
DiscreteVal.Value = ""
ParamField.CurrentValues.Add(DiscreteVal)
MsgBox(DiscreteVal.Value)
ParamFields.Add(ParamField)
'ParamField.AllowCustomValues = False
Next
'将参数字段集合绑定到浏览器
CrystalReportViewer1.ParameterFieldInfo = ParamFields
''将Dataset传递给报表
crReportDocument.SetDataSource(dataSet)
''将报表传递给浏览器
CrystalReportViewer1.ReportSource = crReportDocument