1

リストを使用してグリッドビューをバインドしました

private void BindEntirePropertyGrid()
{
    List<Tbl_RoomMaster> items = new List<Tbl_RoomMaster>();
    for (int i = 0; i < Convert.ToInt32(bedrooms.Text); i++)
    {
        items.Add(objRoomMaster);
    }
    ViewState["GridView1"] = items;
    GridView1.DataSource = items;
    GridView1.DataBind();

}

今、私は値を取得しようとしましたが、エラーが表示されます

if (ViewState["GridView1"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["GridView1"];
            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {
                    //extract the TextBox values
                    Label box1 = (Label)GridView1.Rows[rowIndex].Cells[1].FindControl("lblbedroom");
                    DropDownList box2 = (DropDownList)GridView1.Rows[rowIndex].Cells[1].FindControl("ddpAccomodates");
                    DropDownList box3 = (DropDownList)GridView1.Rows[rowIndex].Cells[1].FindControl("ddpBathroom");
                    DropDownList box4 = (DropDownList)GridView1.Rows[rowIndex].Cells[1].FindControl("ddpBedType");
                    //get the values from the TextBoxes
                    //then add it to the collections with a comma "," as the delimited values
                    sc.Add("BedRoom" + box1.Text + "," + box2.SelectedItem.Value + "," + box3.SelectedItem.Value + "," + box4.SelectedItem.Value);
                    rowIndex++;
                }
                //Call the method for executing inserts
                InsertRoomDetails(sc, propIDreturnedOnSave);
            }
        }

空のコントロールをグリッドビューにバインドして値を取得する他の方法はありますか?

この行のエラー

DataTable dtCurrentTable =(DataTable)ViewState ["GridView1"];

Unable to cast object of type 'System.Collections.Generic.List`1[CT.Bussiness.Tbl_RoomMaster]' to type 'System.Data.DataTable'.
4

1 に答える 1

1

ViewState["GridView1"]の内容はDataTableではありません。

変化する

DataTable dtCurrentTable = (DataTable)ViewState["GridView1"];

List<Tbl_RoomMaster> dtCurrentTable = (List<Tbl_RoomMaster>)ViewState["GridView1"];

また、変更する必要があります

if (dtCurrentTable.Rows.Count > 0)
        {
            for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)

if (dtCurrentTable.Count > 0)
        {
            for (int i = 1; i <= dtCurrentTable.Count; i++)
于 2012-06-20T08:03:52.150 に答える