Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Data Binding / August 2008

Tip: Looking for answers? Try searching our database.

DataGridView BindingList and CellFormatting and CellParsing

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Joe - 06 Aug 2008 13:51 GMT
I have an object that inherits from BindingList<T> that I'm using as a data
source for a DataGridView. The items in the BindingList have 2 properties
which are enums. I'm using the CellParsing to assign e.Value to my enum
value and in the CellFormatting I'm doing the reverse. The problem is that
the CellFormatting e.Value is always the 1st value in the ComboBox list and
not the real value in the underlying object.

Also, dataGridView1_DataError always gets called with e.Exception =
"DataGridViewComboBoxCell value is not valid." and the Context = Formatting
| Display.

Any ideas?

For example:

enum MyEnum {Item1, Item2, Item3};

public MyClass
{
   private MyEnum myenum;
   private string astring;

   public MyEnum TheEnum
   {
       get {return myenum;}
       set {myenum = value;}
   }

   public string MyString
   {
       get {return astring;}
       set {astring = value;}
   }
}

public class MyListClass : BindingList<MyClass>
{

}

.....

DataGridViewComboBoxColumn column1 = new DataGridViewComboBoxColumn();
column1.Items.AddRange("First Item", "Second Item", "Third Item");
dataGridView1.Columns.Add(column1);

......

MyListClass listClass = new MyListClass();

dataGridView1.DataSource = listClass;

....

       private void dataGridView1_CellParsing(object sender,
DataGridViewCellParsingEventArgs e)
       {
           if (e.DesiredType == typeof(MyEnum))
           {
               if (e.Value != null)
               {
                   switch (e.Value.ToString())
                   {
                       case "First Item": e.Value = MyEnum.Item1; break;
                       case "Second Item": e.Value = MyEnum.Item2; break;
                       case "Third Item": e.Value = MyEnum.Item3; break;

                   }

                   e.ParsingApplied = true;
               }
           }
       }

       private void dataGridView1_CellFormatting(object sender,
DataGridViewCellFormattingEventArgs e)
       {
           // Here e.Value always equals "First Item" even after a
different item has been selected.

           switch (e.ColumnIndex)
           {
               case 0:
                   if (e.Value != null && e.Value.GetType() ==
typeof(MyEnum))
                   {
                       switch ((ColumnDataFilterOperator)e.Value)
                       {
                           case MyEnum.Item1: e.Value = "First Item";
break;
                           case MyEnum.Item2: e.Value = "Second Item";
break;
                           case MyEnum.Item3: e.Value = "Third Item";
break;
                       }

                   }
           }

       }

Thanks for any help,
Joe
Zhi-Xin Ye [MSFT] - 07 Aug 2008 03:47 GMT
Dear Joe,

The CellParsing event occurs when a cell leaves edit mode if the cell value
has been modified.
When the form loads, none of the cells is in edit mode, so no CellParsing
event occurs, the values in the cells are still of string type, but the
desired type is MyEnum, so the "value not valid" exception throws.

For binding Enum types, then best way is calling the Enum.GetValues()
method to retrieve an array of values of the constants in a the
enumeration, then use this array as the data source of the
DataGridViewComboBoxColumn, something like this:

      private void Form1_Load(object sender, EventArgs e)
       {
           DataGridViewComboBoxColumn column1 = new
DataGridViewComboBoxColumn();

           column1.DataPropertyName = "TheEnum";
           column1.DataSource = Enum.GetValues(typeof(MyEnum));

           dataGridView1.Columns.Add(column1);

           MyListClass listClass = new MyListClass();

           listClass.Add(new MyClass("a", MyEnum.Item3));
           listClass.Add(new MyClass("b", MyEnum.Item2));
           listClass.Add(new MyClass("c", MyEnum.Item1));

           dataGridView1.DataSource = listClass;
       }

Through this approach, we can avoid the manually parsing and formatting
stuff, which means, we don't need to handle the CellParsing and
CellFormating events at all in this scenario.

[Documents for your information]

DataGridView.CellParsing Event
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.ce
llparsing.aspx

Enum.GetValues Method
http://msdn.microsoft.com/en-us/library/system.enum.getvalues.aspx

If you need further help on this, please don't hesitate to let me know.

Have a nice day!

Sincerely,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Zhi-Xin Ye [MSFT] - 11 Aug 2008 11:40 GMT
Dear Joe,

Does my last reply make sense to you? If you need further help, please feel
free to let me know, I will be happy to be of assistance.

Have a nice day!

Sincerely,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no rights.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.