2013-02-14 34 views
2

我有一个Gridview控件,有两列:一个是ID(标签),另一个是排序顺序(下拉列表)。下拉列表编号从1到n,其中n是Gridview中的行数。Gridview中的排序顺序下拉

例如:

ID  Sort Order 
001   1 
002   2 
003   3 
004   4 

我改变的下拉列表中值后的行之一 - 例如,我会为ID 00223更改排序顺序下拉 - 在GridView应更新这样的:

ID  Sort Order 
001   1 
003   2 
002   3 
004   4 

我需要的逻辑来SelectedIndexChanged事件下拉内做到这一点,以及代码执行更新到数据库。

+0

排序顺序依然存在1 2 3 4 ?? – Sakthivel 2013-02-14 10:37:05

+0

@codebrain,你会注意到'ID' 002不再有'排序顺序'为2现在是3 – glh 2013-02-14 11:30:11

+0

你能给我们提供更多的信息吗?数据如何绑定到GridView,以及模型是什么样子的? – 2013-12-03 20:29:44

回答

0

如果我理解正确的话那么你的问题,找到你的问题的解决方案如下:

代码隐藏

public partial class _Default : System.Web.UI.Page 
    { 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!Page.IsPostBack) 
     { 
      BindGrid(); 
     } 
    } 

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      DropDownList objDropDownList = e.Row.FindControl("DropDownList1") as DropDownList; 
      objDropDownList.DataSource = CustomSorting.GetAll(); 
      objDropDownList.DataTextField = "SOText"; 
      objDropDownList.DataValueField = "SOID"; 
      objDropDownList.DataBind(); 

      HiddenField objHiddenField = e.Row.FindControl("HiddenField1") as HiddenField; 
      string currSortOrder = (!string.IsNullOrEmpty(objHiddenField.Value) ? objHiddenField.Value : 
    "0"); 
      objDropDownList.SelectedIndex = objDropDownList.Items.IndexOf(objDropDownList.Items.FindByValue(currSortOrder)); 
     } 
    } 

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     DropDownList objDropDownList = sender as DropDownList; 
     string test = objDropDownList.SelectedValue; 

     GridViewRow currRow = objDropDownList.NamingContainer as GridViewRow; 
     Label objLabel = currRow.FindControl("Label1") as Label; 
     HiddenField objHiddenField = currRow.FindControl("HiddenField1") as HiddenField; 

     List<TestClass> lstTestClass = PageData; 
     if (lstTestClass.Exists(x => x.SortOrder == Convert.ToInt32(test))) 
      lstTestClass.Find(x => x.SortOrder == Convert.ToInt32(test)).SortOrder = 
    Convert.ToInt32(objHiddenField.Value); 
     if (lstTestClass.Exists(x => x.ID == objLabel.Text)) 
      lstTestClass.Find(x => x.ID == objLabel.Text).SortOrder = Convert.ToInt32(test); 
     PageData = lstTestClass; 
     BindGrid(); 
    } 

    protected List<TestClass> PageData 
    { 
     get 
     { 
      return (Session["_PageData"] == null) ? TestClass.GetAll() : Session["_PageData"] as List<TestClass>; 
     } 
     set 
     { 
      Session["_PageData"] = value; 
     } 
    } 
    protected void BindGrid() 
    { 
     GridView1.DataSource = PageData.OrderBy(x=>x.SortOrder); 
     GridView1.DataBind(); 
    } 
} 

public class CustomSorting 
{ 
    public int SOID { get; set; } 
    public string SOText { get; set; } 

    public static List<CustomSorting> GetAll() 
    { 
     return new List<CustomSorting>(){ 
      new CustomSorting(){SOID=1,SOText="1"}, 
      new CustomSorting(){SOID=2,SOText="2"}, 
      new CustomSorting(){SOID=3,SOText="3"}, 
      new CustomSorting(){SOID=4,SOText="4"}, 
     }; 
    } 
} 

public class TestClass 
{ 
    public string ID { get; set; } 
    public int SortOrder { get; set; } 

    public static List<TestClass> GetAll() 
    { 
     return new List<TestClass>(){ 
      new TestClass(){ID="001",SortOrder=1}, 
      new TestClass(){ID="002",SortOrder=2}, 
      new TestClass(){ID="003",SortOrder=3}, 
      new TestClass(){ID="004",SortOrder=4} 
     }.OrderBy(x=>x.SortOrder).ToList(); 
    } 
} 

ASPX变化:

<asp:GridView ID="GridView1" runat="server" 
OnRowDataBound="GridView1_RowDataBound" AutoGenerateColumns="False"> 
    <Columns> 
     <asp:TemplateField HeaderText="ID"> 
      <ItemTemplate> 
       <asp:Label ID="Label1" runat="server" Text='<%# Eval("ID")%>'></asp:Label> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Sort Order"> 
      <ItemTemplate> 
       <asp:HiddenField ID="HiddenField1" runat="server"  Value='<%#Eval("SortOrder") %>' /> 
       <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"></asp:DropDownList> 
      </ItemTemplate> 
     </asp:TemplateField> 
    </Columns> 
</asp:GridView>