2

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

例如:

ID        Sort Order
001          1
002          2
003          3
004          4

在我更改其中一行的下拉列表中的值后 - 例如,我将 ID 的排序顺序下拉列表002从更改23- 网格视图应该像这样更新:

 ID        Sort Order
001          1
003          2
002          3
004          4

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

4

1 回答 1

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>
于 2017-01-02T10:51:25.767 回答