可能这很容易,但是我遇到麻烦,要为网格视图的列进行排序。想法是点击列:
<asp:BoundField ItemStyle-Width="150px" HeaderText="Available Inventory" SortExpression="availInventory" />
因此,解释如何获取gridview,以下列值是通过sql查询咨询获得的,它们绑定到gridview:Family,Recipe,Top Part Number,Generic Part Number,Child#1 Part Number,Factor Child1, Child#2 Part Number,Factor Child2,Child#3 Part Number,Factor Child3。 列Attrition,Inventory Child1,Inventory Child2,Inventory Child3和Available Inventory的值是在gridview的RowDataBound方法中计算的。 选择一些下拉列表后,将在页面加载gridview。
的GridView
<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>
sortDirection Method
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.dll中发生了'System.IndexOutOfRangeException'类型的异常,但未在用户代码中处理 附加信息:找不到列availInventory。
非常感谢任何帮助或指导。 感谢
EDIT 将gridview的取值/存储值添加到datatable中,该数据包含在RowDataBound方法中。该方法太大,所以我只发布我形成数据表的位置并定义会话变量:
//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;
答案 0 :(得分:0)
错误是:Cannot find column availInventory.
您的绑定字段是
<asp:BoundField ItemStyle-Width="150px" HeaderText="Available Inventory"
SortExpression="availInventory" />
尝试将SortExpression更改为Available Inventory
,以匹配
dt.Columns.Add("Available Inventory", typeof(Int32));
。