0

即使我的列表不为空,我也没有将对象引用设置为对象的实例

  protected void gridview_bind()
{
    try
    {
        CompanyMasterClass co = new CompanyMasterClass();
        co.strcompany_code = Request.Cookies["userinfo"]["companycode"].ToString();
        ResultClass objress = co.fn_GetNameNumberStyle();
        if (objress.bStatus)
        {
            eslist<CompanyMasterClass> OBJLISTS = objress.objData as eslist<CompanyMasterClass>;

            if (OBJLISTS.Count > 0)
            {
                if (OBJLISTS[0].strdatestyle.ToString() == "DM")
                {
                    mskextCreatedOn.Mask = "99/99/9999";
                    mskextCreatedOn.CultureName = "en-GB";
                    //mskextCreatedOn.UserDateFormat="DayMonthYear";
                    mskextUpdatedOn.Mask = "99/99/9999";
                    mskextUpdatedOn.CultureName = "en-GB";
                }
                if (OBJLISTS[0].strdatestyle.ToString() == "MD")
                {
                    mskextCreatedOn.Mask = "99/99/9999";
                    mskextCreatedOn.CultureName = "en-US";
                    //mskextCreatedOn.UserDateFormat="DayMonthYear";
                    mskextUpdatedOn.Mask = "99/99/9999";
                    mskextUpdatedOn.CultureName = "en-US";
                }
            }
        }
        //mm.getlookup(ddlUserType, "user_type", "s");
        CustomerMasterClass cs = new CustomerMasterClass();

        ResultClass objres = cs.fn_GetCustomerMasterGrid();
        if (objres.bStatus)
        {
            eslist<CustomerMasterClass> OBJLIST = objres.objData as eslist<CustomerMasterClass>;
            if (OBJLIST.Count > 0)
            {
                if ((bool)ViewState["filter"] == true)
                {
                    DataTable dts = (DataTable)ViewState["searchtable"];
                    pds.DataSource = dts.DefaultView;
                    pds.AllowPaging = true;
                    pds.PageSize = Convert.ToInt16(ddlPerPage.SelectedValue);
                    //pds.PageSize = 5;
                    DataView dvs = dts.DefaultView;

                    if (this.ViewState["SortExp"] != null)
                    {
                        dvs.Sort = this.ViewState["SortExp"].ToString()
                                 + " " + this.ViewState["SortOrder"].ToString();
                    }
                    pds.CurrentPageIndex = CurrentPage;
                    lnkbtnNext.Enabled = !pds.IsLastPage;
                    lnkbtnPrevious.Enabled = !pds.IsFirstPage;
                    if (pds.IsLastPage)
                    {
                        //lnkbtnNext.Style.Add("color", "#CCCCCC");
                        lnkbtnNext.Style.Add("background-color", "#CCCCCC");
                        lnkbtnNext.Style.Add("border-width", "0px");
                    }
                    else
                    {
                        //lnkbtnNext.Style.Remove("color");
                        lnkbtnNext.Style.Remove("background-color");
                        lnkbtnNext.Style.Remove("border-width");
                    }
                    if (pds.IsFirstPage)
                    {
                        //lnkbtnPrevious.Style.Add("color", "#CCCCCC");
                        lnkbtnPrevious.Style.Add("background-color", "#CCCCCC");
                        lnkbtnPrevious.Style.Add("border-width", "0px");
                    }
                    else
                    {
                        //lnkbtnPrevious.Style.Remove("color");
                        lnkbtnPrevious.Style.Remove("background-color");
                        lnkbtnPrevious.Style.Remove("border-width");
                    }
                    gvCustomerMaster.DataSource = pds;
                    gvCustomerMaster.DataBind();
                    doPaging();
                    // gvGroupMaster.DataSource = OBJLIST;
                    //gvGroupMaster.DataBind();
                    gridmain.Visible = true;
                    //frmvwGroup.Visible = false;
                    lblTotalRecords.Text = dts.Rows.Count.ToString();

                }
                else
                {
                        DataTable dt = (DataTable)OBJLIST;
                        //dt = OBJLIST.ToString();
                        ViewState["nosearch"] = dt;
                        pds.DataSource = dt.DefaultView;
                        pds.AllowPaging = true;
                        pds.PageSize = Convert.ToInt16(ddlPerPage.SelectedValue);
                        //pds.PageSize = 5;
                        DataView dv = dt.DefaultView;

                        if (this.ViewState["SortExp"] != null)
                        {
                            dv.Sort = this.ViewState["SortExp"].ToString()
                                     + " " + this.ViewState["SortOrder"].ToString();
                        }
                        pds.CurrentPageIndex = CurrentPage;
                        lnkbtnNext.Enabled = !pds.IsLastPage;
                        lnkbtnPrevious.Enabled = !pds.IsFirstPage;
                        if (pds.IsLastPage)
                        {
                            //lnkbtnNext.Style.Add("color", "#CCCCCC");
                            lnkbtnNext.Style.Add("background-color", "#CCCCCC");
                            lnkbtnNext.Style.Add("border-width", "0px");
                        }
                        else
                        {
                            //lnkbtnNext.Style.Remove("color");
                            lnkbtnNext.Style.Remove("background-color");
                            lnkbtnNext.Style.Remove("border-width");
                        }
                        if (pds.IsFirstPage)
                        {
                            //lnkbtnPrevious.Style.Add("color", "#CCCCCC");
                            lnkbtnPrevious.Style.Add("background-color", "#CCCCCC");
                            lnkbtnPrevious.Style.Add("border-width", "0px");
                        }
                        else
                        {
                            //lnkbtnPrevious.Style.Remove("color");
                            lnkbtnPrevious.Style.Remove("background-color");
                            lnkbtnPrevious.Style.Remove("border-width");
                        }
                        gvCustomerMaster.DataSource = pds;
                        gvCustomerMaster.DataBind();
                        doPaging();
                        // gvGroupMaster.DataSource = OBJLIST;
                        //gvGroupMaster.DataBind();
                        gridmain.Visible = true;
                        //frmvwGroup.Visible = false;
                        lblTotalRecords.Text = OBJLIST.Count.ToString();
                        //this.GetUsersPageWise(1);

                }
            }
        }
    }
    catch (Exception ex)
    {
        ErrorLogClass ObjError = new ErrorLogClass("customer_master.aspx", "customer_master()", ex);
    }
}

我在 DataTable dt = (DataTable)OBJLIST; 在 else 子句中。

请问有什么想法或建议吗?

谢谢,

4

2 回答 2

1

数据有问题。一些值为空的值。通过为数据变量分配默认值来解决。

于 2012-07-09T06:21:21.243 回答
0

您不能像这样将列表转换为数据表。要么使用

static DataTable ConvertListToDataTable(eslist<CompanyMasterClass> list)
{
    // New table.
    DataTable table = new DataTable();

    // Get max columns.
    int columns = 0;
    foreach (var array in list)
    {
    if (array.Length > columns)
    {
        columns = array.Length;
    }
    }

    // Add columns.
    for (int i = 0; i < columns; i++)
    {
    table.Columns.Add();
    }

    // Add rows.
    foreach (var array in list)
    {
    table.Rows.Add(array);
    }

    return table;
}

或参考以下链接 http://www.codeproject.com/Articles/83271/Convert-a-Generic-List-to-a-Datatable

于 2012-06-06T09:57:47.103 回答