IWETHEY v. 0.3.0 | TODO
1,095 registered users | 0 active users | 0 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New Datagrids suck/don't suck in .NET
I have a VB.Net program with a form containing a datagrid control. I have the VB.Net app reading from a SQL Server table, returning only 2 columns: the Product Code and the Product Description. The code is a 3 byte varchar field and the description is a 50 byte varchar field.

I want to display the data such that the code takes up about 20% of the datagrid, while the description uses the remaining 80%. However, one of the properties of a datagrid is "PreferredColumnWidth" which defaults to a value of 75 units. I want the Product Code column width to be around 50 units and the Product Description column to be around 200 units. This can only be done in the VB.Net code using a "TableStyle" and "ColumnStyle" attributes that you add to the datagrid control.

THIS DOESN'T WORK!

The "PreferredColumnWidth" value OVERRULES my settings created in code, even though the documentation says it should be the other way around.

Sadly, creating a screen in ASP.Net that uses VB.Net as its "code behind", containing a datagrid displaying the exact same data pulled from the same SQL Server table displays EXACTLY THE WAY I WANT IT TO DISPLAY - PERFECTLY PROPORTIONAL.

I've posted this problem on half a dozen forums asking for a solution, including a newsgroup available on the support.microsoft web site, but no one has answered me yet.
lincoln

"Chicago to my mind was the only place to be. ... I above all liked the city because it was filled with people all a-bustle, and the clatter of hooves and carriages, and with delivery wagons and drays and peddlers and the boom and clank of freight trains. And when those black clouds came sailing in from the west, pouring thunderstorms upon us so that you couldn't hear the cries or curses of humankind, I liked that best of all. Chicago could stand up to the worst God had to offer. I understood why it was built--a place for trade, of course, with railroads and ships and so on, but mostly to give all of us a magnitude of defiance that is not provided by one house on the plains. And the plains is where those storms come from." -- E.L. Doctorow


Never apply a Star Trek solution to a Babylon 5 problem.


I am not merely a "consumer" or a "taxpayer". I am a Citizen of the United States.


[link|mailto:bconnors@ev1.net|contact me]
New They do suck, but try this
Assuming you are using a typed DataSet as your data source (this can all be done using the Designer):

- Set your DataGrid's DataSource to the DataSet (not the DataTable in the DataSet)
- Set the DataMember to the DataTable withing the DataSet
- Add a DataGridTableStyle to the collection
- Set its MappingName to the DataTable name (should be in the drop down)
- Add your DataGridColumnStyles to the TableStyle.
- Check their MappingName properties. They should get auto-filled. If not select them in the drop down.
- Set your desired widths and other properties.
- Click OK a few times.

I think the critical step is having the MappingName match the table name in the TableStyle. The first two steps let you do the third so you don't have to go looking for the table name elsewhere.

I've never really used the DataGrid in .Net 1.1 becuase I found it lacking in a number of other areas. Others, including MS seem to agree as it's been completely reqwritten as the DataGridView in .Net 1.0
--
Chris Altmann
Expand Edited by altmann March 24, 2006, 07:45:52 PM EST
New So this is easier than writing your own HTML?
===

Purveyor of Doc Hope's [link|http://DocHope.com|fresh-baked dog biscuits and pet treats].
[link|http://DocHope.com|http://DocHope.com]
New To create a editable grid in a Windows program? Yes.
--
Chris Altmann
New That worked, but I prefer to do things in code
The only way that this would work was to assign a connection, data adapter and dataset objects at design time to this form. Then, it's click, click, click, click, click, etc., to set everything in their properties. When I eventually move the database to our SAN, I'll have to manually adjust the properties of each of these objects.

I prefer to build these objects in code, so when something like the database move occurs, all I have to do is change one line of code (the global connection string), recompile, and then I'm done.
lincoln

"Chicago to my mind was the only place to be. ... I above all liked the city because it was filled with people all a-bustle, and the clatter of hooves and carriages, and with delivery wagons and drays and peddlers and the boom and clank of freight trains. And when those black clouds came sailing in from the west, pouring thunderstorms upon us so that you couldn't hear the cries or curses of humankind, I liked that best of all. Chicago could stand up to the worst God had to offer. I understood why it was built--a place for trade, of course, with railroads and ships and so on, but mostly to give all of us a magnitude of defiance that is not provided by one house on the plains. And the plains is where those storms come from." -- E.L. Doctorow


Never apply a Star Trek solution to a Babylon 5 problem.


I am not merely a "consumer" or a "taxpayer". I am a Citizen of the United States.


[link|mailto:bconnors@ev1.net|contact me]
New Like I said, this (datagrids) is easier than writing code?
===

Purveyor of Doc Hope's [link|http://DocHope.com|fresh-baked dog biscuits and pet treats].
[link|http://DocHope.com|http://DocHope.com]
New Oh, come the heck on! Six or eight drop-down selections!
OK, in Delphi it would be even fewer properties to set... But still. Six or eight fucking selections in drop-down lists, where it's debatable whether to count each selection as one or two mouse-clicks; I'd say one-and-a-half mouse-clicks per selection.

Makes some nine to twelve mouse-clicks.

How the fuck is that NOT easier than writing code?


   [link|mailto:MyUserId@MyISP.CountryCode|Christian R. Conrad]
(I live in Finland, and my e-mail in-box is at the Saunalahti company.)
Yes Mr. Garrison, genetic engineering lets us correct God's horrible, horrible mistakes, like German people. - [link|http://maxpages.com/southpark2k/Episode_105|Mr. Hat]
New My experience with it
I had a developer working (Visual Studio .NET) on a page that was a simple, tabular display of the results of a query. Basic Reporting 101. There was a parent/children relationship in the data, and the request was to group all children together, and leave a blank space after each group. After re-writing the page four times, changing the column layout twice, complaining that he couldn't do what I asked with late binding, re-writing it and changing the column layout again in order to remove the late binding, he still couldn't make it do what he wanted.

I'd have had the page done in about five minutes using any of a half-dozen freely-available frameworks, or two that I've done myself. If I wanted to do a one-off, I could have done it in a half-hour.

So while I'll admit it may be possible to do things with a data grid that are hard to do manually, I've gotten the impression it also makes it very hard to do some simple things, if the simple thing you want isn't one of the default behaviors.

Or I was working with a moron. That's always a possibility.
===

Purveyor of Doc Hope's [link|http://DocHope.com|fresh-baked dog biscuits and pet treats].
[link|http://DocHope.com|http://DocHope.com]
New Again, we're talking about client side Windows apps here.
--
Chris Altmann
New Come on, let me slam Winders, please?
The moron who was having all these problems was the same one who told me he treated the browser window as a client display surface, and wrote everything as though it were a desktop Windows app. He let the tools deal with making it all work. And he was constantly fighting with the tools to do it.

So yes, a datagrid for a desktop application is probably the bee's knees. But I've never heard a good word about using them in a web app.
===

Purveyor of Doc Hope's [link|http://DocHope.com|fresh-baked dog biscuits and pet treats].
[link|http://DocHope.com|http://DocHope.com]
New Like shooting fish in a barrel
ASP.Net requires that you manage your events wisely. ASP.Net's event model makes too heavy a reliance on round trips. For what it does, it's not bad. But the framework is not particular geared towards fine-tuning the processing on the client side.
New BTW, I just checked: In Delphi, it's zero properties to set.
At least if you happen to be using the Live Data functionality, opening your queries at design-time so you can study how your app looks with data to display...

Then you just drag the edges of the column headers, and the width you drag them to is what'll be compiled into your app.

Counting a click-and-drag as one-and-a-half clicks, like I did for drop-down-and-select of properties, that makes a grand total of one-and-a-half clicks.

But yeah, writing code must of course be sooo much more efficient...


   [link|mailto:MyUserId@MyISP.CountryCode|Christian R. Conrad]
(I live in Finland, and my e-mail in-box is at the Saunalahti company.)
Yes Mr. Garrison, genetic engineering lets us correct God's horrible, horrible mistakes, like German people. - [link|http://maxpages.com/southpark2k/Episode_105|Mr. Hat]
New So do that.
"I prefer to build these objects in code, so when something like the database move occurs, all I have to do is change one line of code (the global connection string), recompile, and then I'm done."

You can put the conn string in a config file and still point and click everything else. In .Net 2.0 it even does it for you by default.

No recompile in either case.
--
Chris Altmann
New You're overlooking
how the Datagrid control in VB.Net is supposed to be exactly the same one in ASP.Net (at least, according to Microsoft).

When assigning a dataset to a grid by using

Grid.Datasource = dataset retrieved from SQL table

in an ASP.Net program (using VB.net as the "code behind"), it does not require ANY TableStyle or GridColumn properties to be set - it displays the data proportionally AUTOMATICALLY. Using a datagrid in a VB.Net Windows Forms program DOES. And doing it in the code DOESN'T work; you have to click and set properties at design time.

And that's the source of my frustration.
lincoln

"Chicago to my mind was the only place to be. ... I above all liked the city because it was filled with people all a-bustle, and the clatter of hooves and carriages, and with delivery wagons and drays and peddlers and the boom and clank of freight trains. And when those black clouds came sailing in from the west, pouring thunderstorms upon us so that you couldn't hear the cries or curses of humankind, I liked that best of all. Chicago could stand up to the worst God had to offer. I understood why it was built--a place for trade, of course, with railroads and ships and so on, but mostly to give all of us a magnitude of defiance that is not provided by one house on the plains. And the plains is where those storms come from." -- E.L. Doctorow


Never apply a Star Trek solution to a Babylon 5 problem.


I am not merely a "consumer" or a "taxpayer". I am a Citizen of the United States.


[link|mailto:bconnors@ev1.net|contact me]
New You've got your mappings wrong
If you are ChicagoBoy and [link|http://groups.google.com/group/microsoft.public.dotnet.framework.aspnet.datagridcontrol/browse_frm/thread/8777eb1801fc8510/216709814fc7d3f0?lnk=st&q=datagrid+proportionally&rnum=7#216709814fc7d3f0|this] is your post then...

a) This code doesn't compile becuase Tables is not a member of SqlDataAdapter. I think you meant MyDataset.Tables(0)
b) You map the 2nd column to ProductDesc when it's called ProductName in the SQL query.

If this wasn't you or if these were transcription errors then I apologize but it does look just like what you are talking about, so lets move on.

The problem lies in these two lines:
MyDataset = New DataSet("Products")
MyDataAdapter.Fill(MyDataset)

The first line names the Dataset "Products"
The 2nd line doesn't name the table within the Dataset so it gets assigned a default name "Table". Further down you set myGridStyle.MappingName = "Products". That doesn't match "Table" so your style gets ignored. You should either set the MappingName to "Table", or better yet, change your Fill call to:
MyDataAdapter.Fill(MyDataset, "Products")
You might consider naming (the string name) your Dataset something else to avoid confusion.

As to why this is so complex and different from ASP.Net's Datagrid? The WinForms datagrid supports drilling down into hierarchical data, so it has support for these mappings so that the appropriate styles and column settings can be retrieved as the user drills into the data. The ASP.Net DataGrid doesn't have this feature.

As to why the WinForms DataGrid doesn't support proportional column sizing? My guess is they didn't want to have to loop thru all the column values, measuring each's length with the current font. ASP.Net's DataGrid can get away with this because:
a) It's made to display data in pages of limited length.
b) The browser can size the columns based on the generated HTML

The .Net 2.0 DataGridView has options for autosizing columns based on content or by setting relative weights for each column. It also doesn't do the hierarchical thing so you have one less level of abstraction to worry about
--
Chris Altmann
Expand Edited by altmann March 30, 2006, 01:29:35 AM EST
New On to the future!

The .Net 2.0 DataGridView has options for autosizing columns based on content or by setting relative weights for each column. It also doesn't do the hierarchical thing so you have one less level of abstraction to worry about


I looking forward to convincing my boss to go to .Net 2.0, so I don't have to keep jumping through these hoops. He'll probably agree, as long as it doesn't cost us too much out-of-pocket.

BTW, I tried your code suggestions - it still doesn't work. Pisser.

lincoln

"Chicago to my mind was the only place to be. ... I above all liked the city because it was filled with people all a-bustle, and the clatter of hooves and carriages, and with delivery wagons and drays and peddlers and the boom and clank of freight trains. And when those black clouds came sailing in from the west, pouring thunderstorms upon us so that you couldn't hear the cries or curses of humankind, I liked that best of all. Chicago could stand up to the worst God had to offer. I understood why it was built--a place for trade, of course, with railroads and ships and so on, but mostly to give all of us a magnitude of defiance that is not provided by one house on the plains. And the plains is where those storms come from." -- E.L. Doctorow


Never apply a Star Trek solution to a Babylon 5 problem.


I am not merely a "consumer" or a "taxpayer". I am a Citizen of the United States.


[link|mailto:bconnors@ev1.net|contact me]
New Might take a look at this too
Be aware (beware) that VS.Net 2005 is somewhat designed for SQL Server 2005. It sounds like you can still use it with SQL Server 2000, but I don't know what pitfalls await there (MS tying products together?!? Shocking!). ASP.Net 2.0 is quite different from ASP.Net 1.1 as well. Fortunately you can run VS.NET 2003 and VS.NET 2005 side by side.

If you can solve whatever other problems you are having with the DataGrid, here's how to do autosizing of columns with the DataGrid: [link|http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c44c.aspx#q877q|http://www.syncfusio...Q_c44c.aspx#q877q] That FAQ is a good reference overall for WinForms programming.

In case we missed something else, here's the code based on yours that worked for me with a empty form on which I dropped a DataGrid named DisplayGrid and a OleDbConnection named objCon. I also used an Access DB instead of SQL Server. And I've excised the designer generated code (which BTW, is where all that point and click config of the DataGrid from my original solution would appear as code).

\nPublic Class Form1\n    Inherits System.Windows.Forms.Form\n\n#Region " Windows Form Designer generated code "\n...\n#End Region\n    Dim MyDataAdapter As System.Data.OleDb.OleDbDataAdapter\n    Dim MyDataset As System.Data.DataSet\n\n    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load\n        Dim strSQL As String\n\n        strSQL = "SELECT DISTINCT ProductCode, ProductName FROM Products"\n        strSQL = strSQL & " WHERE ProductName IS NOT NULL ORDER BY ProductName"\n        MyDataAdapter = New System.Data.OleDb.OleDbDataAdapter(strSQL, objCon)\n        MyDataset = New DataSet("ProductsDataset")\n        MyDataAdapter.Fill(MyDataset, "Products")\n        DisplayGrid.DataSource = MyDataset.Tables("Products")\n\n\n        Dim myGridStyle As DataGridTableStyle = New DataGridTableStyle\n        myGridStyle.MappingName = "Products"\n        ' \n        '     The ProductCode is a 3 byte varchar field \n        ' \n        Dim myFirstColumn As DataGridColumnStyle = New DataGridTextBoxColumn\n        myFirstColumn.MappingName = "ProductCode"\n        myFirstColumn.HeaderText = "Code"\n        myFirstColumn.Width = 40\n        myFirstColumn.Alignment = HorizontalAlignment.Center\n        ' \n        '     The ProductDesc is a 50 byte varchar field \n        ' \n        Dim mySecondColumn As DataGridColumnStyle = New DataGridTextBoxColumn\n        mySecondColumn.MappingName = "ProductName"\n        mySecondColumn.HeaderText = "Name"\n        mySecondColumn.Width = 400\n        mySecondColumn.Alignment = HorizontalAlignment.Left\n\n\n        myGridStyle.GridColumnStyles.Add(myFirstColumn)\n        myGridStyle.GridColumnStyles.Add(mySecondColumn)\n\n\n        DisplayGrid.TableStyles.Add(myGridStyle)\n    End Sub\nEnd Class\n


Good luck!
--
Chris Altmann
New VS 2005 (.NET 2.0) cost depends on the level
Professional isn't too bad (<$500/seat upgrade), and I guess Enterprise Developer (VS2005 ED) might not be too bad if you're already paying for MSDN Premium, otherwise it's way more expensive than before - at about $2300/seat upgrade - since you have to get it with MSDN Premium.

VS2005 Enterprise Kitchen Sink is about $10,000/seat, not including the various server thingies (TeamServer?)

We have VS2001 Enterprise Developer, looked at the price to upgrade to VS2005 ED, laughed, and upgraded to VS2005 Professional. I saw no value in VS2005 ED, since all the interesting extras have open source equivalents that are probably better (e.g. Subversion for version control, NUnit for unit testing).

The big advantage of .NET 2.0 is, of course, being able to run IronPython.

--Tony
     Datagrids suck/don't suck in .NET - (lincoln) - (17)
         They do suck, but try this - (altmann) - (16)
             So this is easier than writing your own HTML? -NT - (drewk) - (1)
                 To create a editable grid in a Windows program? Yes. -NT - (altmann)
             That worked, but I prefer to do things in code - (lincoln) - (13)
                 Like I said, this (datagrids) is easier than writing code? -NT - (drewk) - (6)
                     Oh, come the heck on! Six or eight drop-down selections! - (CRConrad) - (5)
                         My experience with it - (drewk) - (3)
                             Again, we're talking about client side Windows apps here. -NT - (altmann) - (2)
                                 Come on, let me slam Winders, please? - (drewk) - (1)
                                     Like shooting fish in a barrel - (ChrisR)
                         BTW, I just checked: In Delphi, it's zero properties to set. - (CRConrad)
                 So do that. - (altmann) - (5)
                     You're overlooking - (lincoln) - (4)
                         You've got your mappings wrong - (altmann) - (3)
                             On to the future! - (lincoln) - (2)
                                 Might take a look at this too - (altmann)
                                 VS 2005 (.NET 2.0) cost depends on the level - (tonytib)

He done made them there squiggly lines into WORDS!
141 ms