数据网格源异常 [英] Data grid Source exception

查看:97
本文介绍了数据网格源异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到以下问题

我向数据网格视图编写了两个过滤器,如下所示

hi i am having following problem

I wrote two filters to data grid view as follows

public void FilterFromBodyContent(CheckedListBox obj)
        {
            List<unreademails> newList = new List<unreademails>();
            List<unreademails> list = (List<unreademails>)dgvUnreadMails.DataSource;
            List<unreademails> filteredList = (List<unreademails>)dgvUnreadMails.DataSource;
            List<string> words = new List<string>();
            foreach (object o in obj.CheckedItems)
            {
                string s = o.ToString().ToLower();
                if (s != null)
                {
                    string[] separated = s.Split('','');
                    foreach (string word in separated)
                    {
                        if (!string.IsNullOrEmpty(word))
                        {
                            if (!words.Contains(word))
                            {
                                words.Add(word);
                            }
                        }
                    }
                }
            }

            foreach (string item in words)
            {

                filteredList = (List<unreademails>)(from ci in list
                                                    where ci.BodyContent.ToLower().Contains(item)

                                                    select ci).ToList<unreademails>();

                newList = newList.Union(filteredList).ToList();
            }

            dgvUnreadMails.DataSource = newList;
            btnCancel.Enabled = true;
        }</unreademails></unreademails></string></string></unreademails></unreademails></unreademails></unreademails></unreademails></unreademails>

然后我按照动态计数器操作来获取paticutler单元中的单词数,如下所示

then i wort dynamic counter for get number of words in paticutler cell as follows

 private object GetDynamicDataTable(List<unreademails> emailList, List<string> words)
        {
            DataTable dtList = new DataTable();
            dtList.Columns.Add("Sender Name");
            dtList.Columns.Add("Sender Address");
            dtList.Columns.Add("Date Time");
            dtList.Columns.Add("Subject");
            dtList.Columns.Add("Body Content");
            dtList.Columns.Add("Attachments");
            dtList.Columns.Add("EntryID");

            foreach (string dynamic in words)
            {
                dtList.Columns.Add(dynamic);
            }

            foreach (UnreadEmails mail in emailList)
            {
                DataRow dr = dtList.NewRow();
                dr["Sender Name"] = mail.SenderName;
                dr["Sender Address"] = mail.SenderAddress;
                dr["Date Time"] = mail.RecivedTime;
                dr["Body Content"] = mail.BodyContent;
                dr["Subject"] = mail.Subject;
                dr["EntryID"] = mail.EntryID;



                foreach (string item in words)
                {
                    object count = GetWordCount(mail.BodyContent.ToString(), item);
                    //if (count.ToString != null)
                    //{
                        dr[item] = count;
                    //}
                   // dr[item] = GetWordCount(mail.BodyContent.ToString(), item);
                }

                dtList.Rows.Add(dr);
            }                      
            return dtList;
        }

        private object GetWordCount(string p, string item)
        {
            string[] source = p.Split(new char[] { ''.'', ''?'', ''!'', '' '', '';'', '':'', '','' }, StringSplitOptions.RemoveEmptyEntries);
            string[] arrremove = null;
            int count = 0;            
            frmStopWordList stopWords = new frmStopWordList();
            arrremove = stopWords.getStopWords();

            for (int i = 0; i < source.Length; i++)
            {

                for (int j = 0; j < arrremove.Length; j++)
                {
                    if (source[i] == arrremove[j])
                    {
                        source[i] = "";
                    }                  
                }
            }

            var matchQuery = from word in source
                             where word.ToLowerInvariant() == item.ToLowerInvariant()
                             select word;

            count = matchQuery.Count();

            return count;            
        }
</string></unreademails>




但是当我运行过滤器时,它抛出一个异常,告诉无法转换类型为"System.Data.DataTable"的对象来键入类型为"System.Collections.Generic.List`1 [E_mail_Filter.UnreadEmails]"" ."

我可以理解该异常,但是无法通过精细的解决方案来帮助我该怎么做.




but when i run filter it is throw an exception telling "Unable to cast object of type ''System.Data.DataTable'' to type ''System.Collections.Generic.List`1[E_mail_Filter.UnreadEmails]''."

i can understand the exception but couldnt fine solution pls help how can i do it

推荐答案

ok让我们尝试解决它.​​.

问题在于将linq查询结果显式转换为list,然后再将其分配给过滤列表,因为默认情况下,linq查询的结果在通用集合中返回,这就是为什么它允许您使用var type..so将其强制转换为string列表是不可能的(就像您以前想做的那样)

..so,您应该首先为之前创建的所有列表创建一个通用列表List<string>,然后在foreach循环中创建一个通用列表,
返回查询到IEnumerable<string> var类型的变量. (我个人建议使用IEnumerable<string>类型,以避免类型和值的歧义).

然后由于它已经是一种可枚举的通用集合类型,因此您可以轻松地将其所有内容直接复制到您最初想要的list类型中

所以我认为您应该修改代码的这一部分,使其看起来像这样..

ok lets attempt to resolve it..

The problem is with the explicit cast of the linq query result to a list before assigning it to filtered list because by default the results of the linq query is returned in a generic collection that is why it allows you to use var type..so casting it to a string list would be impossible ( as in the case you previously wanted to do)

..so what you should do is to first create a generic list List<string> for all the previous lists you created and then secondly, in the foreach loop,
return the query to a variable with the IEnumerable<string> or the var type. (I would personally recommend the IEnumerable<string> type to avoid type and value ambiguity).

and then since its already an enumerable generic collection type, you can easily copy all its contents directly into the list type you initially wanted to do

so I think you should modify that section of your code to look like this..

foreach(string item in words)
{
   IEnumerable<string> q = from ci in list where ci.BodyContent.ToLower().Contains(item) select ci;

//now use a nested foreach loop to push the items in q into a generic List<string> instead..It should work
   
}


这篇关于数据网格源异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆