如何用数据集填充网格视图

how to fill grid view with dataset

我已经完成了用数据集填充网格视图的代码。 看看 :

public void FillGrid(string StartAlpha, string CommandName, string ColumnName, string SearchText)
    {
        using (DataClassesDataContext db = new DataClassesDataContext())
        {
            int userid = db.Users.Where(u => u.EmailAddress.Equals((String)Session["EmailID"])).Select(u => u.Id).SingleOrDefault();
            var sms = Enumerable.Repeat(new
            {
                Id = default(int),
                Title = string.Empty,
                Body = string.Empty,
                FromUser = string.Empty,
                ToUser = string.Empty,
                SentDateTime = default(DateTime?),
                IsMedia = default(bool),
                CreatedDate = default(DateTime),
            }, 1).ToList();
            DataSet myDataSet = new DataSet();
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("Id", typeof(string)));
            dt.Columns.Add(new DataColumn("Title", typeof(string)));
            dt.Columns.Add(new DataColumn("Body", typeof(string)));
            dt.Columns.Add(new DataColumn("FromUser", typeof(string)));
            dt.Columns.Add(new DataColumn("ToUser", typeof(string)));
            dt.Columns.Add(new DataColumn("SentDateTime", typeof(DateTime)));
            dt.Columns.Add(new DataColumn("IsMedia", typeof(bool)));
            dt.Columns.Add(new DataColumn("CreatedDate", typeof(DateTime)));
            if (StartAlpha.Equals("All"))
            {
                switch (CommandName)
                {
                    case "Inbox":
                        break;
                    case "Outbox":
                        lbl_countall.Text = db.SMS.Where(s => s.SentDateTime != null).Count().ToString();
                        lbut_showinbox.Font.Bold = false;
                        lbut_showoutbox.Font.Bold = true;
                        lbut_showdraffs.Font.Bold = false;
                        sms = db.SMS.OrderByDescending(s => s.SentDateTime).Where(s => (s.SentDateTime != null || s.IsDraft.Equals(false)) & s.user_id.Equals(userid)).Select(s => new
                        {
                            Id = s.Id,
                            Title = s.Title,
                            Body = s.Body,
                            FromUser = db.SMSAccounts.Where(a=>a.user_id.Equals(userid)).Select(a=>a.FromMobileNo).FirstOrDefault(),
                            ToUser = s.To_MobileNo,
                            SentDateTime = s.SentDateTime,
                            IsMedia = false,
                            CreatedDate = s.CreatedDate
                        }).FilterForColumn(ColumnName, SearchText).ToList();
                        foreach (var item in sms)
                        {
                            if (item != null)
                            {
                                DataRow dr = dt.NewRow();
                                dr["Id"] = item.Id.ToString();
                                dr["Title"] = item.Title.ToString();
                                dr["Body"] = Regex.Replace(item.Body.ToString().Trim(), @"<(.|\n)*?>", string.Empty);
                                dr["FromUser"] = item.FromUser.ToString();
                                if (item.ToUser != null)
                                {
                                    dr["ToUser"] = item.ToUser.ToString();
                                }
                                else
                                {
                                    dr["ToUser"] = "NoN";
                                }
                                if (item.SentDateTime != null)
                                {
                                    dr["SentDatetTime"] = item.SentDateTime;
                                }
                                else
                                {
                                    dr["SentDatetTime"] = DBNull.Value;
                                }
                                dr["IsMedia"] = item.IsMedia;
                                dr["CreatedDate"] = item.CreatedDate;
                                dt.Rows.Add(dr);
                            }
                        }
                        break;
                    case "Drafts":
                        lbl_countall.Text = db.SMS.Where(s => s.SentDateTime != null || s.IsDraft.Equals(true)).Count().ToString();
                        lbut_showinbox.Font.Bold = false;
                        lbut_showoutbox.Font.Bold = false;
                        lbut_showdraffs.Font.Bold = true;
                        sms = db.SMS.OrderByDescending(s => s.SentDateTime).Where(s => (s.SentDateTime == null || s.IsDraft.Equals(true)) & s.user_id.Equals(userid)).Select(s => new
                        {
                            Id = s.Id,
                            Title = s.Title,
                            Body = s.Body,
                            FromUser = db.SMSAccounts.Where(a => a.user_id.Equals(userid)).Select(a => a.FromMobileNo).FirstOrDefault(),
                            ToUser = s.To_MobileNo,
                            SentDateTime = s.SentDateTime,
                            IsMedia = false,
                            CreatedDate = s.CreatedDate
                        }).FilterForColumn(ColumnName, SearchText).ToList();
                        foreach (var item in sms)
                        {
                            if (item != null)
                            {
                                DataRow dr = dt.NewRow();
                                dr["Id"] = item.Id.ToString();
                                dr["Title"] = item.Title.ToString();
                                dr["Body"] = Regex.Replace(item.Body.ToString().Trim(), @"<(.|\n)*?>", string.Empty);
                                dr["FromUser"] = item.FromUser.ToString();
                                if (item.ToUser != null)
                                {
                                    dr["ToUser"] = item.ToUser.ToString();
                                }
                                else
                                {
                                    dr["ToUser"] = "NoN";
                                }
                                if (item.SentDateTime != null)
                                {
                                    dr["SentDatetTime"] = item.SentDateTime;
                                }
                                else
                                {
                                    dr["SentDatetTime"] = DBNull.Value;
                                }
                                dr["IsMedia"] = item.IsMedia;
                                dr["CreatedDate"] = item.CreatedDate;
                                dt.Rows.Add(dr);
                            }
                        }
                        break;
                }
            }
            else
            {
                switch (CommandName)
                {
                    //case "Inbox":
                     .............
                    //    break;
                    case "Outbox":
                        lbl_countall.Text = db.SMS.Where(s => s.SentDateTime != null).Count().ToString();
                        lbut_showinbox.Font.Bold = false;
                        lbut_showoutbox.Font.Bold = true;
                        lbut_showdraffs.Font.Bold = false;
                        sms = db.SMS.OrderByDescending(s => s.SentDateTime).Where(s => (s.SentDateTime != null || s.IsDraft.Equals(false)) & s.user_id.Equals(userid)).Select(s => new
                        {
                            Id = s.Id,
                            Title = s.Title,
                            Body = s.Body,
                            FromUser = db.SMSAccounts.Where(a => a.user_id.Equals(userid)).Select(a => a.FromMobileNo).FirstOrDefault(),
                            ToUser = s.To_MobileNo,
                            SentDateTime = s.SentDateTime,
                            IsMedia = false,
                            CreatedDate = s.CreatedDate
                        }).FilterForColumn(ColumnName, SearchText).ToList().Where(x => x.Title.StartsWith(StartAlpha, StringComparison.CurrentCultureIgnoreCase)).ToList();
                        foreach (var item in sms)
                        {
                            if (item != null)
                            {
                                DataRow dr = dt.NewRow();
                                dr["Id"] = item.Id.ToString();
                                dr["Title"] = item.Title.ToString();
                                dr["Body"] = Regex.Replace(item.Body.ToString().Trim(), @"<(.|\n)*?>", string.Empty);
                                dr["FromUser"] = item.FromUser.ToString();
                                if (item.ToUser != null)
                                {
                                    dr["ToUser"] = item.ToUser.ToString();
                                }
                                else
                                {
                                    dr["ToUser"] = "NoN";
                                }
                                if (item.SentDateTime != null)
                                {
                                    dr["SentDatetTime"] = item.SentDateTime;
                                }
                                else
                                {
                                    dr["SentDatetTime"] = DBNull.Value;
                                }
                                dr["IsMedia"] = item.IsMedia;
                                dr["CreatedDate"] = item.CreatedDate;
                                dt.Rows.Add(dr);
                            }
                        }
                        break;
                    case "Drafts":
                        lbl_countall.Text = db.SMS.Where(s => s.SentDateTime != null).Count().ToString();
                        lbut_showinbox.Font.Bold = false;
                        lbut_showoutbox.Font.Bold = false;
                        lbut_showdraffs.Font.Bold = true;
                        sms = db.SMS.OrderByDescending(s => s.SentDateTime).Where(s => (s.SentDateTime == null || s.IsDraft.Equals(true)) & s.user_id.Equals(userid)).Select(s => new
                        {
                            Id = s.Id,
                            Title = s.Title,
                            Body = s.Body,
                            FromUser = db.SMSAccounts.Where(a => a.user_id.Equals(userid)).Select(a => a.FromMobileNo).FirstOrDefault(),
                            ToUser = s.To_MobileNo,
                            SentDateTime = s.SentDateTime,
                            IsMedia = false,
                            CreatedDate = s.CreatedDate
                        }).FilterForColumn(ColumnName, SearchText).ToList().Where(x => x.Title.StartsWith(StartAlpha, StringComparison.CurrentCultureIgnoreCase)).ToList();
                        foreach (var item in sms)
                        {
                            if (item != null)
                            {
                                DataRow dr = dt.NewRow();
                                dr["Id"] = item.Id.ToString();
                                dr["Title"] = item.Title.ToString();
                                dr["Body"] = Regex.Replace(item.Body.ToString().Trim(), @"<(.|\n)*?>", string.Empty);
                                dr["FromUser"] = item.FromUser.ToString();
                                if (item.ToUser != null)
                                {
                                    dr["ToUser"] = item.ToUser.ToString();
                                }
                                else
                                {
                                    dr["ToUser"] = "NoN";
                                }
                                if (item.SentDateTime != null)
                                {
                                    dr["SentDatetTime"] = item.SentDateTime;
                                }
                                else
                                {
                                    dr["SentDatetTime"] = DBNull.Value;
                                }
                                dr["IsMedia"] = item.IsMedia;
                                dr["CreatedDate"] = item.CreatedDate;
                                dt.Rows.Add(dr);
                            }
                        }
                        break;
                }
            }
            myDataSet.Tables.Add(dt);
            if (myDataSet.Tables[0].Rows.Count > 0)
            {
                DataView myDataView = new DataView();
                myDataView = myDataSet.Tables[0].DefaultView;
                if (this.ViewState["SortExp"] != null)
                {
                    myDataView.Sort = this.ViewState["SortExp"].ToString()
                             + " " + this.ViewState["SortOrder"].ToString();
                }
                GV_ViewSMS.DataSource = myDataView;
            }
            if (GV_ViewSMS.Rows.Count != 0)
            {
                SetPageNumbers();
            }
            GV_ViewSMS.DataBind();
        }
    }

并出现此错误:

“/”应用程序中的服务器错误。 列 'SentDatetTime' 不属于 table。 说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的来源的详细信息。

异常详细信息:System.ArgumentException:列 'SentDatetTime' 不属于 table。

Source Error:


Line 439:                                else
Line 440:                                {
Line 441:                                    dr["SentDatetTime"] = DBNull.Value;
Line 442:                                }
Line 443:                                dr["IsMedia"] = item.IsMedia;


Source File: e:\EASYMAIL_off\EASYMAIL\BulkSMS.aspx.cs    Line: 441

堆栈跟踪:

[ArgumentException: Column 'SentDatetTime' does not belong to table .]
   System.Data.DataRow.GetDataColumn(String columnName) +5731291
   System.Data.DataRow.set_Item(String columnName, Object value) +13
   BulkSMS.FillGrid(String StartAlpha, String CommandName, String ColumnName, String SearchText) in e:\EASYMAIL_off\EASYMAIL\BulkSMS.aspx.cs:441
   BulkSMS.Page_Load(Object sender, EventArgs e) in e:\EASYMAIL_off\EASYMAIL\BulkSMS.aspx.cs:40
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
   System.Web.UI.Control.OnLoad(EventArgs e) +95
   System.Web.UI.Control.LoadRecursive() +59
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +678


Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.6.1055.0 

我的代码有什么问题?????

请帮我解决这个问题.....

您似乎有拼写错误 SentDatetTime -> SentDateTime:

if (item.SentDateTime != null)
{
    dr["SentDateTime"] = item.SentDateTime;
}
else
{
    dr["SentDateTime"] = DBNull.Value;
}