YOUR FEEDBACK
Iterators in C#: Nothing Beats the Foreach Loop
Darryl wrote: I'm with Dave, screw your crappy pop-ups.

SYS-CON.TV
TOP MICROSOFT .NET LINKS


DataWindow.NET How To: DataWindow Formatting
A simple but powerful way of formatting data in the presentation layer

Digg This!

Page 2 of 4   « previous page   next page »

The FormatGridWithBothTableAndColumnStyles method (only an excerpt is shown below) uses both of those approaches and also defines a series of DataGridTextBoxColumn objects to handle the formatting of specific columns. The DataGridTextBoxColumn objects are then assigned to the DataGridTableStyle before it's applied to the DataGrid:

FormatGridWithBothTableAndColumnStyles

Dim grdColStyle1 As New DataGridTextBoxColumn()
With grdColStyle1
   .HeaderText = "ID"
   .MappingName = "ProductID"
   .Width = 50
End With
.
.
.
grdTableStyle1.GridColumnStyles.AddRange _
   (New DataGridColumnStyle() _
   {grdColStyle1, grdColStyle2, grdColStyle3, grdColStyle4})

Now, the most obvious thing to do if you want generic formatting of a DataWindow is to set up that formatting in the DataWindow object to begin with. However, the point of this "how to" is to show how we can do it through code, so we'll do that instead.

Some of the formatting of the DataWindow we'll do by setting the properties of the control itself:

dw_products.BorderStyle = Sybase.DataWindow.DataWindowBorderStyle.None
dw_products.TitleBar = True
dw_products.Text = "Northwind Products"

We'll implement others by doing SetProperty calls on the underlying DataWindow object. I've subclassed the DataWindow control and added methods for setting a number of the font and color properties.

dw_products.SetDataWindowColor(Color.GhostWhite)
dw_products.SetDataWindowBandColor (BandType.Detail, Color.Lavender)
dw_products.SetColumnFont(New Font("Tahoma", 8.0!))
dw_products.SetColumnBackColor(Color.GhostWhite)
dw_products.SetColumnColor(Color.MidnightBlue)
dw_products.SetHeaderBackColor (Color.MidnightBlue)
dw_products.SetHeaderFont(New Font ("Tahoma", 8.0!, FontStyle.Bold))
dw_products.SetHeaderColor(Color.Lavender)
dw_products.SetColumnWidth(100)
dw_products.SetRowHeight(15)

Note that there are a couple of properties that the DataGrid lets you set that the DataWindow doesn't give us any control over. Specifically with regard to this demo, we can't set the font or colors for the caption (the DataWindow control title area). I imagine that we might be able to do it by overriding the painting of the title bar in the subclassed control, but I'm not interested enough in those particular attributes to try it. The other properties of interest in this particular example are the fore and back colors for the band indicating the selected row(s) and color of the grid lines.

To get the same effect as the DataGridTextBoxColumn object, we just set the properties for individual columns of the DataWindow object:

dw_products.SetProperty("productid.width", "50")
dw_products.SetProperty("unitprice.width", "75")
dw_products.SetProperty("unitsinstock.width", "75")

Note that the DataGrid doesn't offer much manipulation of the column-level properties. For example, the DataGridTextBoxColumn object doesn't provide access to color attributes - so you can't set the color for individual columns. There are ways to overcome this, but they involve a great deal more effort than a SetProperty call. For more information, you can refer to George Shephard's Windows Forms FAQ for DataGrid controls at www.syncfusion.com/FAQ/WindowsForms/FAQ_c44c.aspx

Many of the features listed in the FAQ that take some effort to implement (setting row height, outthought rows, displaying a data-driven combobox or other edit controls (checkbox)) are simply a matter of setting a property or adding an edit style to the DataWindow object.

Also note that once you start defining DataGridTextBoxColumn objects, you have to create one for each column you want to display in the DataGrid regardless of whether you want to change its formatting. If you specify DataGridTextBoxColumn objects for some - but not all - of the columns in the DataSet, only the columns that have a DataGridTextBoxColumn object defined for them will display. (Shepard's FAQ offers a workaround for this particular behavior). With the DataWindow control, you can alter the formatting for any number of columns without affecting the visibility of the other columns in the result set.

However, the main advantage of the DataWindow over the DataGrid with regard to formatting is that many of the DataWindow object properties accept expressions that can be used to determine their value. Those expressions can use functions that determine state information for the control, or refer to data in the control. This lets you specify formatting not just for individual columns, but for individual cells. For example, in the FormatDataWindowTableStyle method if there are no units in stock, we set the color of the text in the unitsinstock column to red as follows:

Dim modify As String
modify = dw_products.GetRGB
  (Color.MidnightBlue) + _
"~tif ( unitsinstock < 5," + _
   dw_products.GetRGB(Color.Red)
+ "," + _
   dw_products.GetRGB(Color.Midnight
Blue) + ")"
  dw_products.SetProperty("units
instock.Color", modify)

For a DataWindow object property expression, the default value is followed by a tab character ("~t") and then the expression used to derive the value.

Now let's take a look at some of the code we had to implement to give the DataWindow control a bit more of a DataGrid look-and-feel. One item of code was added to the Click event handler for the DataWindow control in the form (see below). It sets the row to selected if the user clicks on the first column of the row. Note the SelectRow(0,False) call, which deselects all previously selected rows. If we wanted to allow multi-selection, we would check the selected status of the current row and toggle it rather than clearing the selection status of all rows.

dw_products_Click

Dim clickedobject As Sybase.
  DataWindow.ObjectAtPointer
clickedobject = CType(sender,
  DataWindowControl).ObjectUnderMouse()
Select Case clickedobject.Band.Type
   Case Sybase.DataWindow.BandType.Detail
   CType(sender, DataWindowControl).SelectRow(0, False)
   If clickedobject.RowNumber > 0 Then
    If clickedobject.Gob.Name = "c_1" Then
   CType(sender, DataWindowControl)
    .SelectRow(clickedobject.
    RowNumber, True)
    End If
   End If
End Select

Subclassed DataWindow Control
The remainder of the additional code is in our subclass of the DataWindow control. The first thing we did is override the WndProc method. One problem with the DataWindow control is that if you make the title bar visible, it also makes the control moveable. To prevent the control from being moved, we monitor for the WM_SYSCOMMAND message (which is passed to WndProc) and determine if a SC_MOVE is being attempted. If so, we return from the overloaded method without calling the ancestor - otherwise we call the ancestor so normal processing can continue.

WndProc

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
Const WM_SYSCOMMAND As Long = &H112
Const SC_MOVE As Int32 = &HF010
If m.Msg = WM_SYSCOMMAND Then
   'The four low order bits of wParam are used internally by Windows,
   'so we need to do a bitwise and with FFF0 to get the real value.
   If ((m.WParam.ToInt32 And &HFFF0) = SC_MOVE) Then Exit Sub
End If
MyBase.WndProc(m)
End Sub


Page 2 of 4   « previous page   next page »

About Bruce Armstrong
Bruce Armstrong is a development lead with Integrated Data Services (www.get-integrated.com). A member of TeamSybase, he has been using PowerBuilder since version 1.0.B. He was a contributing author to SYS-CON's PowerBuilder 4.0 Secrets of the Masters and the editor of SAMs' PowerBuilder 9: Advanced Client/Server Development.

alvin wrote: how can i change the highlight color of selected row? the default is blue which looks heavy. thanks
read & respond »
Bruce Armstrong wrote: I'd suggest posting your question in Sybase's DataWindow.Net forum. http://www.sybase.com/det ail?id=1031119 It would be much easier to address there.
read & respond »
Praveen wrote: Sir , We are using Datawindow.net 2.0[Trail] in VS2005 with ASP.Net 2.0 & C#.Net . Using WebDatawindow.net in ASP.Net data retrieving and also data interting are so smoth . We using connection ADO.net & ASP.Net security trust level default security level ( full trust level ) For security problem most of developer suggests that 'medium trust'and also our webserver support only medium trust.So we are changing Security policy to 'medium'. ( In %windir%\ Microsoft.NET\Framework\v 2.0.50727\CONFIG\Web.Conf ig to change to ) ***********Then I am facing a crucial problem ************ Required permissions cannot be acquired. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where...
read & respond »
Bruce Armstrong wrote: I'd suggest posting your question in Sybase's DataWindow.Net forum: http://www.sybase.com/det ail?id=1031119 It would be much easier to address there.
read & respond »
Atif Riaz wrote: Hey this is atif i have read your articles they were really help full. what i am trying is that i am using data window 2.0 in Dotnet now i want any CSS file like we do it for html or asp pages i want to define CSS file from where i can formate the report made in Data Window automatically. I need your help?
read & respond »
Bruce Armstrong wrote: If you can't find it here, check the Sybase CodeXchange site. I loaded it there as well: http://datawindownet.code xchange.sybase.com/servle ts/ProjectDocumentList?fo lderID=566&expandFolder=5 66&folderID=567
read & respond »
Mike wrote: Where is an arcticle code?
read & respond »
SYS-CON Australia News Desk wrote: In this article, we're going to look at how DataWindow.NET technology is a simpler but more powerful way of formatting data in the presentation layer. We'll be taking a sample application provided by Microsoft for .NET and implementing it using DataWindow.NET technology.
read & respond »
MICROSOFT .NET LATEST STORIES
"Cloud Computing Is the Plan" - Ballmer Memo
With Microsoft mandarin Kevin Johnson bolting to Jupiter, leaving Microsoft to lick its wounds over Yahoo and reorganize, CEO Steve Ballmer sent out an all-hands e-mail to Microsoft folk encapsulating the message he delivered to financial analysts gathering in Redmond Thursday. Ballmer
Adobe's Kevin Lynch and Microsoft's Scott Guthrie to Keynote AJAX World RIA Conference & Expo
Two of the biggest launches in Rich Internet Application history took place in 2007/2008 when Adobe launched AIR 1.0 in February '08 and Microsoft launched Silverlight (September '07). At the 6th International AJAXWorld RIA Conference & Expo in October SYS-CON Events is delighted to be
Virtualization, Microsoft, Yahoo & Google
Citrix has tapped its VP of channels and emerging product sales Al Monserrat to replace its departing sales chief John Burris, who, as previously reported, is going to Sourcefire as CEO. A couple of years ago Monserrat was responsible for Citrix' North American sales. Meanwhile, Citrix
AJAX RIA Tutorial - Accessing the ASP.NET Authentication, Profile and Role Service in Silverlight
In ASP.NET 2.0, we introduced a very powerful set of application services in ASP.NET (Membership, Roles and profile). In 3.5 we created a client library for accessing them from Ajax and .NET Clients and exposed them via WCF web services. For more information on the base level ASP.NET
Cloud Computing - IBM's Got Its Head in the Clouds
Reminding people of how its backing was the making of Linux, IBM, to no one's surprise, has thrown its support behind cloud computing, that delicious nexus of every chi-chi buzzword technology currently in vogue: Web 2.0, rich Internet applications, software-as-a-service, SOA, grid com
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS
SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021


SYS-CON FEATURED WHITEPAPERS

ADS BY GOOGLE
BREAKING NEWS FROM THE WIRES
Anexeon Achieves Microsoft Gold Partner Certification
Anexeon, LLC announced today it has achieved Microsoft Gold Certified Partner status along wit