且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将数据源绑定到列表<字典< string,string>>?

更新时间:2022-10-21 09:48:57

这不能完成我的想法。我会做的是:


  1. 而不是使用字典< string,string> 定义一个包含字段和消息的两个公共属性的类

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

  3. GetErrorMessages()返回列表< ErrorClass> 而不是字典

  4. 将该列表分配给绑定源。



这是根据最新的评论来澄清事情。您需要的是一个类,其中包含一个错误的信息。例如:

  public class ErrorInfo 
{
public string Field {get {...}}
public string Message {get {...}}
}

之后您在表单和(代码)中放置 BindingSource 将其 DataSource 属性设置为列表错误消息类。例如:

 私人列表< ErrorInfo> errorList = new List< ErrorInfo>(); 
errorList.Add(new ErrorInfo(){...});
errorList.Add(new ErrorInfo(){...});
errorList.Add(new ErrorInfo(){...});

bindingSource.DataSource = errorList;

数据网格视图绑定到 BindingSource 。您现在应该会看到数据。您可以手动创建列并将其设置为 ErrorInfo 类的相应属性名称,但是您必须设置 dataGridView.AutoCreateColumns false 代码中的某个地方。


I have a class that stores a list of dictionary entries. I want bind that to a datasource for gridview from codebehind.

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;
    }
}

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();

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.

If interested why i am doing this to store error messages, have a look at this:link 1

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. 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 { ... } }
}

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;

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.