VB.net tips Five------display a picture in a DataGrid

Source: Internet
Author: User
Tags compact eval visual studio
Datagrid| Skills | show

In the datasheet userlist there is a field foto the path (including the picture file name) to hold the picture, in order to display the actual picture in the Cell of the DataGrid, we can define a template column, then give the column the value of the field foto, can be in the DataGrid Cel L displays pictures in.
First, look at the following code:
Webform2.aspx file:

<%@ 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= "Song Body" >
<asp:datagrid id= "DataGrid1" style= "Z-INDEX:101; left:224px; Position:absolute; Top:16px "
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 file

Public Class WebForm2
Inherits System.Web.UI.Page

#Region the code generated by the Web Forms Designer

' This call is required by the Web Forms Designer.
<system.diagnostics.debuggerstepthrough () > Private Sub InitializeComponent ()

End Sub
Protected WithEvents DataGrid1 as System.Web.UI.WebControls.DataGrid

' NOTE: The following placeholder declarations are required by the Web Forms Designer.
' Do not delete or move it.
Private Designerplaceholderdeclaration as System.Object

Private Sub Page_Init (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Mybase.init
' CodeGen: This method call is required by the Web Forms Designer
' Do not modify it using the Code Editor.
InitializeComponent ()
End Sub

#End Region

Private Sub Page_Load (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles MyBase.Load
' The user code to place the initialization page here
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

Summary and Summary:

When I didn't know how to display a picture in a datagrid, I searched the relevant technical data on the Internet. but
Often written a lot, very messy. Even put people into mistaken ideas.
Here's the key to this topic: first, add a datagrid1 to a page (such as the webform2.aspx in this example) and add a template column to DataGrid1. Add an image control to the template column. The most important place to come is that

<asp:TemplateColumn>
<ItemTemplate>
<asp:image id= "Image1" runat= "Server" width= "96px" Imageurl= ' <% #DataBinder. Eval (Container, "Dataitem.foto")% > ' >
</asp:Image>
</ItemTemplate>
</asp:TemplateColumn>
In the Blue section. The path to the picture is bound to the Foto field in userlist. As for the role of the WebForm2.aspx.vb document, the
The data in the table userlist in the database is bound to DataGrid1.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.