在數據表userlist 中有一個字段 foto 用來存放圖片的路徑(包括圖片文件名),為了在 datagrid 的 cell 中顯示實際的圖片,我們可以定義一個模板列,然后給該列賦予字段foto 的值,就可以在 datagrid 的 cell 中顯示圖片.
       首先請看如下代碼: 
      webform2.aspx 文件:
  <%@ page language="vb" autoeventwireup="false" codebehind="webform2.aspx.vb" inherits="house.webform2"%>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
 <head>
  <title>webform2</title>
  <meta name="generator" content="microsoft visual studio .net 7.1">
  <meta name="code_language" content="visual basic .net 7.1">
  <meta name="vs_defaultclientscript" content="javascript">
  <meta name="vs_targetschema" content="http://schemas.microsoft.com/intellisense/ie5">
 </head>
 <body ms_positioning="gridlayout">
  <form id="form1" method="post" runat="server">
   <font face="宋體">
    <asp:datagrid id="datagrid1" 
     runat="server" width="272px" height="350px">
     <columns>
      <asp:templatecolumn>
       <itemtemplate>
        <asp:image id="image1" runat="server" width="96px" imageurl='<% #databinder.eval(container,"dataitem.foto") %>'>
        </asp:image>
       </itemtemplate>
      </asp:templatecolumn>
     </columns>
    </asp:datagrid></font>
  </form>
 </body>
</html>
webform2.aspx.vb 文件
public class webform2
    inherits system.web.ui.page
#region " web 窗體設計器生成的代碼 "
    '該調用是 web 窗體設計器所必需的。
    <system.diagnostics.debuggerstepthrough()> private sub initializecomponent()
    end sub
    protected withevents datagrid1 as system.web.ui.webcontrols.datagrid
    '注意: 以下占位符聲明是 web 窗體設計器所必需的。
    '不要刪除或移動它。
    private designerplaceholderdeclaration as system.object
    private sub page_init(byval sender as system.object, byval e as system.eventargs) handles mybase.init
        'codegen: 此方法調用是 web 窗體設計器所必需的
        '不要使用代碼編輯器修改它。
        initializecomponent()
    end sub
#end region
    private sub page_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load
        '在此處放置初始化頁的用戶代碼
        dim sql as string = " select  * from userlist "
        dim dataset1 as new dataset
        dataset1 = new dataset("dataset1")
        dim connectionstring as string
        connectionstring = "jet oledb:global partial bulk ops=2;jet oledb:registry path=;jet oledb:database l" & _
        "ocking mode=1;data source=""labrecord.mdb"";jet oledb:engine type=5;provider=""mic" & _
        "rosoft.jet.oledb.4.0"";jet oledb:system database=;jet oledb:sfp=false;persist sec" & _
        "urity info=false;extended properties=;mode=share deny none;jet oledb:encrypt dat" & _
        "abase=false;jet oledb:create system database=false;jet oledb:don't copy locale o" & _
        "n compact=false;jet oledb:compact without replica repair=false;user id=admin;jet" & _
        " oledb:global bulk transactions=1"
        dim oledbconnection1 as new oledb.oledbconnection(connectionstring)
        dim oledbdataadapter1 as new oledb.oledbdataadapter(sql, oledbconnection1)
        oledbdataadapter1.fill(dataset1, "userlist")
        oledbdataadapter1 = nothing  
        datagrid1.datasource = dataset1
        datagrid1.databind()
        oledbconnection1.close()
        oledbconnection1 = nothing
end sub
end class
綜述與總結:
      以前不知道如何在datagrid中如何顯示圖片的時候,在網上搜查相關技術資料的時候,很多.但是
往往寫得很多,很雜亂.甚至把人引入誤區.
     下面我用簡要的話來說明這個主題的關鍵: 首先要在一個頁面(如本例中的webform2.aspx )中添加一個datagrid1然后給datagrid1添加一個模板列.然后再往這個模板列中加入一個image控件. 最關鍵的地方來了,那就是 
<asp:templatecolumn>
       <itemtemplate>
        <asp:image id="image1" runat="server" width="96px" imageurl='<% #databinder.eval(container,"dataitem.foto") %>'>
        </asp:image>
       </itemtemplate>
      </asp:templatecolumn>
      中藍色的部分.即將圖片的路徑與 userlist 中的 foto字段綁定. 至于  webform2.aspx.vb 文件 的作用是將
數據庫中的表userlist 的數據與datagrid1綁定.      
新聞熱點
疑難解答
圖片精選