Gridview 排序、更新和编辑
向大家致以节日的问候。我真的需要一些帮助,因为这让我发疯。
目标。我有一个文本框,客户输入一个数字,他们单击单选按钮列表来选择材料,然后单击按钮来填充网格视图。
这是前端代码:
<asp:TextBox ID="tbxHowMany" runat="server"
style="z-index: 1; left: 300px; top: 250px; position: absolute"></asp:TextBox>
<asp:Button ID="btnDisplayTopReport" runat="server"
style="z-index: 1; left: 645px; top: 285px; position: absolute; height: 25px; width: 170px"
Text="Display TOP Report" onclick="btnDisplayTopReport_Click" />
<asp:RadioButtonList ID="radTOP" runat="server"
style="z-index: 1; left: 575px; top: 180px; position: absolute; height: 177px; width: 86px">
<asp:ListItem>Paper</asp:ListItem>
<asp:ListItem>Glass</asp:ListItem>
<asp:ListItem>Aluminium</asp:ListItem>
<asp:ListItem>Steel</asp:ListItem>
<asp:ListItem>Plastic</asp:ListItem>
<asp:ListItem>Wood</asp:ListItem>
<asp:ListItem>Other</asp:ListItem>
</asp:RadioButtonList>
<asp:LinqDataSource ID="LQTOPReportDS" runat="server"
ContextTypeName="CompleteWeightsDataContext"
EnableUpdate="True" TableName="tblOnlineReportingCOMPLETEWeights"
Where="IDDesc == @IDDesc && UnitUserfield1 == @UnitUserfield1 && UnitUserfield2 == @UnitUserfield2 && MaterialLevel == @MaterialLevel"
OrderBy="UnitId, MaterialLevel, MaterialText, UnitWeight"
StoreOriginalValuesInViewState="True">
</asp:LinqDataSource>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" CellPadding="4"
DataKeyNames="PriKey" DataSourceID="LQTOPReportDS" ForeColor="#333333"
GridLines="None" Font-Size="X-Small"
style="z-index: 1; left: 5px; top: 375px; position: absolute; height: 133px; width: 187px"
onpageindexchanging="GridView1_PageIndexChanging"
onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating"
onsorting="GridView1_Sorting">
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="UnitId" HeaderText="UnitId"
SortExpression="UnitId" />
<asp:BoundField DataField="UnitDescription" HeaderText="UnitDescription"
SortExpression="UnitDescription" />
<asp:BoundField DataField="PackagingGroupId" HeaderText="PackagingGroupId"
SortExpression="PackagingGroupId" />
<asp:CheckBoxField DataField="IsPackagingGroup" HeaderText="IsPackagingGroup"
SortExpression="IsPackagingGroup" />
<asp:BoundField DataField="PackagingTypeCode" HeaderText="PackagingTypeCode"
SortExpression="PackagingTypeCode" />
<asp:BoundField DataField="UnitWeight" HeaderText="UnitWeight"
SortExpression="UnitWeight" />
<asp:BoundField DataField="WeightUnitCode" HeaderText="WeightUnitCode"
SortExpression="WeightUnitCode" />
<asp:BoundField DataField="MaterialLevel" HeaderText="MaterialLevel"
SortExpression="MaterialLevel" />
<asp:BoundField DataField="MaterialText" HeaderText="MaterialText"
SortExpression="MaterialText" />
<asp:BoundField DataField="ProductPercentage" HeaderText="ProductPercentage"
SortExpression="ProductPercentage" />
<asp:BoundField DataField="UnitUserfield2" HeaderText="UnitUserfield2"
SortExpression="UnitUserfield2" />
<asp:BoundField DataField="Comment" HeaderText="Comment"
SortExpression="Comment" />
</Columns>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
这是后面的代码:
public partial class TOP : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["MemberKey"] = "FM00012";
GridView1.DataSourceID = null;
GridView1.DataBind();
}
}
private object GetMaterialData(string MemberKey, string MaterialType, string MaterialLevel, int Count)
{
CompleteWeightsDataContext db = new CompleteWeightsDataContext();
var query = db.tblOnlineReportingCOMPLETEWeights
.Where(x => x.MemberId == MemberKey && x.MaterialText == MaterialType && x.MaterialLevel == MaterialLevel)
.OrderByDescending(x => x.ProductPercentage)
.Take(Count);
return query;
}
protected void btnDisplayTopReport_Click(object sender, EventArgs e)
{
GridView1.DataSourceID = null;
GridView1.DataBind();
if (radTOP.SelectedValue == "" || tbxHowMany.Text == "")
{
MessageBox.Show("Please Ensure that BOTH 'The Number of Products' and Appropriate material Is selected Before You Attempt To Run a TOP X Report", "Top X Error!!!",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
GridView1.DataSourceID = null;
GridView1.DataBind();
}
else
{
int max = 0;
if (int.TryParse(tbxHowMany.Text, out max))
{
GridView1.DataSource = GetMaterialData(Session["MemberKey"].ToString(), radTOP.SelectedItem.Value, "Primary", max);
GridView1.DataBind();
}
}
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
}
}
现在我知道我必须填充排序事件,但我已经尝试了很多组合,我已经陷入了正确的困境(因此为空!)。
我怀疑我会遇到困难,因为它用于填充 gridview 的 linq 正在使用 take,因此在运行排序时数据可能会丢失。
有人可以指出我正确的方向以及如何实现这一目标吗?同样,我知道我会在 rowupdating 和 rowediting 方面遇到类似的问题,所以如果我能提出实现预期结果的建议,我将不胜感激......事实上,欢迎您和我一起在当地喝一杯节日饮料!
seasons greetings to you all. I really need some help as this is driving me mad.
The aim. I have a textbox which a client enters a number, they click on a radio button list to select a material and hit a button to populate a gridview.
This is the front end code:
<asp:TextBox ID="tbxHowMany" runat="server"
style="z-index: 1; left: 300px; top: 250px; position: absolute"></asp:TextBox>
<asp:Button ID="btnDisplayTopReport" runat="server"
style="z-index: 1; left: 645px; top: 285px; position: absolute; height: 25px; width: 170px"
Text="Display TOP Report" onclick="btnDisplayTopReport_Click" />
<asp:RadioButtonList ID="radTOP" runat="server"
style="z-index: 1; left: 575px; top: 180px; position: absolute; height: 177px; width: 86px">
<asp:ListItem>Paper</asp:ListItem>
<asp:ListItem>Glass</asp:ListItem>
<asp:ListItem>Aluminium</asp:ListItem>
<asp:ListItem>Steel</asp:ListItem>
<asp:ListItem>Plastic</asp:ListItem>
<asp:ListItem>Wood</asp:ListItem>
<asp:ListItem>Other</asp:ListItem>
</asp:RadioButtonList>
<asp:LinqDataSource ID="LQTOPReportDS" runat="server"
ContextTypeName="CompleteWeightsDataContext"
EnableUpdate="True" TableName="tblOnlineReportingCOMPLETEWeights"
Where="IDDesc == @IDDesc && UnitUserfield1 == @UnitUserfield1 && UnitUserfield2 == @UnitUserfield2 && MaterialLevel == @MaterialLevel"
OrderBy="UnitId, MaterialLevel, MaterialText, UnitWeight"
StoreOriginalValuesInViewState="True">
</asp:LinqDataSource>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" CellPadding="4"
DataKeyNames="PriKey" DataSourceID="LQTOPReportDS" ForeColor="#333333"
GridLines="None" Font-Size="X-Small"
style="z-index: 1; left: 5px; top: 375px; position: absolute; height: 133px; width: 187px"
onpageindexchanging="GridView1_PageIndexChanging"
onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating"
onsorting="GridView1_Sorting">
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="UnitId" HeaderText="UnitId"
SortExpression="UnitId" />
<asp:BoundField DataField="UnitDescription" HeaderText="UnitDescription"
SortExpression="UnitDescription" />
<asp:BoundField DataField="PackagingGroupId" HeaderText="PackagingGroupId"
SortExpression="PackagingGroupId" />
<asp:CheckBoxField DataField="IsPackagingGroup" HeaderText="IsPackagingGroup"
SortExpression="IsPackagingGroup" />
<asp:BoundField DataField="PackagingTypeCode" HeaderText="PackagingTypeCode"
SortExpression="PackagingTypeCode" />
<asp:BoundField DataField="UnitWeight" HeaderText="UnitWeight"
SortExpression="UnitWeight" />
<asp:BoundField DataField="WeightUnitCode" HeaderText="WeightUnitCode"
SortExpression="WeightUnitCode" />
<asp:BoundField DataField="MaterialLevel" HeaderText="MaterialLevel"
SortExpression="MaterialLevel" />
<asp:BoundField DataField="MaterialText" HeaderText="MaterialText"
SortExpression="MaterialText" />
<asp:BoundField DataField="ProductPercentage" HeaderText="ProductPercentage"
SortExpression="ProductPercentage" />
<asp:BoundField DataField="UnitUserfield2" HeaderText="UnitUserfield2"
SortExpression="UnitUserfield2" />
<asp:BoundField DataField="Comment" HeaderText="Comment"
SortExpression="Comment" />
</Columns>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
And this is the code behind:
public partial class TOP : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["MemberKey"] = "FM00012";
GridView1.DataSourceID = null;
GridView1.DataBind();
}
}
private object GetMaterialData(string MemberKey, string MaterialType, string MaterialLevel, int Count)
{
CompleteWeightsDataContext db = new CompleteWeightsDataContext();
var query = db.tblOnlineReportingCOMPLETEWeights
.Where(x => x.MemberId == MemberKey && x.MaterialText == MaterialType && x.MaterialLevel == MaterialLevel)
.OrderByDescending(x => x.ProductPercentage)
.Take(Count);
return query;
}
protected void btnDisplayTopReport_Click(object sender, EventArgs e)
{
GridView1.DataSourceID = null;
GridView1.DataBind();
if (radTOP.SelectedValue == "" || tbxHowMany.Text == "")
{
MessageBox.Show("Please Ensure that BOTH 'The Number of Products' and Appropriate material Is selected Before You Attempt To Run a TOP X Report", "Top X Error!!!",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
GridView1.DataSourceID = null;
GridView1.DataBind();
}
else
{
int max = 0;
if (int.TryParse(tbxHowMany.Text, out max))
{
GridView1.DataSource = GetMaterialData(Session["MemberKey"].ToString(), radTOP.SelectedItem.Value, "Primary", max);
GridView1.DataBind();
}
}
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
}
}
Now I know I have to populate the sorting event but I have tried sooo many combinations, I have got myself in a right pickle (hence the null!).
I suspect I will have difficulty as the linq that it is using to populate the gridview is using take so the data might be lost when the sorting is run.
Can someone please point me in the right direction and how I can achieve this? Similarly, I know I will encounter similar problems with the rowupdating and rowediting so if I can have suggests for achieving the desired results, I would be most grateful...in fact, you are welcome to join me for a festive drink at my local!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不完全确定您要做什么,但通常,排序处理程序用于对网格视图中的列进行排序。您可以参考下面的示例代码,其中我按升序或降序处理排序,并且还进行缓存,这样当它们按列排序时我不必再次访问数据库。
但一般来说,您希望更新网格视图的数据源并将其与排序结果重新绑定。
I'm not totally sure what you are trying to do, but generally, a sort handler is used for sorting columns in a gridview. You can refer to this sample code below, in which I'm handling sorting by ascending or descending, and also caching so I don't have to hit the database again when they sort by a column.
But in general, you want to update the data source for the grid view and rebind it with the sorted result.