how to bind datasource to List<Dictionary<string, string>>?

我是研究僧i 提交于 2019-12-05 18:20:47

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.

Arif

Databind List of Dictionnary into a GridView

List<Dictionary<string,string>> resultSet = SOME List of Dictionaries...
DataGridView.DataSource = resultSet.Select(x => new { 
fieldOne = x["key1"], fieldTwo = x["key2"] 
}).ToList();
DataGridView.DataBind();

Now u can Bind fieldOne and fieldTwo in the DataGridView element...

Kindly check the Link for the precise ans...

Thanks

.NET provides a handy KeyValuePair<(Of <(TKey, TValue>)>) structure, that can be used in cases like this. That way you don't have to define your own class. HTH.

Or you could bind to the Value & Key properties of each Dictionary item:

ErrorBoxGridView.DataSource = FailedRecords.GetErrorMessages();
ErrorBoxGridView.DataTextField = "Value";
ErrorBoxGridView.DataValueField = "Key";
ErrorBoxGridView.DataBind();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!