如何绑定数据源列出<&字典LT;字符串,字符串>> ;?

如何绑定数据源列出<&字典LT;字符串,字符串>> ;?

问题描述:

我有存储字典条目的列表的类。我想结合,为从代码隐藏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 Question I will document a complete project when i finish on the wiki.

这不能做,我认为。我会做的是:

This can not be done I think. What I'd do is:


  1. 而不是使用词典&LT;字符串,字符串&GT; 定义包含字段信息

  2. 创建该类的对象的数据源(使用Visual Studios的数据源窗口)

  3. GetErrorMessages()收益列表&LT; ErrorClass&GT; 而不是词典

  4. 分配该列表绑定源。

  1. Instead of using Dictionary<string, string> define a class that contains two public properties for field and message
  2. Create an object data source for that class (using Visual Studios "Data Sources" window)
  3. Have GetErrorMessages() return List<ErrorClass> instead of Dictionary
  4. Assign that list to the binding source.

修改

这是根据最新的意见,以澄清事情。你需要的是的有一个的类,它包含的有一个的错误信息。例如:

EDIT
This 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.