|

UML design

Preview
Introduction
The DataGrid control is one of the worst things to work with, yet very needed. The real problem about this control is printing the grid. This class supports RightToLeft printing, colored and aligned grid printing.
Background
I had to do a project that supports RightToLeft grid printing using VB.NET, but all the printing classes I found just didn't do the job. I found a project but it was written using C# and I had to write the project using VB.NET. So I have rewritten the same project in DataGrid Printing Class V1.0b by nashcontrol in VB.NET.
Implementation
The main class is PrinterClass, it does the actual print. Grid is a class holding the DataGrid data, it's made of cells and headers. Cell represents a single cell in a grid with location, font alignment etc. Header represents a single header cell (inherits cell).
Using the code
Grid Creation: Creating the Constructor for the GridPublic Sub New(ByVal TheGrid As DataGrid)
Try
Dim TableGrid As DataTable = Nothing
If (TheGrid.DataSource.GetType() Is GetType(DataView)) Then
Dim ViewGrid As DataView = CType(TheGrid.DataSource, DataView)
TableGrid = ViewGrid.Table
Else
TableGrid = CType(TheGrid.DataSource, DataTable)
End If
row = TableGrid.Rows.Count
If (TheGrid.TableStyles.Count = 0) Then
CreateColumnStyles(TheGrid, TableGrid)
Else
If (TheGrid.TableStyles(TableGrid.TableName).GridColumnStyles.Count=0) Then
CreateColumnStyles(TheGrid, TableGrid
End If
End If
column = TheGrid.TableStyles(TableGrid.TableName).GridColumnStyles.Count
Cells = New Cell(Rows, Columns) {}
Dim i As Integer
For i = 0 To Rows - 1
Dim j As Integer
For j = 0 To Columns - 1
Cells(i, j) = New Cell(i, j, TheGrid.Font, _
TheGrid.TableStyles(TableGrid.TableName).GridColumnStyles(j).Alignment,_
New RectangleF(0, 0, TheGrid.GetCellBounds(i, j).Width, _
TheGrid.GetCellBounds(i, j).Height), TheGrid(i, j).ToString())
Next
Next
Headers = New Header(column) {}
SetHeaders(TheGrid, TableGrid)
SetColors(TheGrid)
Catch e As Exception
Console.WriteLine(e.Message)
End Try
End Sub
Grid PrintingPrivate Function PrintDataGrid(ByVal g As Graphics) As Boolean
Dim sf As StringFormat = New StringFormat
PageCounter = PageCounter + 1
If (bRightToLeft) Then
CurrentX = PrintDoc.DefaultPageSettings.PaperSize.Width_
- PrintDoc.DefaultPageSettings.Margins.Right
sf.FormatFlags = StringFormatFlags.DirectionRightToLeft
Else
CurrentX = PrintDoc.DefaultPageSettings.Margins.Left
End If
Dim i As Integer
For i = CurrentRow To PrintGrid.Rows - 1
Dim j As Integer
For j = 0 To PrintGrid.Columns - 1
Select Case (PrintGrid.Cell(i, j).Alignment)
Case HorizontalAlignment.Left
sf.Alignment = StringAlignment.Near
Case HorizontalAlignment.Center
sf.Alignment = StringAlignment.Center
Case HorizontalAlignment.Right
sf.Alignment = StringAlignment.Far
End Select
If (bRightToLeft) Then
g.FillRectangle(New SolidBrush(PrintGrid.BackColor), _
CurrentX - PrintGrid.Cell(i, j).Width, CurrentY, _
PrintGrid.Cell(i, j).Width, PrintGrid.Cell(i, j).Height)
g.DrawRectangle(New Pen(PrintGrid.LineColor), _
CurrentX - PrintGrid.Cell(i, j).Width, CurrentY, _
PrintGrid.Cell(i, j).Width, PrintGrid.Cell(i, j).Height)
g.DrawString(PrintGrid.Cell(i, j).CText, _
PrintGrid.Cell(i, j).Font, New SolidBrush(PrintGrid.ForeColor),_
New RectangleF(CurrentX - PrintGrid.Cell(i, j).Width, _
CurrentY, PrintGrid.Cell(i, j).Width, _
PrintGrid.Cell(i, j).Height), sf)
CurrentX -= PrintGrid.Cell(i, j).Width
Else
g.FillRectangle(New SolidBrush(PrintGrid.BackColor), _
CurrentX, CurrentY, PrintGrid.Cell(i, j).Width, _
PrintGrid.Cell(i, j).Height)
g.DrawRectangle(New Pen(PrintGrid.LineColor), CurrentX, _
CurrentY, PrintGrid.Cell(i, j).Width, _
PrintGrid.Cell(i, j).Height)
g.DrawString(PrintGrid.Cell(i, j).CText, _
PrintGrid.Cell(i, j).Font, New SolidBrush(PrintGrid.ForeColor),_
New RectangleF(CurrentX, CurrentY, _
PrintGrid.Cell(i, j).Width, PrintGrid.Cell(i, j).Height), sf)
CurrentX += PrintGrid.Cell(i, j).Width
End If
Next
If (bRightToLeft) Then
CurrentX = PrintDoc.DefaultPageSettings.PaperSize.Width_
- PrintDoc.DefaultPageSettings.Margins.Right
Else
CurrentX = PrintDoc.DefaultPageSettings.Margins.Left
End If
CurrentY += PrintGrid.Cell(i, 0).Height
CurrentRow += 1
If (CurrentY > PrintDoc.DefaultPageSettings.PaperSize.Height_
- PrintDoc.DefaultPageSettings.Margins.Bottom) Then
Return True
End If
Next
Return False
End Function
Current Todo:
- Fix - When grid is empty, header is not printed well.
- When grid is lager than the width of the page, continue to next.
Other DataGrid Printing Classes
Acknowledgements
Thanks to nashcontrol who has given me permission to upload the project converted into VB.NET.
| You must Sign In to use this message board. |
|
| | Msgs 1 to 25 of 35 (Total in Forum: 35) (Refresh) | FirstPrevNext |
|
|
 |
|
|
Hi friend, my problem is,
I have one form which contains so many textboxes. And one datagrid also. Datagrid is using for listing the size,color etc. of item code. Iam not created the report, Instead I want to print like that form itself. I am using formprint to print the form.But datagrid was connected to the table so that it is not displaying. Is there any method to print datagid along with the contents of the form.
I used this code,it will give print as an image.Not like report
e.Graphics.DrawImage(formImage, 100, 100) also I used Dim fp As New FormPrinting.FormPrinting(Me) fp.Print()
but still problem.Datagrid not printing along with form by rani
rani
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
You have to write both the codes(form print and datagrid print).
Thanks
Nidhi Shrivastava Freelance Software Developer Email: nishiddn@yahoo.com
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi All
Nice article and well done Nidi.. Well anyone able to find the solution for multiple page print ??
Please let me know
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
I am having a problem with my SMS application. I can send SMS's fine in PDU mode but once i go over 160 characters the message doesnt get sent and I get a +CMS ERROR:305. i thought moving onto using PDU mode it will work but still doesnt.Now I dont know what to do.... Pleez help .I used a class that i downloaded to encode the text into PDU mode.My code is as follows:
For countEncoding = 0 To lstReceiving.Items.Count - 1 strEncoded = encoder.EncodePDU(txtMessage.Text, lstReceiving.Items(countEncoding).ToString, "", intActualPDULength, True) encoder = Nothing EncodePDUandSend = strEncoded
MessageBox.Show(CStr(intActualPDULength) & vbCrLf & strEncoded) 'Send AT command to modem to test connectivity to it myComm.Output = "AT" & Chr(13)
'Set message format to PDU mode myComm.Output = "AT+CMGF=0" & Chr(13)
'Set the actual PDU length of message myComm.Output = "AT+CMGS=" & CStr(intActualPDULength) & Chr(13)
myComm.Output = strEncoded & Chr(26) MessageBox.Show(myComm.Input())
Next
|
| Sign In·View Thread·PermaLink | 1.50/5 (2 votes) |
|
|
|
 |
|
|
Hi,
I need to search records in a database using radio buttons. I have a InputBox where the user write for example the code of an article that he need to find. When I get that inputbox I need that the program make search with the inputbox. I try it with datareader and the only thing that I get is if I put the position of the record in the table I can see it but with the inputbox I can't. I hope you can help me with this.
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
Write a sql query on change of the inputbox.
Nidhi Shrivastava Freelance Software Developer Email: nishiddn@yahoo.com
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi. Printing is perfect bt when i try to "print with header and footer" it just prints the headings and doesnt print the contents of the grid?? plz help me out
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
 |
|
|
hi dear.. i am just a novice for vb.net and want to print my datagrid using this code. but i dont know how to use it.. can u help me on my msn. makeafirstmove@hotmail.com i will be grateful. thanks
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I have a datagrid that is very long and when I try to print it, it only prints about half the grid. So my question is, how do I get it to print using a landscape layout, and not portrait.
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
|
Did you solve the printing datagrid problem? i just download and see your datagrid printing project. For the 1st page can print out, but for the second page it only print one row of data.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
whem iam writing code for printing a datagrid in asp.net,i get the error message "tablestyle" not declared.how can i solve this problem
ffrf
|
| Sign In·View Thread·PermaLink | 1.50/5 (2 votes) |
|
|
|
 |
|
|
Hi there, when I use in my code... the header is missing (I have a Datagrid populated from an Oracle query)
Can this be solved??
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
I am using a datagrid to view some items in VB.Net. It loads fine and displays all the correct data I just have one problem. I have the user enter an item number then use the dataview to locate the row the item is in. I then want to choose that row and have it visible to the customer. so its going something like this:
itemNum = promptItemNumber() If itemNum = "" Then return
Dim row As Integer = myDataView.Find(itemNum) If row > -1 Then myDataGrid.NavigateTo(row, "")
This selects the correct item(it has the arrow next to the row such as in the picture at the top of the artical) the only problem is that when there are enough rows to require scroll bars it doesn't display scoll all the way to the item it is one click to high. Any ideas?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I am using a datagrid to view some items in VB.Net. It loads fine and displays all the correct data I just have one problem. I have the user enter an item number then use the dataview to locate the row the item is in. I then want to choose that row and have it visible to the customer. so its going something like this:
itemNum = promptItemNumber() If itemNum = "" Then return
Dim row As Integer = myDataView.Find(itemNum) If row > -1 Then myDataGrid.NavigateTo(row, "")
This selects the correct item(it has the arrow next to the row such as in the picture at the top of the artical) the only problem is that when there are enough rows to require scroll bars it doesn't display scoll all the way to the item it is one click to high. Any ideas?
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
I want to change the size (width)of the columns in the datagrid and in the print so that the first colomn has the width of 50; the second column the width of 70 etc; How can i do it? Thanks
Gsax
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hello,
Try to change the height and width of the cell in the New(TheGrid as Datagrid).
Thanks. Nidhi
Nidhi Shrivastava Freelance Software Developer Email: nishiddn@yahoo.com
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Sorry, but I'm not able to change the width of the cell! In your example, how would you change the width of the second column (company)? I tried but nothing has changed. Thank you for your help. Gsax
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I am having a problem with my printing. My table width is too big for my page and my columns are getting cut out. I need some code that will cause a page break every time the columns get too big for the width of the page and cause a Page Break also for the rows.
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
|
Hello,
You can add and if condition if the lenth of the contents are big then incerase the sixe of the column.
Thanks. Nidhi
Nidhi Shrivastava Freelance Software Developer Email: nishiddn@yahoo.com
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
OLD: (Class Grid)
Public Sub New(ByVal TheGrid As DataGrid) Try '//get the Data in the grid Dim TableGrid As DataTable = Nothing If (TheGrid.DataSource.GetType() Is GetType(DataView)) Then Dim ViewGrid As DataView = CType(TheGrid.DataSource, DataView) TableGrid = ViewGrid.Table Else TableGrid = CType(TheGrid.DataSource, DataTable) End If
'//set number of rows row = TableGrid.Rows.Count " ViewGrid.RowFilter <> ""
NEW : (Class Grid)
Public Sub New(ByVal TheGrid As DataGrid) Try '//get the Data in the grid Dim TableGrid As DataTable = Nothing If (TheGrid.DataSource.GetType() Is GetType(DataView)) Then Dim ViewGrid As DataView = CType(TheGrid.DataSource, DataView) TableGrid = ViewGrid.Table row = ViewGrid.Count Else TableGrid = CType(TheGrid.DataSource, DataTable) row = TableGrid.Rows.Count End If
'//set number of rows
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
First: I not speaken Englisch (only Spanisch and Deustch)
Spanisch:
El articulo es excelente, me fue de gran utilidad. Me ayudo mucho, El unico problema que tuve fue cuando dimensionaba la Datagrid, al imprimirla el Heigth del TextBox (TextBox.Height) pasa a ser cero "0" entonces no se imprime, la solucion fue la siguiente
Replace : IN: Private Sub SetHeaders(ByVal TheGrid As DataGrid, ByVal TableGrid As DataTable)
OUT: Headers(i) = New Header(i, TheGrid.HeaderFont, columnStyle.Alignment, New RectangleF(Cells(0, i).Location.X, columnStyle.TextBox.Bounds.Y, Cells(0, i).Location.Width, CType(TheGrid.TableStyles(TableGrid.TableName).GridColumnStyles(0), DataGridTextBoxColumn).TextBox.Height), columnStyle.HeaderText)
FOR: Headers(i) = New Header(i, TheGrid.HeaderFont, columnStyle.Alignment, New RectangleF(Cells(0, i).Location.X, columnStyle.TextBox.Bounds.Y, Cells(0, i).Location.Width, TheGrid.TableStyles(TableGrid.TableName).GridColumnStyles(i).DataGridTableStyle.RowHeaderWidth), columnStyle.HeaderText)
-- modified at 15:26 Tuesday 18th July, 2006
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
hi thanx for the help!!! but i hava problem with print and print preview. the application raising an error on clicking the print button that error : Object reference not set to an instance of an Objcet.
plz help any one
Anil
|
| Sign In·View Thread·PermaLink | 1.00/5 (2 votes) |
|
|
|
 |
|
|
General News Question Answer Joke Rant Admin
|