Namespace MyDataGridView ''' ''' display a datagrid with row numbers ''' Class MyDataGridView Inherits DataGridView ''' ''' draw the row number on the header cell, auto adjust the column width ''' to fit the row number ''' ''' Protected Overloads Overrides Sub OnRowPostPaint(ByVal e As DataGridViewRowPostPaintEventArgs) MyBase.OnRowPostPaint(e)
' get the row number in leading zero format, ' where the width of the number = the width of the maximum number Dim RowNumWidth As Integer = Me.RowCount.ToString().Length Dim RowNumber As New StringBuilder(RowNumWidth) RowNumber.Append(e.RowIndex + 1) While RowNumber.Length < RowNumWidth RowNumber.Insert(0, " ") End While
' get the size of the row number string Dim Sz As SizeF = e.Graphics.MeasureString(RowNumber.ToString(), Me.Font)
' adjust the width of the column that contains the row header cells If Me.RowHeadersWidth < CInt((Sz.Width + 20)) Then Me.RowHeadersWidth = CInt((Sz.Width + 20)) End If
' draw the row number e.Graphics.DrawString(RowNumber.ToString(), Me.Font, SystemBrushes.ControlText, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + ((e.RowBounds.Height - Sz.Height) / 2)) End Sub End Class End Namespace
|
| Author: DotNetMatrix 08 Jun 2009 | Member Level: Gold Points : 1 |
Dear Vijj,
Defiantly this is junk of code is good but, its throwing some errors. It'll good if you send complete code, like what the namespace we've to use all those stuff
|
| Author: Viji RAJKUMAR 08 Jun 2009 | Member Level: Diamond Points : 2 |
Hi,
Thanks for your reply. Please rate my reply if you find this helpful.
About MyDataGridView
1. This code is written in VB.NET
2. draws the row number in the dataGridView
3. It is a component. Means that you can drag it and use it anywhere as a tool(You can see this in Tool Box after successful building of your project)
Steps to follow: ================
1) You have to copy and paste the above code at the very bottom of any form's Code after End Class as it is a NameSpace.
Example: Assume you are having a project with three Forms.(Form1,Form2,Form3)
In Form1's Coding Part,
Public Class Form1
End Class
'Paste your code Here - At the very bottom
Namespace MyDataGridView ''' ''' display a datagrid with row numbers ''' Class MyDataGridView Inherits DataGridView ''' ''' draw the row number on the header cell, auto adjust the column width ''' to fit the row number ''' ''' Protected Overloads Overrides Sub OnRowPostPaint(ByVal e As DataGridViewRowPostPaintEventArgs) MyBase.OnRowPostPaint(e)
' get the row number in leading zero format, ' where the width of the number = the width of the maximum number Dim RowNumWidth As Integer = Me.RowCount.ToString().Length Dim RowNumber As New StringBuilder(RowNumWidth) RowNumber.Append(e.RowIndex + 1) While RowNumber.Length < RowNumWidth RowNumber.Insert(0, " ") End While
' get the size of the row number string Dim Sz As SizeF = e.Graphics.MeasureString(RowNumber.ToString(), Me.Font)
' adjust the width of the column that contains the row header cells If Me.RowHeadersWidth < CInt((Sz.Width + 20)) Then Me.RowHeadersWidth = CInt((Sz.Width + 20)) End If
' draw the row number e.Graphics.DrawString(RowNumber.ToString(), Me.Font, SystemBrushes.ControlText, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + ((e.RowBounds.Height - Sz.Height) / 2)) End Sub End Class End Namespace
No need to copy the code for every Form, Other wise will throw error.
2) Build the project
3) You should see MyDataGridView Component as a Tool in the ToolBox.
4. Just Drag and use it in places where you want to use the DataGridView.
Hope it will be helpful.
Please feel free to ask me if you have any doubts.
|