問題を正しく理解した場合は、以下のように問題の解決策を見つけてください。
コードビハインド
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>