web analytics

Print a DataGridView in VB.Net





VB.Net Tutorial:Datagridview Printing in VB.Net

There will be situations to just print a DataGridView in a application than designing again the columns and rows in a Crystal Report. But in .Net there is no straight forward way to do this or in other words this is not supported natively. This article is based on article from VB.Net Tutorial. To print the Datagridview we need to draw all the objects using Graphics Object given in the PrintDocument before calling print method in the respective PrintDocument.

PrintDocument and the Graphics Object

So as I earlier said we need to actually draw all including text using the Graphics object. The print document needs to be prepared before actual printing starts. So we can use the event PrintPage, in this event we can draw all the objects as we need.

This is a versatile piece of the functionality of Datagridview – if you want to print out your data anyway. This tutorial should help you with the DataGridView – now you know what your broadband (http://www.o2.co.uk/broadband/) is for – great VB.net tips. You will be able to see your data looking organised, which will enable you to extract exactly what you want. Good luck with this.

Draw the DataGridView like a table using lines

To get a tabular data to represent the DataGridView, we need to do the following
• Draw an outer rectangle as the border to table
• Draw a line for every row
• Draw a line for every column while iterating the cell
• Draw the cell value as text
All the above said items can be drawn with the help of the DataGridView’s native properties, Left, Top, Height, Width, Row.Hieght, and Column. Width

You can use graphics object GxPrint to draw text lines and strings as follows

GxPrint.DrawString(CellText, PrintFont, Brushes.Gray, StartLeft, StartTop)
GxPrint.DrawLine(Pens.Black, StartLeft, StartTop, StartLeft, StartTop + PrintRow.Height)

Windows forms Combobox Autocomplete





VB.Net Tutorial:Combobox Autocomplete

ComboBox control is similar to ListBox control, in which you can select one item from a list of items. But it takes less space on screen and it allows you to locate an item by setting value to the ComboBox control’s text property. In simple word, it is an expandable(collapse/expand) ListBox control.

AutoCompleteMode Property

Here we are going to see the AutoCompleteMode property in ComboBox control. This property automatically matches the input string given on runtime (starts with match) of all strings in the source database column. Based on that it will display the matched string in ComboBox. This property is very useful for frequently searching strings. Such as URLs, file name, customer name, or any command that is frequently used. This property go well when there is no duplication in source data. If there is duplication occurs in source data then the AutoCompleteMode property omits the duplication and display only once.

Fetch data from SQL server

There is an easy way to fetch data from the SQL server, first select the “Add New Item” from Project menu. In which, select “LINQ to SQL Classes”, name it as Northwind.dbml and press Add button. Now you get the new Northwind.dbml designer on screen. Now drag the Customer table from server explorer window to Northwind.dbml. Now you can fetch the source data from SQL server by using the following code

ComboBox1.DataSource = (New NorthwindDataContext).Customers.Where(Function(Cust)
Cust.ContactName.Contains(comboBox1.SelectedText))

Implementation of AutoCompleteMode property

Pickup a ComboBox control from the toolbox and place it in your form1.vb[Design]. Now set the “DropDownStyle” property value as DropDown. To execute AutoCompleteMode property, The AutoCompleteModeProperty and AutoCompleteSource property must be used together. First you should set the AutoCompleteMode property value as Suggest and set AutoCompleteCustomSource property value as ListItems. If the value of AutoCompleteCustomSource property is null, then the prefix of the source data that gets matched with the input string will not get listed below the ComboBox .
The following code is used to set both the properties.

ComboBox1.AutoCompleteMode = AutoCompleteMode.Suggest
ComboBox1.AutoCompleteSource = AutoCompleteSource.ListItems

Javascript based Rich Text Editor for asp.net





VB.Net Tutorial: Iframe Based Editor using Javascript
Please don’t get confused with Rich Text Editor in classic VB. These controls are working with rich text format. The editor, I am explaining here also providing rich text editing. However the final format is HTML.
So it has its own limitations, For example it can not paste Image data directly. And it is actually a JavaScript which turns a iframe as a editor. So it is not written in asp.net but can be used in ASP.Net applications. This is what I am doing for few of my websites.
I don’t have a working source code for Rich Text Editor. However if you are just looking for an editor which supports hyperlinks and few word formatting, this article can help you to code one for your simple needs.
IFRAME as an editor
It is relatively very easy to build an editor using an IFRAME. But the real trouble comes in when trying to build an editor similar to a word processor like a Microsoft office word.
Bring the IFRAME to Editable mode using designMode
IFRAME can be used as an editor once the design mode is set to ‘ON’ as shown in the following code.


Native formatting abilities
The basic formatting like bold, italics, and underline natively supported with the shortcuts CTRL +B, CTRL + I, CTRL+U respectively. All the fully qualified URLS will be considered as hyperlinks once after pasting followed by pressing enter or pasting (next to the hyperlink). So without much code you can start using the IFRAME as an editor.
Specific formatting using a toolbar
If you still need more functionality like adding images and formatting using a tool bar .You may use the following syntax to fire commands
var EditorFrame = frames. EditorFrame.document.selection.createRange()
EditorFrame.execCommand(“Bold”)
You can use the commands using ‘execCommand’ on the Range object of the Selection. Majority of the operations can be programmed like this to function as you expect.

Go back to top