【问题标题】:c# - How to store ArrayList in ViewState?c# - 如何在 ViewState 中存储 ArrayList?
【发布时间】:2013-08-28 10:52:50
【问题描述】:

我想在 ViewState 中存储 ArrayList 值

但我收到一个错误:

"Type 'System.Data.DataRow' in Assembly 'System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable"

代码:

  private void bindGridView()
    {
        DbConnection.Open();
        OleDbCommand DbCommand = new OleDbCommand("select emp_id,emp_name,father_name,gender,designation,department,location from emp_master", DbConnection);
        OleDbDataAdapter Dbreader1 = new OleDbDataAdapter(DbCommand);
        DataSet dsemer = new DataSet();

        Dbreader1.Fill(dsemer);
        ArrayList arrList = new ArrayList();
        foreach (DataRow dtRow in dsemer.Tables[0].Rows)
        {
            arrList.Add(dtRow);
        }
        //Here getting an error
        ViewState["ds"] = arrList;
        EmpMasterGrid.DataSource = dsemer;
        EmpMasterGrid.DataBind();

        DbConnection.Close();
    }

为什么我需要将 ArrayList 存储在 ViewState 中?

通过使用该 ViewState,我将使用以下代码将选定的 gridview 列导出到 excel

 private void GetCheckBoxStates()
    {
        CheckBox chkCol0 = (CheckBox)EmpMasterGrid.HeaderRow.Cells[0]
                                .FindControl("chkCol0");
        CheckBox chkCol1 = (CheckBox)EmpMasterGrid.HeaderRow.Cells[0]
                                .FindControl("chkCol1");
        CheckBox chkCol2 = (CheckBox)EmpMasterGrid.HeaderRow.Cells[0]
                                .FindControl("chkCol2");
        CheckBox chkCol3 = (CheckBox)EmpMasterGrid.HeaderRow.Cells[0]
                                .FindControl("chkCol3");
        CheckBox chkCol4 = (CheckBox)EmpMasterGrid.HeaderRow.Cells[0]
                                .FindControl("chkCol4");
        CheckBox chkCol5 = (CheckBox)EmpMasterGrid.HeaderRow.Cells[0]
                                .FindControl("chkCol5");
        CheckBox chkCol6 = (CheckBox)EmpMasterGrid.HeaderRow.Cells[0]
                                .FindControl("chkCol6");
        ArrayList arr;

        if (ViewState["ds"] == null)
        {
            arr = new ArrayList();
        }
        else
        {
            arr = (ArrayList)ViewState["ds"];
        }
        arr.Add(chkCol0.Checked);
        arr.Add(chkCol1.Checked);
        arr.Add(chkCol2.Checked);
        arr.Add(chkCol3.Checked);
        arr.Add(chkCol4.Checked);
        arr.Add(chkCol5.Checked);
        arr.Add(chkCol6.Checked);
        ViewState["ds"] = arr;
    }

有什么想法吗?提前致谢。 已编辑!

【问题讨论】:

    标签: c# asp.net excel gridview arraylist


    【解决方案1】:

    由于错误 sais System.Data.DataRow 不可序列化,因此您无法将其存储在 ViewState 中。

    我建议您创建一个标记为Serializable 的自定义类,其中包含您需要的所有值,并将这些对象的ArrayList 保存在ViewState 中。您将使用来自DataRow 的数据在您的foreach 循环中填充这些对象。

    例子:

    数据对象

    [Serializable]
    public class Employee
    {
        public int emp_id,
        public string emp_name,
        //other properties
    }
    

    在您的 bindGridView 方法中

    //other code
    ArrayList arrList = new ArrayList();
    foreach (DataRow dtRow in dsemer.Tables[0].Rows)
    {
        arrList.Add(new Employee
        {
            emp_id = dtRow[0],
            emp_name = dtRow[1],
            //other properties
        };
    }
    ViewState["ds"] = arrList;
    

    【讨论】:

    • 如何在下面的行 arr.Add(chkCol0.Checked); 中添加数据集值而不是这个数组列表;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    相关资源
    最近更新 更多