Riaan Lehmkuhl's Blog

Subversion, Progamming, Tips & Tricks and whatever else springs to mind.
23Feb

Windows.Forms Binding errors with .Net 2.0 (Part 4)

23 February 2009 20:53 by Riaan Lehmkuhl

The problem:
In a DataGridView control, after adding a new row, and clicking away, the new row just disappears without any visible error.

The solution:
This is most likely caused by an error being raised due to a data input error. Im my case it was because of a non visible column not allowing nulls.
To find out if this is the cause of yoyr issue, put a break point in the DataError event handler. Even though the error might have beeen written to the row's ErrorText, it is never displayed because the row disappears.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
09Jan

Accessing fields in the BindingSource.Current property

09 January 2008 22:18 by Riaan Lehmkuhl

I was using the Current property of a BindingSource object in VB.Net for sometime now using inplicit conversions to access fields:

MyBindingSource.Current(0) = someValue While in C# we need to explicitly cast the Current property to some type and use that. I tried using typeof(...) and got this peculiar result...
typeof(MyBindingSource.Current) 'Myproject.Form1.MyBindingSource' is a 'field' but is used like a 'type'

Then I tried .ToSting(), and this had a more promising result...
MyBindingSource.Current.ToString() "System.Data.DataRowView"

So, if you want to access a field in a BindingSource:
DataRowView drv = MyBindingSource.Current as DataRowView; drv[0] = someValue; //or assignment (if the field's type is an int) int someValue = Convert.ToInt32(drv[0]);
Hope i save someone five minutes.

Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
04Jan

Windows.Forms Binding errors with .Net 2.0 (Part 3)

04 January 2008 22:13 by Riaan Lehmkuhl
On to the next hurdle (sigh).

The problem:
In a DataGridView, when deleting the last remaining data row, the UserDeletedRow event does not fire.

The solution:
To work around this we need to add some logic to the UserDeletingRow event handler to count the number of rows and call the event handler when necessary... private void DataGridView1_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e) {    if (DataGridView1.RowCount == (DataGridView1.AllowUserToAddRows ? 2 : 1))       DataGridView1_UserDeletedRow(DataGridView1, new DataGridViewRowEventArgs(e.Row)); } private void DataGridView1_UserDeletedRow(object sender, DataGridViewRowEventArgs e) {    // do something here... }

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
03Jan

Windows.Forms Binding errors with .Net 2.0 (Part 2)

03 January 2008 22:05 by Riaan Lehmkuhl
Hi, this is number two of my Binding error series.

I've got a BindingSource setup with the Sort property set to a valid column name and all works well, (dejavu) but the problem is when the form closes, and starts Disposing controls, I get this message:
"Sort string contains a property that is not in the IBindingList."

The solution is almost the same as in Part 1, but this time we want to clear the Sort property of all BindingSources on the form...
/// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) {    try {       if (disposing && (null != components)) {          foreach (System.ComponentModel.IComponent comp in components.Components) {             if (comp is System.Windows.Forms.BindingSource)                (comp as System.Windows.Forms.BindingSource).Sort = null;          }          components.Dispose();       }    } finally {       base.Dispose(disposing);    } }

Notice the bit where we loop through the components to be disposed of. Now Isn't this fun!

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
02Jan

Windows.Forms Binding errors with .Net 2.0 (Part 1)

02 January 2008 21:49 by Riaan Lehmkuhl

I've recently had to work on a project using DataBinding. All seemed to go well until I ran into some strange issues.

This is the first of a few posts describing these issues and how to work around them.

I've got some bound ComboBox columns in a bound DataGridView and all works well, but the problem is when the form closes, and starts Disposing controls, I get this message:

"Message: ArgumentException: Cannot bind to the property or column State on the DataSource. Parameter name: dataMember"

It seems to affect the ComboBoxes bound to the NameValueLists where the Text property is databound. The solution is to clear all DataBinding before Dispose().

Step 1: Create a method to clear DataBinding recursively... using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; using System.ComponentModel; using System.Reflection; namespace DataBinding {    internal sealed class Utilities {       /// <summary>       /// Clears all databindings from the control and all child controls.       /// </summary>       /// <param name="ctl">The control from which to clear the databindings.</param>       public static void ClearBindings(Control ctl) {          Binding[] bindings = new Binding[ctl.DataBindings.Count];          ctl.DataBindings.CopyTo(bindings, 0);          ctl.DataBindings.Clear();                    foreach (Binding bnd in bindings)              if (null != bnd) TypeDescriptor.Refresh(bnd.DataSource);                    PropertyInfo dataSourceProperty = ctl.GetType().GetProperty("DataSource");          object[] obj = new object[]{};          if (null != dataSourceProperty)             dataSourceProperty.SetValue(ctl, null, obj);                    foreach (Control child in ctl.Controls)             ClearBindings(child);       }    } } Step 2: Call the ClearBindings method when the Form has been closed... private void Form1_FormClosed(object sender, FormClosedEventArgs e) {    // Release bindings    Utilities.ClearBindings(this); }

That should do the trick.

Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Riaan Lehmkuhl


Me, a disorder of the brain that results in a disruption in a person's thinking, mood, and ability to relate to others.

Recent comments

Comment RSS

Thingies

Calendar And Month List

<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

View posts in large calendar

Disclaimer & Privacy

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Privacy:
We use third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.

Most comments

Cool Quote

I know that you believe that you understood what you think I said, but I am not sure you realize that what you heard is not what I meant. - Robert McCloskey