数据源是无效类型。它必须是IListSource,IEnumerable或IDataSource

时间:2013-05-14 04:16:03

标签: c# asp.net gridview multiview

我正在尝试从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);
    }

1 个答案:

答案 0 :(得分:0)

您正在尝试在需要集合或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;
}