更改gridview字段类型 [英] Change gridview field type
问题描述
我是vb.net的新手!我的代码工作在GridView上显示我的数据库数据它的工作正常,但我想将特定值更改为Image?我的代码:
Dim img_c As String =my image Url
Dim img_x As String
Dim constr As String =(my connection string )
Dim con As New SqlConnection(constr)
Dim cmd As New SqlCommand()
cmd.CommandText =SELECT * FROM Attendance
cmd.Connection = con
Dim sda As New SqlDataAdapter(cmd)
Dim dt As New DataTable()
sda.Fill(dt)
GridView1.DataSource = dt
GridView1.DataBind()
Dim ds As New DataSet
For i As Integer = 1 To GridView1.Rows.Count - 2
For s As Integer = 3 To GridView1.Columns.Count - 2
If GridView1。行(i).Cells(s)。text =1然后
GridView1.ImageField(img_c)
结束如果
下一个
下一个
我的来源:
< asp:GridView ID =GridView1runat =serverAutoGenerateColumns =False
CellPadding =4DataKeyNames =std_num
高度=448pxWidth =1352pxBackColor =WhiteBorderColor =#3366CC
BorderStyle =NoneBorderWidth =1pxstyle =margin-right:12px>
< Columns>
< asp:BoundField DataField =TotalHeaderText =totlaSortExpression =Total>
< HeaderStyle ForeColor =White/>
< / asp:BoundField>
< asp:BoundField DataField =4HeaderText =4SortExpression =4>
< HeaderStyle ForeColor =White/>
< / asp:BoundField>
< asp:BoundField DataField =3HeaderText =3SortExpression =3>
< HeaderStyle ForeColor =White/>
< / asp:BoundField>
< asp:BoundField DataField =2HeaderText =2SortExpression =2>
< HeaderStyle ForeColor =White/>
< / asp:BoundField>
< asp:BoundField DataField =1HeaderText =1SortExpression =1>
< HeaderStyle ForeColor =White/>
< / asp:BoundField>
< asp:BoundField DataField =std_nameHeaderText =std_name
SortExpression =std_name>
< HeaderStyle ForeColor =White/>
< / asp:BoundField>
< asp:BoundField DataField =std_numHeaderText =std_num
SortExpression =std_numReadOnly =True>
< HeaderStyle ForeColor =White/>
< / asp:BoundField>
< asp:BoundField DataField =NoHeaderText =NoSortExpression =No
InsertVisible =FalseReadOnly =True>
< HeaderStyle ForeColor =White/>
< / asp:BoundField>
< / Columns>
< FooterStyle BackColor =#99CCCCForeColor =#003399/>
< HeaderStyle BackColor =#003399Font-Bold =TrueForeColor =#CCCCFF/>
< PagerStyle BackColor =#99CCCCForeColor =#003399HorizontalAlign =Left/>
< RowStyle BackColor =WhiteForeColor =BlackBorderColor =#333333
Font-Bold =TrueHorizontalAlign =CenterVerticalAlign =Middle/>
< SelectedRowStyle BackColor =#009999Font-Bold =TrueForeColor =#CCFF99/>
< SortedAscendingCellStyle BackColor =#EDF6F6/>
< SortedAscendingHeaderStyle BackColor =#0D4AC4/>
< SortedDescendingCellStyle BackColor =#D6DFDF/>
< SortedDescendingHeaderStyle BackColor =#002876/>
< / asp:GridView>
我的问题是改变字段到Image,没有GridView图像属性
< b>我尝试了什么:
如果ds.Tables(出勤)。行(i - 1).Item(s).ToString =1然后
GridView1.Rows(i).Cells(s).Text = ImageField(img_c)
结束如果
您无法在GridView中为ImageField分配值。但是,您可以使用ImageField的DataImageUrlField属性并在其中设置图像URL。
另外,如果你想完全控制你的Image,那么我建议你改用TemplateField。例如:
< asp:TemplateField >
< ItemTemplate >
< asp:image id = Image1 runat = server / >
< / ItemTemplate >
< / asp:TemplateField >
然后在您的代码中,在RowDataBound事件中,您可以访问Image控件并根据您的条件将ImageUrl设置为您想要的任何内容。一个简单的例子是:
受保护的 Sub GridView1_RowDataBound( ByVal sender As Object , ByVal e As GridViewRowEventArgs)
如果 e.Row.RowType = DataControlRowType.DataRow 那么
Dim img1 As Image = CType (e.Row.FindControl( Image1),Image)
如果 img1 IsNot 没什么 然后
Dim fieldToCheck = e.Row.Cells( 4 )。文字
如果 fieldToCheck.Equals( 1)然后
img1.Visible = True
img1.ImageUrl = 在此处设置图像路径
否则
img1.Visible = 错误
结束 如果
结束 如果
结束 如果
结束 Sub
I'm a newbie in vb.net ! my code working to show my database data on GridView it's work fine but I want to change specific value to Image? my code :
Dim img_c As String = "my image Url" Dim img_x As String Dim constr As String = ("my connection string") Dim con As New SqlConnection(constr) Dim cmd As New SqlCommand() cmd.CommandText = "SELECT * FROM Attendance" cmd.Connection = con Dim sda As New SqlDataAdapter(cmd) Dim dt As New DataTable() sda.Fill(dt) GridView1.DataSource = dt GridView1.DataBind() Dim ds As New DataSet For i As Integer = 1 To GridView1.Rows.Count - 2 For s As Integer = 3 To GridView1.Columns.Count - 2 If GridView1.Rows(i).Cells(s).text ="1" Then GridView1.ImageField(img_c) End If Next Next
my source:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="std_num" Height="448px" Width="1352px" BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" style="margin-right: 12px"> <Columns> <asp:BoundField DataField="Total" HeaderText="totla" SortExpression="Total" > <HeaderStyle ForeColor="White" /> </asp:BoundField> <asp:BoundField DataField="4" HeaderText="4" SortExpression="4" > <HeaderStyle ForeColor="White" /> </asp:BoundField> <asp:BoundField DataField="3" HeaderText="3" SortExpression="3" > <HeaderStyle ForeColor="White" /> </asp:BoundField> <asp:BoundField DataField="2" HeaderText="2" SortExpression="2" > <HeaderStyle ForeColor="White" /> </asp:BoundField> <asp:BoundField DataField="1" HeaderText="1" SortExpression="1" > <HeaderStyle ForeColor="White" /> </asp:BoundField> <asp:BoundField DataField="std_name" HeaderText="std_name" SortExpression="std_name" > <HeaderStyle ForeColor="White" /> </asp:BoundField> <asp:BoundField DataField="std_num" HeaderText="std_num" SortExpression="std_num" ReadOnly="True" > <HeaderStyle ForeColor="White" /> </asp:BoundField> <asp:BoundField DataField="No" HeaderText="No" SortExpression="No" InsertVisible="False" ReadOnly="True" > <HeaderStyle ForeColor="White" /> </asp:BoundField> </Columns> <FooterStyle BackColor="#99CCCC" ForeColor="#003399" /> <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" /> <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" /> <RowStyle BackColor="White" ForeColor="Black" BorderColor="#333333" Font-Bold="True" HorizontalAlign="Center" VerticalAlign="Middle" /> <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" /> <SortedAscendingCellStyle BackColor="#EDF6F6" /> <SortedAscendingHeaderStyle BackColor="#0D4AC4" /> <SortedDescendingCellStyle BackColor="#D6DFDF" /> <SortedDescendingHeaderStyle BackColor="#002876" /> </asp:GridView>
my issue is change field to Image , there is no GridView image properties
What I have tried:
If ds.Tables("Attendance").Rows(i - 1).Item(s).ToString = "1" Then
GridView1.Rows(i).Cells(s).Text = ImageField(img_c)
End If
You can't assign a value to the ImageField in GridView. However, you can probably use the DataImageUrlField attribute of ImageField and set the image URL there.
Also, if you want to have a full control over your Image, then I would suggest you to use TemplateField instead. For example:
<asp:TemplateField> <ItemTemplate> <asp:image id="Image1" runat="server" /> </ItemTemplate> </asp:TemplateField>
Then in your code, at RowDataBound event, you can access the Image control and set the ImageUrl to whatever you want based on your condition. A quick example would be:
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) If e.Row.RowType = DataControlRowType.DataRow Then Dim img1 As Image = CType(e.Row.FindControl("Image1"), Image) If img1 IsNot Nothing Then Dim fieldToCheck = e.Row.Cells(4).Text If fieldToCheck.Equals("1") Then img1.Visible = True img1.ImageUrl = "Set the image path here" Else img1.Visible = False End If End If End If End Sub
这篇关于更改gridview字段类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!