Binding control property with DataGridTextBoxColumn in datagrid

  • Thread starter Thread starter BigRich
  • Start date Start date
B

BigRich

Binding control property used with DataGridTextBoxColumn
in datagrid
I'm having trouble with the WinFoms DataGrid control.

I'm using a collection of DataGridTextBoxColumn(s) and
DataGridBoolColumn(s) in a DataGridTableStyle on a
DataGrid.

I need to format and parse each column distinctly, so I'm
adding a Binding, and then Format and Parse event
handlers, to the DataGrid.

Although my code throws no exceptions, the Format event
doesn't seem to fire... I think the problem lies in what
to specify as the control property in the Binding
constructor call.

While I'm usually loathe to post code ("Read my code"
== "Eat my shorts"), here's a simplified fragment. I'd
appreciate a hand with this:

DataGridTableStyle columns = new DataGridTableStyle();
columns.MappingName = "myTable";

DataGridTextBoxColumn colTime = new DataGridTextBoxColumn
();
colTime.HeaderText = "Time";
colTime.MappingName = "time"; // valid DataTable col name
columns.GridColumnStyles.Add(this.colTime);

myGrid.TableStyles.Add(this.columns);

Binding timeBinding = new Binding(
colTime.TextBox.Text.ToString(),
myTable,
"time");
timeBinding.Format += new ConvertEventHandler
(timeBinding_Format);
myGrid.DataBindings.Add(timeBinding);

CheersThenYeah
BigRich
 
Thanks Ken,

I'll look into that.

However I don't understand how it will help: the problem
seems to be with the Binding object I create, and knowing
what to specify in the first parm (propertyName) on the
Binding constructor.

The VS .NET help for Binding describes the Format event,
for example, and I'm not sure what subclassing would do -
what would I need to put in the derived class that would
make the difference?

Thanks
BigRich
 
Thanks Ken,

I'll look into that.

However I don't understand how it will help: the problem
seems to be with the Binding object I create, and knowing
what to specify in the first parm (propertyName) on the
Binding constructor.

The VS .NET help for Binding describes the Format event,
for example, and I'm not sure what subclassing would do -
what would I need to put in the derived class that would
make the difference?

Thanks
BigRich
 
Binding control property used with DataGridTextBoxColumn
in datagrid
I'm having trouble with the WinFoms DataGrid control.

I'm using a collection of DataGridTextBoxColumn(s) and
DataGridBoolColumn(s) in a DataGridTableStyle on a
DataGrid.
Binding timeBinding = new Binding(
colTime.TextBox.Text.ToString(),
myTable,
"time");
timeBinding.Format += new ConvertEventHandler
(timeBinding_Format);
myGrid.DataBindings.Add(timeBinding);


The first parameter to the Binding ctor is the name of a property on the
control that will own the DataBinding, usually something like "Text" or
"SelectedValue". At the time you're binding, I'm not sure what
Text.ToString() is equal to (probably String.Empty), but it's not going
to be the name of a DataGrid property.

*Possibly*, you might be able to do

timeBinding = new Binding("Text", myTable, "time");
timeBinding.Format += ...

colTime.TextBox.DataBindings.Add(timeBinding);

But I can't test this right now, and I doubt it would work. For one
thing, I don't think the TextBox of a DataGrid column is actually bound
to anything (I strongly suspect the DataGridTextBoxColumn manually
pushes and pulls the data)

All in all, the format and parse events are pretty useful for normal
controls, but the DataGrid has its own way of doing things. I'd agree
with the other poster, subclassing the DataGridTextBoxColumn is by far
the easiest solution here (although the DataGridTextBoxColumn itself
does have some basic formatting capabilities which might do what you
need).
 
Back
Top