23Feb
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
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
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
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
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