在Gridview中排序订单下拉列表

时间:2013-02-14 10:32:18

标签: asp.net c#-3.0 webforms

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

例如:

ID        Sort Order
001          1
002          2
003          3
004          4

在我更改其中一行的下拉列表中的值后 - 例如,我会将ID 002的排序顺序下拉列表从2更改为3 - gridview应该像这样更新:

 ID        Sort Order
001          1
003          2
002          3
004          4

我需要逻辑来在下拉列表的SelectedIndexChanged事件中完成此操作,以及执行数据库更新的代码。

1 个答案:

答案 0 :(得分: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>