数据源类型无效.它必须是IListSource、IEnumerable或IDataSource

本文关键字:IListSource IEnumerable IDataSource 类型 无效 数据源 | 更新日期: 2023-09-27 18:13:19

我正试图从gridview中填充一个详细信息视图,但我得到了一个错误,请帮助我

public void viewDetailsNew(decimal decDv)
    {
        SubjectInfo infosubject = new SubjectInfo();
        SubjectSp spcubject = new SubjectSp();
        dvSubject.DataSource = spcubject.SubjectViewDetails(decDv);
        dvSubject.DataBind();
        mvSubject.ActiveViewIndex = 1;
    }
public SubjectInfo SubjectViewDetails(decimal decsubjectid)
    {
        SubjectInfo infosubject = new SubjectInfo();
        SqlDataReader sqlreader = null;
        try
        {
            if (sqlcon.State == ConnectionState.Closed)
            {
                sqlcon.Open();
            }
            SqlCommand sqlcmd = new SqlCommand("SubjectView", sqlcon);
            sqlcmd.CommandType = CommandType.StoredProcedure;
            sqlcmd.Parameters.Add("@subjectId", SqlDbType.Decimal).Value = decsubjectid;
            sqlreader = sqlcmd.ExecuteReader();
            while (sqlreader.Read())
            {
                infosubject.subjectId = decimal.Parse(sqlreader["subjectId"].ToString());
                infosubject.subjectName = sqlreader["subjectName"].ToString();
                infosubject.shortName = sqlreader["shortName"].ToString();
            }
        }
        catch
        {
            throw;
        }
        finally
        {
            sqlreader.Close();
            sqlcon.Close();
        }
        return infosubject;
    }
protected void gvViewSubject_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        ViewState["subjectId"] = gvViewSubject.DataKeys[e.NewSelectedIndex]["subjectId"].ToString();
        decimal decDv = decimal.Parse(ViewState["subjectId"].ToString());
        viewDetailsNew(decDv);
    }

数据源类型无效.它必须是IListSource、IEnumerable或IDataSource

当您需要一个集合或IEnumerable

时,您正在尝试绑定到单个对象

试试这个

 public IEnumerable<SubjectInfo> SubjectViewDetails(decimal decsubjectid)
{
    var list = new List<SubjectInfo>();
    try
    {
        if (sqlcon.State == ConnectionState.Closed)
        {
            sqlcon.Open();
        }
        SqlCommand sqlcmd = new SqlCommand("SubjectView", sqlcon);
        sqlcmd.CommandType = CommandType.StoredProcedure;
        sqlcmd.Parameters.Add("@subjectId", SqlDbType.Decimal).Value = decsubjectid;
        using (var sqlreader = sqlcmd.ExecuteReader())
        {
            while (sqlreader.Read())
            {
                SubjectInfo infosubject = new SubjectInfo();
                infosubject.subjectId = decimal.Parse(sqlreader["subjectId"].ToString());
                infosubject.subjectName = sqlreader["subjectName"].ToString();
                infosubject.shortName = sqlreader["shortName"].ToString();
                list.Add(infosubject);
            }
        }
    }
    catch
    {
        throw;
    }
    finally
    {
        sqlcon.Close();
    }
    return list;
}