1

我使用列表绑定了一个gridview

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);
            }
        }

有没有其他方法可以将空控件绑定到 gridview 并检索值?

此行中的错误

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 回答