need some advice on writing a custom format event handler for data-bound Fields

L

Larry

I've been reading some VB.net articles written for wndows forms allow one to
write a custom format routine to customize the formating of data in controls
at binding time to a dataset. I wanted to try applying this idea to a
datagrid. The examples in the docs look like this:

Private Sub DecimalToCurrencyString(sender As Object, cevent As _
ConvertEventArgs)
' The method converts only to string type. Test this using the
DesiredType.
If Not cevent.DesiredType Is GetType(String) Then
Exit Sub
End If

' Use the ToString method to format the value as currency ("c").
cevent.Value = CType(cevent.Value, Decimal).ToString("c")
End Sub

Private Sub CurrencyStringToDecimal(sender As Object, cevent As _
ConvertEventArgs)
' The method converts back to decimal type only.
If Not cevent.DesiredType Is GetType(Decimal) Then
Exit Sub
End If

' Converts the string back to decimal using the static ToDecimal method.
cevent.Value = Decimal.Parse(cevent.Value.ToString, _
NumberStyles.Currency, nothing)
End Sub

Private Sub BindControl
' Creates the binding first. The OrderAmount is a Decimal type.
Dim b As Binding = New Binding _
("Text", ds, "customers.custToOrders.OrderAmount")
' Add the delegates to the event
AddHandler b.Format, AddressOf DecimalToCurrencyString
AddHandler b.Parse, AddressOf CurrencyStringToDecimal
text1.DataBindings.Add(b)
End Sub

The first problem I ran into was trying to find a module to import that
would define the ConvertEventArgs type. So, my question is: does a format
event exists for databinding to a web control? and if so can it be done the
same way a windows form is done (as above) or is there a different way to do
the same thing? and lastly if this (the above example) should work how do I
get the references for the convertEventArgs included in my web application?

-Larry
 
J

Jeffrey Tan[MSFT]

Hi Larry,

Based on my understanding, you want to format the column expression of
webform datagrid.

The code snippet you pasted is WinForm databinding. For webForm
databinding, the senario is completely different.

For Asp.net WebForm, all the web controls are existed at server side, once
renderred to the client side, these objects are destroyed. All the objects
will render as Html code.

So for webform DataGrid databinding, it just populate all the rows and
columns data for the DataGrid, then all the data will render into html.

To do the formatting for the WebForm datagrid, you should just format the
data that will specify for the Column. Such as:

<asp:datagrid id=DataGrid1 runat="server" DataSource="<%# dataSet11 %>">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:Label runat="server" ID="lb"
Text='<%#formatdata(Container.DataItem)%>'></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>

Then in code behind, you can format it:
protected string formatdata(DataRowView drv)
{
//do the formatting

return formatted_string;
}

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
L

Larry

Thanks for your answer. I was aware that the code snippet I pasted here was
from a winform but I've seen and hoped that there was consistencey between
the two (as much as possible). I know I need to do more work with datagrid
templetes, but just haven't found the time as I keep getting caught up in
system admin issues of late.

Thanks again for your help.
-Larry
 
J

Jeffrey Tan[MSFT]

Hi Larry,

Thanks very much for your feedback.

I am glad my reply makes sense to you. There is an article writen by Dino
Esposito about the Template Columns, please refer to:
"Understanding Templates in ASP.NET"
http://msdn.microsoft.com/msdnmag/issues/02/01/cutting/

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top