本文介绍了如何绑定数据源列出<&字典LT;字符串,字符串>> ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有存储字典条目的列表的类。我想结合,为从代码隐藏gridview的数据源。I have a class that stores a list of dictionary entries. I want bind that to a datasource for gridview from codebehind.规范字典类型,代表的ErrorMessage和失败的领域。Code for dictionary type of , representing ErrorMessage and failed field.public partial class FailedFields{ private Dictionary<string, string> Code_Error = new Dictionary<string, string>(); public void AddFailedField(string field, string message) { Code_Error.Add(field, message); } public Dictionary<string, string> GetFailedFields() { return Code_Error; }} 规范词典条目列表。Code for List of Dictionary entries.public partial class ErrorFieldsList{ private static List<Order.FailedFields> ErrorList = new List<Slab.FailedFields>(); public void AddErrorField(Order.FailedFields errs) { ErrorList.Add(errs); } public List<Order.FailedFields> GetErrorMessages() { return ErrorList; }} 在Visual Studio调试模式下运行时,我可以看到列表中有错误列表,但我不能让它在GridView显示。贝娄是众多途径之一(一个最有意义)我试图设置列表作为数据源。Running in Visual Studio debug mode, i can see the list has the error list, but i cannot get it to display in the gridview. Bellow is one of the many ways (the one that makes most sense) i tried to set the list as a datasource.ErrorBoxGridView.DataSource = FailedRecords.GetErrorMessages(). ;ErrorBoxGridView.DataBind(); 任何想法,我错了? 此外,我不希望在指定aspx页面的数据源,因为我只希望发生错误时显示这一点。Any idea where i am going wrong ?Also, i don't want to specify a datasource in the aspx page because i only want to display this when the error occurs.的如果有兴趣的为什么我这样对商店的错误信息,看看这个:的链接1 的 解决这里的有关问题的 我会当我在wiki上记录完一个完整的项目。Solved Here Related QuestionI will document a complete project when i finish on the wiki.推荐答案这不能做,我认为。我会做的是:This can not be done I think. What I'd do is: 而不是使用词典<字符串,字符串> 定义包含字段信息 创建该类的对象的数据源(使用Visual Studios的数据源窗口) 的 GetErrorMessages()收益列表< ErrorClass> 而不是词典 分配该列表绑定源。Instead of using Dictionary<string, string> define a class that contains two public properties for field and messageCreate an object data source for that class (using Visual Studios "Data Sources" window)Have GetErrorMessages() return List<ErrorClass> instead of DictionaryAssign that list to the binding source. 修改 这是根据最新的意见,以澄清事情。你需要的是的有一个的类,它包含的有一个的错误信息。例如:EDITThis is to clarify things according to the latest comments. What you need is one class that contains the information for one error. For example:public class ErrorInfo{ public string Field { get { ... } } public string Message { get { ... } }}在您将在的BindingSource 表单上(代码)的数据源属性设置为列表。例如:After that you place a BindingSource on your form and (in code) set its DataSource property to a list of error message classes. For example:private List<ErrorInfo> errorList = new List<ErrorInfo>();errorList.Add(new ErrorInfo() { ... });errorList.Add(new ErrorInfo() { ... });errorList.Add(new ErrorInfo() { ... });bindingSource.DataSource = errorList; 数据网格视图绑定到的BindingSource 。现在你应该看到的数据。您可以手动创建列,并将其设置为你的 ERRORINFO 类为好,但你必须设置 dataGridView.AutoCreateColumns各自的属性名称到假某处你的代码。The data grid view is bound to the BindingSource. You should see data now. You can manually create columns and set them to the respective property names of your ErrorInfo class as well, but then you'd have to set dataGridView.AutoCreateColumns to false somewhere in your code. 这篇关于如何绑定数据源列出<&字典LT;字符串,字符串>> ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-20 23:40