我在代码隐藏中将一些列添加到GridView中。在实际的.aspx页面中,我有一个链接字段。这工作正常,但链接字段显示为第一列,我更喜欢它显示为最后一个(一直到右边)。
有没有办法指定顺序,所以链接字段在右边?我正在使用ASP.NET 4.0。
这是我的代码隐藏:
Private Sub loadDynamicGrid()
Dim connetionString As String
Dim connection As SqlConnection
Dim command As SqlCommand
Dim adapter As New SqlDataAdapter
Dim ds As New DataSet
Dim sql As String
Dim lastName As String
Dim linkText As String
lastName = Request.QueryString("lastName")
connetionString = ConfigurationManager.ConnectionStrings("dbConnectionString").ConnectionString.ToString()
sql = "SELECT * FROM [EmployeeList] Where [lastname] like '" & lastName & "%' order by lastname"
connection = New SqlConnection(connetionString)
Try
connection.Open()
command = New SqlCommand(sql, connection)
adapter.SelectCommand = command
adapter.Fill(ds)
'GridView3.Columns.Clear()
'Build Bound Columns
Dim curLastName As New BoundField
curLastName.HeaderText = "Last Name"
curLastName.DataField = "LastName"
GridView3.Columns.Add(curLastName)
Dim curFirstName As New BoundField
curFirstName.HeaderText = "First Name"
curFirstName.DataField = "FirstName"
GridView3.Columns.Add(curFirstName)
GridView3.Visible = True
GridView3.DataSource = ds
GridView3.DataBind()
adapter.Dispose()
command.Dispose()
connection.Close()
Catch ex As Exception
MsgBox("Can not open connection ! ")
End Try
End Sub
这是GridView代码:
<asp:GridView id="GridView3" runat="server" AutoGenerateColumns="False"
EmptyDataText="There are no data records to display."
AllowPaging="True"
CssClass="GridViewStyle" GridLines="None" Width="100%">
<Columns>
<asp:HyperLinkField DataNavigateUrlFields="EmplID"
DataNavigateUrlFormatString="EmployeeProfile.aspx?EmplID={0}"
DataTextField="EmplID"
DataTextFormatString= "<img src='Images/icons/document-search-result.png' alt='View'/> <u>View</u>" >
<ControlStyle CssClass="titleLinksB" />
<ItemStyle Wrap="False" />
</asp:HyperLinkField>
</Columns>
</asp:GridView>
感谢您的帮助!
答案 0 :(得分:0)
您可以尝试这种方式,在后面的代码中,您可以删除模具控制字段行。
<asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="False" EmptyDataText="There are no data records to display."
AllowPaging="True" CssClass="GridViewStyle" GridLines="None" Width="100%">
<Columns>
<asp:BoundField HeaderText="Last Name" DataField="LastName"/>
<asp:BoundField HeaderText="First Name" DataField="FirstName"/>
<asp:HyperLinkField DataNavigateUrlFields="EmplID" DataNavigateUrlFormatString="EmployeeProfile.aspx?EmplID={0}"
DataTextField="EmplID" DataTextFormatString="<img src='Images/icons/document-search-result.png' alt='View'/> <u>View</u>">
<ControlStyle CssClass="titleLinksB" />
<ItemStyle Wrap="False" />
</asp:HyperLinkField>
</Columns>
</asp:GridView>
答案 1 :(得分:0)
尝试使用GridView3.Columns.Insert
代替Add
。例如:
GridView3.Columns.Insert(0, curLastName)
通过这种方式,首先添加动态创建的列,然后添加声明性添加的列。