无法将类型 'System.Data.DataRowView' 的对象转换为类型 'QuestionBuilder'

Unable to cast object of type 'System.Data.DataRowView' to type 'QuestionBuilder'

我在尝试投射到我的 class 时得到以下信息: 无法将类型 'System.Data.DataRowView' 的对象转换为类型 'QuestionBuilder'。

问题描述:当前网络请求执行过程中出现未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。

Line 204:            else  if(e.Row.RowType== DataControlRowType.DataRow)
Line 205:            {
Line 206:                QuestionBuilder drv = (QuestionBuilder)e.Row.DataItem;
Line 207:
Line 208:            }

MY CLASS:

============================================= ======

 [Serializable]
        public class QuestionBuilder
        {
            /// Field for connection string
            /// </summary>
            private string connectionstring = Connection.GetConnecionString();

            public string QuestionText
            {
                get;
                set;
            }


            public string DataTypeId
            {
                get;
                set;
            }

            public string DynamicAttributeId
            {
                get;
                set;
            }

            public object DynamicValue
            {
                get;
                set;
            }

            public string SortOrder
            {
                get;
                set;
            }

            public string ProjectID
            {
                get;
                set;
            }

            public string DataTypeName
            {
                get;
                set;
            }

            public bool QuestionID
            {
                get;
                set;
            }


            public int surveyID
            {
                get;
                set;
            }

            public string ID
            {
                get;
                set;
            }

            public string dateteupdated
            {
                get;
                set;
            }

            public string recordstatus
            {
                get;
                set;
            }



            public string modifiedby
            {
                get;
                set;
            }


            public string changereason
            {
                get;
                set;
            }

            public bool Required
            {
                get;
                set;
            }
            public string SectionName
            {
                get;
                set;
            }

            public int SectionID
            {
                get;
                set;
            }

            public string FormType
            {
                get;
                set;
            }
            public int DecisionsetID
            {
                get;
                set;
            }

            public int SurveyID
            {
                get;
                set;
            }
    =========================================================

      if (e.Row.RowType == DataControlRowType.Header)
                {
                    foreach (TableCell tc in e.Row.Cells)
                    {
                        tc.CssClass = "casetableheaderstudyList";
                    }
                }
                else  if(e.Row.RowType== DataControlRowType.DataRow)
                {
                    QuestionBuilder drv = QuestionBuilder)e.Row.DataItem;

                }
                else
                {
                    foreach (TableCell tc in e.Row.Cells)
                    {
                        tc.BorderStyle = BorderStyle.None;
                        tc.BorderWidth = 0;
                        //tc.BorderColor = System.Drawing.Color.Transparent;
                        tc.CssClass = "casetableColumsStudyList";
                    }
                }

如果没有完整的映射细节和数据绑定代码,很难将问题可视化。

但是,根据您的 post,这似乎是数据映射问题。当相关列的 DataField 值与基础源列名称不匹配时,可能会发生这种情况。

例如:如果将 DataKeyNames 属性 指定为 ID,而实际的列名称是 CustomerID。它会抛出上面的错误。

请使用此上下文重新访问您的代码,看看是否有帮助。