可能这很容易,但我在为网格视图的列进行排序时遇到了麻烦。这个想法是点击列:
<asp:BoundField ItemStyle-Width="150px" HeaderText="Available Inventory" SortExpression="availInventory" />
因此,解释网格视图的来源,通过 sql 查询咨询以下列值获取,并将它们绑定到 gridview:系列、配方、顶部部件号、通用部件号、子部件号#1 部件号、因子子项 1、子项 #2 部件号、因子子项 2、子项 #3 部件号、因子子项 3。 列损耗、库存子列 1、库存子列 2、库存子列 3 和可用库存在网格视图的行数据绑定方法中计算。 网格视图在选择一些下拉列表后在页面上加载。
网格视图
<asp:GridView ID="recipegrid" runat="server" Width="1200px" Height="152px" AutoGenerateColumns="false" AllowSorting="true" OnSorting="recipegrid_Sorting" CssClass="generalgrid" AllowPaging="true" OnPageIndexChanging="OnPaging" PageSize="50" OnRowDataBound="recipegrid_RowDataBound" OnSelectedIndexChanged="recipegrid_SelectedIndexChanged" HeaderStyle-CssClass="gridHeader" RowStyle-CssClass="gridRows" PagerStyle-CssClass="paginationRow" OnDataBound="recipegrid_DataBound">
<Columns>
<asp:BoundField ItemStyle-Width="150px" DataField="Family" HeaderText="Family" />
<asp:BoundField ItemStyle-Width="150px" DataField="RecipePN" HeaderText="Recipe" />
<asp:BoundField ItemStyle-Width="150px" DataField="TopPN" HeaderText="Top Part Number" />
<asp:BoundField ItemStyle-Width="150px" DataField="GenericPN" HeaderText="Generic Part Number" />
<asp:BoundField ItemStyle-Width="150px" HeaderText="Attrition" />
<asp:BoundField ItemStyle-Width="150px" DataField="Child1PN" HeaderText="Child#1 Part Number" />
<asp:BoundField ItemStyle-Width="150px" DataField="FactorChild1" HeaderText="Factor Child1" />
<asp:BoundField ItemStyle-Width="150px" HeaderText="Inventory Child1" />
<asp:BoundField ItemStyle-Width="150px" DataField="Child2PN" HeaderText="Child#2 Part Number" />
<asp:BoundField ItemStyle-Width="150px" DataField="FactorChild2" HeaderText="Factor Child2" />
<asp:BoundField ItemStyle-Width="150px" HeaderText="Inventory Child2" />
<asp:BoundField ItemStyle-Width="150px" DataField="Child3PN" HeaderText="Child#3 Part Number" />
<asp:BoundField ItemStyle-Width="150px" DataField="FactorChild3" HeaderText="Factor Child3" />
<asp:BoundField ItemStyle-Width="150px" HeaderText="Inventory Child3" />
<asp:BoundField ItemStyle-Width="150px" HeaderText="Available Inventory" SortExpression="availInventory" />
<asp:TemplateField HeaderText="Status" Visible="false" ItemStyle-Width="150px">
<ItemTemplate>
<asp:Label ID="lblStatus" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Select Data" ItemStyle-Width="150px" >
<ItemTemplate>
<asp:RadioButton ID="recipeselector" runat="server" AutoPostBack="true" OnCheckedChanged="rbtnSelect_CheckedChanged" />
<!-- <asp:HiddenField ID="HiddenField1" runat="server" /> -->
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate><label class="labelemptygriddata"> No data loaded from DB!</label> </EmptyDataTemplate>
</asp:GridView>
排序方向方法
private string sortDirectionMethod (SortDirection sortDirection)
{
string newSortDirection = String.Empty;
switch (sortDirection)
{
case SortDirection.Ascending:
newSortDirection = "ASC";
break;
case SortDirection.Descending:
newSortDirection = "DESC";
break;
}
return newSortDirection;
}
分拣方法
protected void recipegrid_Sorting(object sender, GridViewSortEventArgs e)
{
//DataTable dt = recipegrid.DataSource as DataTable;
DataTable dt = new DataTable();
//Session["grid"] = recipegrid.DataSource;
dt = (DataTable)Session["grid"];
if (dt != null)
{
DataView dv = new DataView(dt);
dv.Sort = e.SortExpression + " " + sortDirectionMethod(e.SortDirection);
recipegrid.DataSource = dv;
recipegrid.DataBind();
}
}
所以问题是排序不起作用,因为我收到以下错误:
System.Data 中发生类型为"System.IndexOutOfRangeException"的异常.dll但未在用户代码中处理 其他信息:找不到可用列库存。
任何帮助或指导将不胜感激。 谢谢
编辑 将获取/存储网格视图值的代码添加到包含在 RowDataBound 方法中的 DataTable 中。该方法太大,所以我只在形成数据表和定义会话变量的位置发布:
//copy info from gridview to session
//need to add each column name and type
DataTable dt = new DataTable();
DataRow dr;
//Boolean noCheck = false;
//column Family
dt.Columns.Add("Family", typeof(string));
//column Recipe
dt.Columns.Add("Recipe", typeof(string));
//column Top Part Number
dt.Columns.Add("Top Part Number", typeof(string));
//column Generic Part Number
dt.Columns.Add("Generic Part Number", typeof(string));
//column Attrition
dt.Columns.Add("Attrition", typeof(string));
//column child#1 Part Number
dt.Columns.Add("Child#1 Part Number", typeof(string));
//column Factor Child1
dt.Columns.Add("Factor Child1", typeof(string));
//column Inventory Child1
dt.Columns.Add("Inventory Child1", typeof(string));
//column Child#2 Part Number
dt.Columns.Add("Child#2 Part Number", typeof(string));
//column Factor Child2
dt.Columns.Add("Factor Child2", typeof(string));
//column Inventory Child2
dt.Columns.Add("Inventory Child2", typeof(string));
//column Child#3 Part Number
dt.Columns.Add("Child#3 Part Number", typeof(string));
//column Factor Child3
dt.Columns.Add("Factor Child3", typeof(string));
//column Inventory Child3
dt.Columns.Add("Inventory Child3", typeof(string));
//column Available Inventory
dt.Columns.Add("Available Inventory", typeof(Int32));
//column select data
dt.Columns.Add("Select Data", typeof(string));
foreach (GridViewRow row in recipegrid.Rows)
{
System.Web.UI.WebControls.RadioButton selectData = (System.Web.UI.WebControls.RadioButton)row.FindControl("recipeselector");
dr = dt.NewRow();
//family
dr[0] = row.Cells[0].Text;
//recipe
dr[1] = row.Cells[1].Text;
//top partnumber
dr[2] = row.Cells[2].Text;
//generic partnumber
dr[3] = row.Cells[3].Text;
//attrition
dr[4] = row.Cells[4].Text;
//child#1 partnumber
dr[5] = row.Cells[5].Text;
//factor child1
dr[6] = row.Cells[6].Text;
//inventory child1
dr[7] = row.Cells[7].Text;
//child#2 partnumber
dr[8] = row.Cells[8].Text;
//factor child2
dr[9] = row.Cells[9].Text;
//inventory child2
dr[10] = row.Cells[10].Text;
//child#3 partnumber
dr[11] = row.Cells[11].Text;
//factor child3
dr[12] = row.Cells[12].Text;
//inventory child3
dr[13] = row.Cells[13].Text;
//available inventory
dr[14] = Convert.ToInt32(row.Cells[14].Text);
//select data
dr[15] = selectData.Checked = false;
dt.Rows.Add(dr);
}
Session["grid"] = dt;
错误是:Cannot find column availInventory.
绑定字段为
<asp:BoundField ItemStyle-Width="150px" HeaderText="Available Inventory"
SortExpression="availInventory" />
尝试将 SortExpression 更改为Available Inventory
以匹配
dt.Columns.Add("Available Inventory", typeof(Int32));
.