How to associate datasource with List <Dictionary <string, string >>?
I have a class that stores a list of dictionary entries. I want to bind this to the data source for the gridview from codebehind.
Code for the dictionary type representing the 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 the list of entries in the dictionary.
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 debugging mode, I see that the list has a list of errors, but I canβt display it in gridview. Bellow is one of many ways (the one that makes the most sense), I tried to set the list as a data source.
ErrorBoxGridView.DataSource = FailedRecords.GetErrorMessages(). ; ErrorBoxGridView.DataBind(); Any idea where I'm wrong? In addition, I do not want to indicate the data source on the aspx page, because I want to display this only when an error occurs.
If you are interested in why I am doing this to store error messages, look at this: link 1
Solved here Related question I will document the full project when I finish on the wiki.
This cannot be done, I think. I would do the following:
- Instead of using
Dictionary<string, string>define a class that contains two public properties for the field and the message - Create an object data source for this class (using the Visual Studio Data Sources window)
- Have
GetErrorMessages()returnList<ErrorClass>instead ofDictionary - Assign this list to a binding source.
EDIT
This should clarify the situation in accordance with the latest comments. You need one class containing information for one error. For instance:
public class ErrorInfo { public string Field { get { ... } } public string Message { get { ... } } } After that, you put the BindingSource in your form and (in code) set its DataSource property to the list of error message classes. For instance:
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 a BindingSource . You should now see the data. You can manually create columns and set them to the corresponding property names of your ErrorInfo class, but then you will need to set dataGridView.AutoCreateColumns to false somewhere in your code.
Dictionnary Data List in 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 you can bind fieldOne and fieldTwo in the DataGridView ...
Please check the link for exact analy ...
thanks