一开始听到job的要求,还真不知道如何下手,用Gridview?好像有做不到把图片show出来的。在网上搜索一遍,发现原来是ListView与Imglist的结合应用。
看设计:
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click Try Me.ListView1.Items.Clear() Me.imglist.Images.Clear() Dim str As String = "" Dim strWhere As String = "" str = "select t1.FEATCODE,t1.FEATTITL,T1.FEATDSC2,t2.featmgid from CD_Features t1 left join CD_FeatureImages t2 on t1.FEATCODE=t2.FEATCODE" & _ " where t1.FEATCODE like '" & Me.txtFeatureCode.Text.Trim & "%' and FEATTITL like '" & Me.txtTitle.Text.Trim & "%'" & _ " and FEATDESC like '%" & Me.txtFeatureDescE.Text.Trim & "%' and FEATDSC2 like '%" & Me.txtFeatureDescC.Text.Trim & "%' " If Me.cboLayer.Text.Trim <> "" Then str = str & " and substring(t1.FEATCODE,5,2) like'" & Me.cboLayer.SelectedValue.ToString & "%' " strWhere = strWhere & " and substring(a.FEATCODE,5,2) like'" & Me.cboLayer.SelectedValue.ToString & "%' " End If If Me.cboComponent.Text.Trim <> "" Then str = str & " and substring(t1.FEATCODE,3,2) like'" & Me.cboComponent.SelectedValue.ToString & "%' " strWhere = strWhere & " and substring(a.FEATCODE,3,2) like'" & Me.cboComponent.SelectedValue.ToString & "%' " End If If Me.cboConstrustion.Text.Trim <> "" Then str = str & " and substring(t1.FEATCODE,7,2) like'" & Me.cboConstrustion.SelectedValue.ToString & "%'" strWhere = strWhere & " and substring(a.FEATCODE,7,2) like'" & Me.cboConstrustion.SelectedValue.ToString & "%'" End If Dim dt As DataTable = New DataTable dt = gData.GetDataTable(str, sqlConn) str = "SELECT A.FEATCODE,FEATTITL,SUM(DOPNSMV)SMV FROM dbo.CD_Features A1 INNER JOIN dbo.CD_FeatureOpns A ON A1.FEATCODE=A.FEATCODE" & _ " INNER JOIN dbo.CD_DtledOperationSmv B ON A.DOPNCODE=B.DOPNCODE" & _ " AND A.MACHCODE=B.MACHCODE AND A.STITINCH=B.STITINCH AND A.STITGAUG=B.STITGAUG" & _ " AND A.NEEDLE=B.NEEDLE AND A.BOBBIN=B.BOBBIN AND A.TOPLOOP=B.TOPLOOP" & _ " AND A.UNDLOOP=B.UNDLOOP WHERE A.FEATCODE like '" & Me.txtFeatureCode.Text.Trim & "%' and FEATTITL like '" & Me.txtTitle.Text.Trim & "%'" str = str & strWhere & " GROUP BY A.FEATCODE,FEATTITL" Dim dtSMV As DataTable = New DataTable dtSMV = gData.GetDataTable(str, sqlConn) Dim smv As Decimal = 0 Dim k As Integer = 0 'imglist Dim indx As Integer = 0 '圖片 For i As Integer = 0 To dt.Rows.Count - 1 Dim dr() As DataRow = dtSMV.Select("FEATCODE='" & dt.Rows(i).Item("FEATCODE").ToString & "'") If dr.Length > 0 Then smv = Decimal.Parse(dr(0).Item("SMV")) Else smv = 0 End If If dt.Rows(i).Item("featmgid").ToString = "" Then indx = 0 Else indx = Int16.Parse(dt.Rows(i).Item("featmgid").ToString) End If Dim command As New SqlCommand("select image from cd_featureimages where FEATCODE='" & dt.Rows(i).Item("FEATCODE") & "' and FEATMGID=" & indx & "", SqlConnect) Dim by As Byte() = DirectCast(command.ExecuteScalar(), Byte()) Dim myImage As System.Drawing.Image = LoadImage(by) Me.ListView1.Items.Add(dt.Rows(i).Item("FEATCODE").ToString & ",Total SMV:" & smv & "," & Chr(13) & dt.Rows(i).Item("FEATTITL").ToString & Chr(13) & dt.Rows(i).Item("FEATDSC2").ToString) If Not myImage Is Nothing Then k = k + 1 Me.imglist.Images.Add(myImage) Me.ListView1.Items(i).ImageIndex = k - 1 End If Next Catch ex As Exception MessageBox.Show(ex.ToString) End Try End Sub Private Function LoadImage(ByVal by As Byte()) As System.Drawing.Bitmap Try If by Is Nothing Then Return Nothing End If Me.Cursor = Cursors.WaitCursor Dim m_PicW As Integer = 168 Dim m_PicH As Integer = 140 Dim ms As New IO.MemoryStream(by) Dim imgT As New PictureBox imgT.SizeMode = PictureBoxSizeMode.AutoSize imgT.Image = Image.FromStream(ms) Dim bmp As New System.Drawing.Bitmap(m_PicW, m_PicH) Dim grp As Graphics = Graphics.FromImage(bmp) Dim blueBrush As New SolidBrush(Color.White) grp.FillRectangle(blueBrush, 0, 0, m_PicW, m_PicH) Dim intW As Single Dim intH As Single If imgT.Width > m_PicW Then intW = m_PicW intH = imgT.Height * (m_PicW / imgT.Width) Else intW = imgT.Width intH = imgT.Height End If If intH > m_PicH Then intH = m_PicH intW = imgT.Width * (m_PicH / imgT.Height) End If grp.DrawImage(imgT.Image, (m_PicW - intW) / 2, (m_PicH - intH) / 2, intW, intH) ms.Close() Me.Cursor = Cursors.Default Return bmp Catch ex As Exception Me.Cursor = Cursors.Default MsgBox(ex.ToString, MsgBoxStyle.Critical, CD_SYSTEM_NAME) Return Nothing End Try End Function