C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » DataGridView »

DataGridView with Row Numbers


Posted Date: 05 Jun 2009    Resource Type: Code Snippets    Category: DataGridView
Author: Viji RAJKUMARMember Level: Diamond    
Rating: 1 out of 5Points: 10



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



Responses

Author: DotNetMatrix    08 Jun 2009Member 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 2009Member 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.




Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
DataGridView with Row Numbers  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: How to get the Values of Slected Cell Row DataGridview(Windows Appliction) using C#?
Previous Resource: selected row content of a datagridview
Return to Discussion Resource Index
Post New Resource
Category: DataGridView


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use