4

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.

Community
  • 1
  • 1
naim5am
  • 1,334
  • 3
  • 17
  • 33
  • Automatic data-binding can't do this. You'll have to use a PropertyDescriptor, so that the table knows how to convert the item to a table. Look up BindingList, PropertyDescriptor and ITypedList for in the MSDN. – gimpf Aug 17 '09 at 08:52
  • 1
    Also consider using a StringDictionary (http://msdn.microsoft.com/en-us/library/system.collections.specialized.stringdictionary.aspx) instead Dictionary. – Hilton Perantunes Dec 07 '09 at 17:28

4 Answers4

3

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.

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
  • Yes, using a simple wrapper is simpler than doing around with property descriptors... – gimpf Aug 17 '09 at 09:11
  • Thorsten, thanks for your answer, but that won't work as per your description. I need to stored many pairs of (FieldName,ErrorMessage). So ErrorClass must contain a list of (FieldName,ErrorMessage) pairs. But you gave me an idea to scrap the dictionary, and use List where ErrorClass represents a list of (FieldName,ErrorMessage) pairs. Hence, i will end up with a list. I will try this and see where it leads. – naim5am Aug 17 '09 at 09:27
  • You will - in the end - have to flatten the List> into a List<...>, because data binding needs a 1:1 relation between displayed item and "data row" (which, in your case will be an error message for a field). – Thorsten Dittmar Aug 17 '09 at 09:45
  • Thanks, i understand that part now. I am now passing the list as an instance of a class. So i have a list objects, each of which is a class containing a list of errors... But i still cannot get it to display in a grid! – naim5am Aug 18 '09 at 01:52
1

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

Community
  • 1
  • 1
Arif
  • 315
  • 2
  • 10
0

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();
jenson-button-event
  • 18,101
  • 11
  • 89
  • 155
0

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