我有这个gridview:
<asp:GridView ID="GridViewTest" AutoGenerateColumns="False" runat="server" AllowSorting="True">
<Columns>
<asp:TemplateField ItemStyle-Width="155px">
<HeaderTemplate>
<asp:Label runat="server" Text="FirstName"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="FirstName" runat="server" Text='<%# Bind("FirstName")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="110px" >
<HeaderTemplate>
<asp:Label runat="server" Text="LastName" ></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="LastName" runat="server" Text='<%# Bind("LastName")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="DateMin" HeaderText="DateMin" ItemStyle-Width="110px" />
<asp:BoundField DataField="DateMax" HeaderText="DateMax" ItemStyle-Width="110px" />
<asp:TemplateField ItemStyle-Width="100px" >
<HeaderTemplate>
<asp:Label runat="server" Text="PhoneNumber"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="PhoneNumber" runat="server" Text='<%# Bind("PhoneNumber")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="100px" >
<HeaderTemplate>
<asp:Label ID="Details" runat="server" Text="Details"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:LinkButton ID="Details" runat="server" OnClick="details_Click" Text="Details">
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
我想:
1)在gridview的x轴上添加一个带有固定标题的滚动条。
2)按FirstName,LastName和PhoneNumber asc / desc。
命令此gridview3)如果我点击元素&#34;细节&#34;在任何一行中,我需要访问该行中的所有列值,以便在特定对象中记忆它们。
1)第一个问题解决了这样的问题:
我添加了一个&#34; div&#34; gridview外部的元素:固定宽度(gridview还有固定的宽度和每个项目的固定宽度);固定高度; &#34;溢出:汽车&#34 ;.我隐藏了gridview的标题。我在gridview上面添加了一个带有新标题的表。它有效(即使我认为它不是最佳解决方案)。
<table style="height:30px;max-height:30px; background-color:#473BA3; border:1px; border-collapse:collapse; text-align:center;">
<tr>
<td style="width:160px; border: 1px solid black; border-collapse: collapse;">
<asp:Label Text="FirstName" runat="server"></asp:Label>
<asp:LinkButton ID="orderFirstNameByIdAsc" runat="server" Text="↑" OnClick="SortMethod" CommandName="Sort" CommandArgument="FirstNameAsc"></asp:LinkButton>
<asp:LinkButton ID="orderFirstNameByIdDesc" runat="server" Text="↓" OnClick="SortMethod" CommandName="Sort" CommandArgument="FirstNameDesc"></asp:LinkButton>
</td>
<td style="width:110px; border: 1px solid black; border-collapse: collapse;">
<asp:Label Text="LastName" runat="server"></asp:Label>
<asp:LinkButton ID="orderLastNameByIdAsc" runat="server" Text="↑" OnClick="SortMethod" CommandName="Sort" CommandArgument="LastNameAsc"></asp:LinkButton>
<asp:LinkButton ID="orderLastNameByIdDesc" runat="server" Text="↓" OnClick="SortMethod" CommandName="Sort" CommandArgument="LastNameDesc"></asp:LinkButton>
</td>
<td style="width:105px; border: 1px solid black; border-collapse: collapse;">
<asp:Label Text="DateMax" runat="server"></asp:Label>
</td>
<td style="width:105px; border: 1px solid black; border-collapse: collapse;">
<asp:Label Text="DateMax" runat="server"></asp:Label>
</td>
<td style="width:93px; border: 1px solid black; border-collapse: collapse;">
<asp:Label Text="PhoneNumber" runat="server"></asp:Label>
<asp:LinkButton ID="orderPhoneNumberByIdAsc" runat="server" Text="↑" OnClick="SortMethod" CommandName="Sort" CommandArgument="PhoneNumberAsc"></asp:LinkButton>
<asp:LinkButton ID="orderPhoneNumberByIdDesc" runat="server" Text="↓" OnClick="SortMethod" CommandName="Sort" CommandArgument="PhoneNumberDesc"></asp:LinkButton>
</td>
<td style="width:100px; border: 1px solid black; border-collapse: collapse;">
<asp:Label Text="Details" runat="server"></asp:Label>
</td>
</tr>
</table>
<div style="height:350px; max-height:350px; width:905px; max-width:905px; overflow:auto;" >
<asp:GridView ID="GridViewTest" Width="885px" Height="100%" ShowHeader="false" AutoGenerateColumns="False" runat="server" AllowSorting="True">
<Columns>
<asp:TemplateField ItemStyle-Width="155px">
<HeaderTemplate>
<asp:Label runat="server" Text="FirstName"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="FirstName" runat="server" Text='<%# Bind("FistName")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="110px" >
<HeaderTemplate>
<asp:Label runat="server" Text="LastName" ></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="LastName" runat="server" Text='<%# Bind("LastName")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="DateMin" HeaderText="DateMin" ItemStyle-Width="110px" />
<asp:BoundField DataField="DateMax" HeaderText="DateMax" ItemStyle-Width="110px" />
<asp:TemplateField ItemStyle-Width="100px" >
<HeaderTemplate>
<asp:Label runat="server" Text="PhoneNumber"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="PhoneNumber" runat="server" Text='<%# Bind("PhoneNumber")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="100px" >
<HeaderTemplate>
<asp:Label ID="Details" runat="server" Text="Details"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:LinkButton ID="Details" runat="server" OnClick="details_Click" Text="Details">
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
2)第二个问题:
MyElementList是一个带有一些元素的特定对象..它在练习中并不重要,所以我称之为&#34; MyElementList&#34;使用一组元素,如&#34; FirstName&#34;,&#34; LastName&#34; ..等。
protected void SortMethod(object sender, EventArgs e)
{
LinkButton btn = (LinkButton)(sender);
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("FirstName", typeof(string)));
dt.Columns.Add(new DataColumn("LastName", typeof(string)));
dt.Columns.Add(new DataColumn("DateMin", typeof(string)));
dt.Columns.Add(new DataColumn("DateMax", typeof(string)));
dt.Columns.Add(new DataColumn("PhoneNumber", typeof(int)));
foreach ("Element" dv in "MyElementList")
{
dr = dt.NewRow();
dr["FirstName"] = dv.FirstName;
dr["LastName"] = dv.LastName;
dr["DateMin"] = dv.DateMin;
dr["DateMax"] = dv.DateMax;
dr["PhoneNumber"] = dv.PhoneNumber;
dt.Rows.Add(dr);
}
if (dt != null)
{
DataView dv = new DataView(dt);
if (btn.CommandArgument.Contains("FirstName"))
{
if (btn.CommandArgument.Equals("FirstNameAsc"))
dv.Sort = String.Format("{0} {1}", btn.CommandArgument.Substring(0, 9), "ASC");
else if (btn.CommandArgument.Equals("FirstNameDesc"))
dv.Sort = String.Format("{0} {1}", btn.CommandArgument.Substring(0, 9), "DESC");
}
else if (btn.CommandArgument.Contains("LastName"))
{
if (btn.CommandArgument.Equals("LastNameAsc"))
dv.Sort = String.Format("{0} {1}", btn.CommandArgument.Substring(0, 8), "ASC");
else if (btn.CommandArgument.Equals("LastNameDesc"))
dv.Sort = String.Format("{0} {1}", btn.CommandArgument.Substring(0, 8), "DESC");
}
else if (btn.CommandArgument.Contains("PhoneNumber"))
{
if (btn.CommandArgument.Equals("PhoneNumberAsc"))
dv.Sort = String.Format("{0} {1}", btn.CommandArgument.Substring(0, 11), "ASC");
else if (btn.CommandArgument.Equals("PhoneNumberDesc"))
dv.Sort = String.Format("{0} {1}", btn.CommandArgument.Substring(0, 11), "DESC");
}
GridViewTest.DataSource = dv;
GridViewTest.DataBind();
}
}
P.S。 如果我使用
DataTable dataTable = GridViewTest.DataSource as DataTable;
而不是创建新的数据表,因为&#34;详细信息&#34;它不起作用。 GridViewTest中的元素。
如果我使用
GridViewTest.DataSource = MyElementList;
GridViewTest.DataBind();
GridViewTest.Sort("FirstName", SortDirection.Ascending);
我有一个例外(我不明白为什么)。
3)第三个问题(以有效的方式解决):
我有方法
protected void details_Click(object sender, EventArgs e)
{
GridViewRow grdrow = (GridViewRow)((LinkButton)sender).NamingContainer;
}
这里我不能使用grdrow.Cells [i] .Text,其中i = 0,1,4因为结果是&#34;&#34;。我需要在另一个索引中找到MyElementList中的这个元素(因为它有另一个关于gridview的顺序)。
有没有办法在myelementlist中拥有该值而不是搜索?
一般来说,有没有更好的方法在同一个gridview中使用这三个东西?