Data Grid Row Count

  • Thread starter Thread starter Tejpal Garhwal
  • Start date Start date
T

Tejpal Garhwal

I have datagrid filled with some data rows. At the run time i want know how
many total rows are there in the data grid ?

Any idea ? Any Suggestions ?

Thanks in advance

Tej
 
Tejpal Garhwal said:
I have datagrid filled with some data rows. At the run time i want know how
many total rows are there in the data grid ?

I think this format will work (doesn't matter which Table you use)

Label1.Text = DataSet.TableName.Rows.Count

Bruce
 
Thanks Bruce, but your code with return the row count of Datatable not the
DataGrid. I need the row count for DataGrid.

I know you have the answer for this too :)
 
Everything you can bind the grid to, a collection, datatable, dataview, and
even dataset, will have something that's enumerable and can be counted...it
depends what you are binding to.

DataTable.Rows.Count should work as Mr. B mentioned unless you are binding
to something else. What is the grid bound to? If a DataView, then
Dataview.rows.count. If a strongly typed collection then
Collection.Items.Count.

HTH,

Bill
 
My Datagrid is bound to Datatable which i am creating and populating the
datagrid with this datatable at the run time.
 
Let me brief you what exact i want to do with this code :

I am reading registry data (presently its hard coaded) and populating it in
the datatable which is bound to datagrid. user can update the registry data
in the grid and on click of save it should update the registry... which is
not happending.. datatable seems empty..

Thanks for your help


Imports System

Imports System.Data

Imports Microsoft.Win32

Imports System.Data.SqlClient

Public Class Configuration

Inherits System.Windows.Forms.Form

Private dataTable As System.Data.DataTable

Private maxColumnNumber As Integer

Private maxRowNumber As Integer

#Region " Windows Form Designer generated code "

Public Sub New()

MyBase.New()

'This call is required by the Windows Form Designer.

InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing Then

If Not (components Is Nothing) Then

components.Dispose()

End If

End If

MyBase.Dispose(disposing)

End Sub

'Required by the Windows Form Designer

Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer

'It can be modified using the Windows Form Designer.

'Do not modify it using the code editor.

Friend WithEvents btnReadRegistry As System.Windows.Forms.Button

Friend WithEvents tvRegistry As System.Windows.Forms.TreeView

Friend WithEvents dtgRegistry As System.Windows.Forms.DataGrid

Friend WithEvents imglstConfiguration As System.Windows.Forms.ImageList

Friend WithEvents btnCancel As System.Windows.Forms.Button

Friend WithEvents btnSave As System.Windows.Forms.Button

Friend WithEvents DataSetConfiguration As System.Data.DataSet

Friend WithEvents SqlConnectionConfiguration As
System.Data.SqlClient.SqlConnection

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

Me.components = New System.ComponentModel.Container()

Dim resources As System.Resources.ResourceManager = New
System.Resources.ResourceManager(GetType(Configuration))

Me.btnReadRegistry = New System.Windows.Forms.Button()

Me.tvRegistry = New System.Windows.Forms.TreeView()

Me.imglstConfiguration = New System.Windows.Forms.ImageList(Me.components)

Me.dtgRegistry = New System.Windows.Forms.DataGrid()

Me.btnCancel = New System.Windows.Forms.Button()

Me.btnSave = New System.Windows.Forms.Button()

Me.DataSetConfiguration = New System.Data.DataSet()

Me.SqlConnectionConfiguration = New System.Data.SqlClient.SqlConnection()

CType(Me.dtgRegistry, System.ComponentModel.ISupportInitialize).BeginInit()

CType(Me.DataSetConfiguration,
System.ComponentModel.ISupportInitialize).BeginInit()

Me.SuspendLayout()

'

'btnReadRegistry

'

Me.btnReadRegistry.AccessibleDescription =
CType(resources.GetObject("btnReadRegistry.AccessibleDescription"), String)

Me.btnReadRegistry.AccessibleName =
CType(resources.GetObject("btnReadRegistry.AccessibleName"), String)

Me.btnReadRegistry.Anchor =
CType(resources.GetObject("btnReadRegistry.Anchor"),
System.Windows.Forms.AnchorStyles)

Me.btnReadRegistry.BackgroundImage =
CType(resources.GetObject("btnReadRegistry.BackgroundImage"),
System.Drawing.Image)

Me.btnReadRegistry.Dock = CType(resources.GetObject("btnReadRegistry.Dock"),
System.Windows.Forms.DockStyle)

Me.btnReadRegistry.Enabled =
CType(resources.GetObject("btnReadRegistry.Enabled"), Boolean)

Me.btnReadRegistry.FlatStyle =
CType(resources.GetObject("btnReadRegistry.FlatStyle"),
System.Windows.Forms.FlatStyle)

Me.btnReadRegistry.Font = CType(resources.GetObject("btnReadRegistry.Font"),
System.Drawing.Font)

Me.btnReadRegistry.Image =
CType(resources.GetObject("btnReadRegistry.Image"), System.Drawing.Image)

Me.btnReadRegistry.ImageAlign =
CType(resources.GetObject("btnReadRegistry.ImageAlign"),
System.Drawing.ContentAlignment)

Me.btnReadRegistry.ImageIndex =
CType(resources.GetObject("btnReadRegistry.ImageIndex"), Integer)

Me.btnReadRegistry.ImeMode =
CType(resources.GetObject("btnReadRegistry.ImeMode"),
System.Windows.Forms.ImeMode)

Me.btnReadRegistry.Location =
CType(resources.GetObject("btnReadRegistry.Location"), System.Drawing.Point)

Me.btnReadRegistry.Name = "btnReadRegistry"

Me.btnReadRegistry.RightToLeft =
CType(resources.GetObject("btnReadRegistry.RightToLeft"),
System.Windows.Forms.RightToLeft)

Me.btnReadRegistry.Size = CType(resources.GetObject("btnReadRegistry.Size"),
System.Drawing.Size)

Me.btnReadRegistry.TabIndex =
CType(resources.GetObject("btnReadRegistry.TabIndex"), Integer)

Me.btnReadRegistry.Text = resources.GetString("btnReadRegistry.Text")

Me.btnReadRegistry.TextAlign =
CType(resources.GetObject("btnReadRegistry.TextAlign"),
System.Drawing.ContentAlignment)

Me.btnReadRegistry.Visible =
CType(resources.GetObject("btnReadRegistry.Visible"), Boolean)

'

'tvRegistry

'

Me.tvRegistry.AccessibleDescription =
CType(resources.GetObject("tvRegistry.AccessibleDescription"), String)

Me.tvRegistry.AccessibleName =
CType(resources.GetObject("tvRegistry.AccessibleName"), String)

Me.tvRegistry.Anchor = CType(resources.GetObject("tvRegistry.Anchor"),
System.Windows.Forms.AnchorStyles)

Me.tvRegistry.BackColor = System.Drawing.SystemColors.Window

Me.tvRegistry.BackgroundImage =
CType(resources.GetObject("tvRegistry.BackgroundImage"),
System.Drawing.Image)

Me.tvRegistry.Dock = CType(resources.GetObject("tvRegistry.Dock"),
System.Windows.Forms.DockStyle)

Me.tvRegistry.Enabled = CType(resources.GetObject("tvRegistry.Enabled"),
Boolean)

Me.tvRegistry.Font = CType(resources.GetObject("tvRegistry.Font"),
System.Drawing.Font)

Me.tvRegistry.ImageIndex =
CType(resources.GetObject("tvRegistry.ImageIndex"), Integer)

Me.tvRegistry.ImageList = Me.imglstConfiguration

Me.tvRegistry.ImeMode = CType(resources.GetObject("tvRegistry.ImeMode"),
System.Windows.Forms.ImeMode)

Me.tvRegistry.Indent = CType(resources.GetObject("tvRegistry.Indent"),
Integer)

Me.tvRegistry.ItemHeight =
CType(resources.GetObject("tvRegistry.ItemHeight"), Integer)

Me.tvRegistry.Location = CType(resources.GetObject("tvRegistry.Location"),
System.Drawing.Point)

Me.tvRegistry.Name = "tvRegistry"

Me.tvRegistry.Nodes.AddRange(New System.Windows.Forms.TreeNode()
{CType(resources.GetObject("tvRegistry.Nodes.Nodes"),
System.Windows.Forms.TreeNode)})

Me.tvRegistry.RightToLeft =
CType(resources.GetObject("tvRegistry.RightToLeft"),
System.Windows.Forms.RightToLeft)

Me.tvRegistry.SelectedImageIndex =
CType(resources.GetObject("tvRegistry.SelectedImageIndex"), Integer)

Me.tvRegistry.Size = CType(resources.GetObject("tvRegistry.Size"),
System.Drawing.Size)

Me.tvRegistry.TabIndex = CType(resources.GetObject("tvRegistry.TabIndex"),
Integer)

Me.tvRegistry.Text = resources.GetString("tvRegistry.Text")

Me.tvRegistry.Visible = CType(resources.GetObject("tvRegistry.Visible"),
Boolean)

'

'imglstConfiguration

'

Me.imglstConfiguration.ColorDepth =
System.Windows.Forms.ColorDepth.Depth8Bit

Me.imglstConfiguration.ImageSize =
CType(resources.GetObject("imglstConfiguration.ImageSize"),
System.Drawing.Size)

Me.imglstConfiguration.ImageStream =
CType(resources.GetObject("imglstConfiguration.ImageStream"),
System.Windows.Forms.ImageListStreamer)

Me.imglstConfiguration.TransparentColor = System.Drawing.Color.Transparent

'

'dtgRegistry

'

Me.dtgRegistry.AccessibleDescription =
CType(resources.GetObject("dtgRegistry.AccessibleDescription"), String)

Me.dtgRegistry.AccessibleName =
CType(resources.GetObject("dtgRegistry.AccessibleName"), String)

Me.dtgRegistry.Anchor = CType(resources.GetObject("dtgRegistry.Anchor"),
System.Windows.Forms.AnchorStyles)

Me.dtgRegistry.BackColor = System.Drawing.SystemColors.Info

Me.dtgRegistry.BackgroundImage =
CType(resources.GetObject("dtgRegistry.BackgroundImage"),
System.Drawing.Image)

Me.dtgRegistry.CaptionFont = CType(resources.GetObject("dtgRegistry.CaptionF
ont"), System.Drawing.Font)

Me.dtgRegistry.CaptionText = resources.GetString("dtgRegistry.CaptionText")

Me.dtgRegistry.DataMember = ""

Me.dtgRegistry.Dock = CType(resources.GetObject("dtgRegistry.Dock"),
System.Windows.Forms.DockStyle)

Me.dtgRegistry.Enabled = CType(resources.GetObject("dtgRegistry.Enabled"),
Boolean)

Me.dtgRegistry.Font = CType(resources.GetObject("dtgRegistry.Font"),
System.Drawing.Font)

Me.dtgRegistry.HeaderForeColor = System.Drawing.SystemColors.ControlText

Me.dtgRegistry.ImeMode = CType(resources.GetObject("dtgRegistry.ImeMode"),
System.Windows.Forms.ImeMode)

Me.dtgRegistry.Location = CType(resources.GetObject("dtgRegistry.Location"),
System.Drawing.Point)

Me.dtgRegistry.Name = "dtgRegistry"

Me.dtgRegistry.RightToLeft =
CType(resources.GetObject("dtgRegistry.RightToLeft"),
System.Windows.Forms.RightToLeft)

Me.dtgRegistry.Size = CType(resources.GetObject("dtgRegistry.Size"),
System.Drawing.Size)

Me.dtgRegistry.TabIndex = CType(resources.GetObject("dtgRegistry.TabIndex"),
Integer)

Me.dtgRegistry.Visible = CType(resources.GetObject("dtgRegistry.Visible"),
Boolean)

'

'btnCancel

'

Me.btnCancel.AccessibleDescription =
CType(resources.GetObject("btnCancel.AccessibleDescription"), String)

Me.btnCancel.AccessibleName =
CType(resources.GetObject("btnCancel.AccessibleName"), String)

Me.btnCancel.Anchor = CType(resources.GetObject("btnCancel.Anchor"),
System.Windows.Forms.AnchorStyles)

Me.btnCancel.BackgroundImage =
CType(resources.GetObject("btnCancel.BackgroundImage"),
System.Drawing.Image)

Me.btnCancel.Dock = CType(resources.GetObject("btnCancel.Dock"),
System.Windows.Forms.DockStyle)

Me.btnCancel.Enabled = CType(resources.GetObject("btnCancel.Enabled"),
Boolean)

Me.btnCancel.FlatStyle = CType(resources.GetObject("btnCancel.FlatStyle"),
System.Windows.Forms.FlatStyle)

Me.btnCancel.Font = CType(resources.GetObject("btnCancel.Font"),
System.Drawing.Font)

Me.btnCancel.Image = CType(resources.GetObject("btnCancel.Image"),
System.Drawing.Image)

Me.btnCancel.ImageAlign = CType(resources.GetObject("btnCancel.ImageAlign"),
System.Drawing.ContentAlignment)

Me.btnCancel.ImageIndex = CType(resources.GetObject("btnCancel.ImageIndex"),
Integer)

Me.btnCancel.ImeMode = CType(resources.GetObject("btnCancel.ImeMode"),
System.Windows.Forms.ImeMode)

Me.btnCancel.Location = CType(resources.GetObject("btnCancel.Location"),
System.Drawing.Point)

Me.btnCancel.Name = "btnCancel"

Me.btnCancel.RightToLeft =
CType(resources.GetObject("btnCancel.RightToLeft"),
System.Windows.Forms.RightToLeft)

Me.btnCancel.Size = CType(resources.GetObject("btnCancel.Size"),
System.Drawing.Size)

Me.btnCancel.TabIndex = CType(resources.GetObject("btnCancel.TabIndex"),
Integer)

Me.btnCancel.Text = resources.GetString("btnCancel.Text")

Me.btnCancel.TextAlign = CType(resources.GetObject("btnCancel.TextAlign"),
System.Drawing.ContentAlignment)

Me.btnCancel.Visible = CType(resources.GetObject("btnCancel.Visible"),
Boolean)

'

'btnSave

'

Me.btnSave.AccessibleDescription =
CType(resources.GetObject("btnSave.AccessibleDescription"), String)

Me.btnSave.AccessibleName =
CType(resources.GetObject("btnSave.AccessibleName"), String)

Me.btnSave.Anchor = CType(resources.GetObject("btnSave.Anchor"),
System.Windows.Forms.AnchorStyles)

Me.btnSave.BackgroundImage =
CType(resources.GetObject("btnSave.BackgroundImage"), System.Drawing.Image)

Me.btnSave.Dock = CType(resources.GetObject("btnSave.Dock"),
System.Windows.Forms.DockStyle)

Me.btnSave.Enabled = CType(resources.GetObject("btnSave.Enabled"), Boolean)

Me.btnSave.FlatStyle = CType(resources.GetObject("btnSave.FlatStyle"),
System.Windows.Forms.FlatStyle)

Me.btnSave.Font = CType(resources.GetObject("btnSave.Font"),
System.Drawing.Font)

Me.btnSave.Image = CType(resources.GetObject("btnSave.Image"),
System.Drawing.Image)

Me.btnSave.ImageAlign = CType(resources.GetObject("btnSave.ImageAlign"),
System.Drawing.ContentAlignment)

Me.btnSave.ImageIndex = CType(resources.GetObject("btnSave.ImageIndex"),
Integer)

Me.btnSave.ImeMode = CType(resources.GetObject("btnSave.ImeMode"),
System.Windows.Forms.ImeMode)

Me.btnSave.Location = CType(resources.GetObject("btnSave.Location"),
System.Drawing.Point)

Me.btnSave.Name = "btnSave"

Me.btnSave.RightToLeft = CType(resources.GetObject("btnSave.RightToLeft"),
System.Windows.Forms.RightToLeft)

Me.btnSave.Size = CType(resources.GetObject("btnSave.Size"),
System.Drawing.Size)

Me.btnSave.TabIndex = CType(resources.GetObject("btnSave.TabIndex"),
Integer)

Me.btnSave.Text = resources.GetString("btnSave.Text")

Me.btnSave.TextAlign = CType(resources.GetObject("btnSave.TextAlign"),
System.Drawing.ContentAlignment)

Me.btnSave.Visible = CType(resources.GetObject("btnSave.Visible"), Boolean)

'

'DataSetConfiguration

'

Me.DataSetConfiguration.DataSetName = "ConfigurationDataSet"

Me.DataSetConfiguration.Locale = New
System.Globalization.CultureInfo("en-US")

'

'Configuration

'

Me.AccessibleDescription =
CType(resources.GetObject("$this.AccessibleDescription"), String)

Me.AccessibleName = CType(resources.GetObject("$this.AccessibleName"),
String)

Me.Anchor = CType(resources.GetObject("$this.Anchor"),
System.Windows.Forms.AnchorStyles)

Me.AutoScaleBaseSize = CType(resources.GetObject("$this.AutoScaleBaseSize"),
System.Drawing.Size)

Me.AutoScroll = CType(resources.GetObject("$this.AutoScroll"), Boolean)

Me.AutoScrollMargin = CType(resources.GetObject("$this.AutoScrollMargin"),
System.Drawing.Size)

Me.AutoScrollMinSize = CType(resources.GetObject("$this.AutoScrollMinSize"),
System.Drawing.Size)

Me.BackgroundImage = CType(resources.GetObject("$this.BackgroundImage"),
System.Drawing.Image)

Me.ClientSize = CType(resources.GetObject("$this.ClientSize"),
System.Drawing.Size)

Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnSave,
Me.btnCancel, Me.dtgRegistry, Me.tvRegistry, Me.btnReadRegistry})

Me.Dock = CType(resources.GetObject("$this.Dock"),
System.Windows.Forms.DockStyle)

Me.Enabled = CType(resources.GetObject("$this.Enabled"), Boolean)

Me.Font = CType(resources.GetObject("$this.Font"), System.Drawing.Font)

Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)

Me.ImeMode = CType(resources.GetObject("$this.ImeMode"),
System.Windows.Forms.ImeMode)

Me.Location = CType(resources.GetObject("$this.Location"),
System.Drawing.Point)

Me.MaximumSize = CType(resources.GetObject("$this.MaximumSize"),
System.Drawing.Size)

Me.MinimumSize = CType(resources.GetObject("$this.MinimumSize"),
System.Drawing.Size)

Me.Name = "Configuration"

Me.RightToLeft = CType(resources.GetObject("$this.RightToLeft"),
System.Windows.Forms.RightToLeft)

Me.StartPosition = CType(resources.GetObject("$this.StartPosition"),
System.Windows.Forms.FormStartPosition)

Me.Text = resources.GetString("$this.Text")

Me.Visible = CType(resources.GetObject("$this.Visible"), Boolean)

CType(Me.dtgRegistry, System.ComponentModel.ISupportInitialize).EndInit()

CType(Me.DataSetConfiguration,
System.ComponentModel.ISupportInitialize).EndInit()

Me.ResumeLayout(False)

End Sub

#End Region

Private cnnConfiguration As SqlConnection

Private daConfiguration As SqlDataAdapter

Private dsConfiguration As DataSet

Private drConfiguration As SqlDataReader

Private Sub Configuration_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

'Expand the tree view

tvRegistry.ExpandAll()

End Sub

Private Sub InitializeTable()

Dim index As Integer

Dim objRegKey As RegistryKey

Dim sKeyValue As String

objRegKey = Registry.LocalMachine.OpenSubKey("Software\database\emailinfo",
True)

sKeyValue = objRegKey.GetValue("FromName", "AAA")

' Create a DataTable named "Registry Data".

dataTable = New DataTable("RegistryData")

' Create columns in the DataTable.

addColumns()

AddNewRow("Company", "REG_SZ", "OCS")

AddNewRow("EmailID", "REG_SZ", "(e-mail address removed)")

AddNewRow("ErrorEmailID", "REG_SZ", "(e-mail address removed)")

AddNewRow("FromName", "REG_SZ", sKeyValue)

AddNewRow("RecipientEmail", "REG_SZ", "(e-mail address removed)")

AddNewRow("ResendInterval", "REG_DWORD", "3e80")

AddNewRow("ResendLimit", "REG_DWORD", "2")

AddNewRow("SenderEmail", "REG_SZ", "(e-mail address removed)")

AddNewRow("WebMaster", "REG_SZ", "(e-mail address removed)")

End Sub

Private Sub addColumns()

dataTable.Columns.Add("Name", Type.GetType("System.String"))

dataTable.Columns.Add("Type", Type.GetType("System.String"))

dataTable.Columns.Add("Data", Type.GetType("System.String"))

End Sub

Private Sub AddNewRow(ByVal StringName As String, ByVal StringType As
String, ByVal StringData As String)

Dim dataRow As DataRow

dataRow = dataTable.NewRow()

dataRow(0) = StringName

dataRow(1) = StringType

dataRow(2) = StringData

dataTable.Rows.Add(dataRow)

End Sub

Private Sub tvRegistry_AfterSelect(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.TreeViewEventArgs) Handles tvRegistry.AfterSelect

Dim sSelectedNode As String = tvRegistry.SelectedNode.Text

dtgRegistry.CaptionText = tvRegistry.SelectedNode.FullPath

If StrComp(UCase(sSelectedNode), "EMAILINFO", CompareMethod.Text) = 0 Then

' Create a DataTable of data to use for Registry Data.

InitializeTable()

' Bind the DataGrid to the DataTable.

dtgRegistry.DataSource = dataTable

Else

If Not IsNothing(dataTable) Then

dataTable.Clear()

End If

End If

End Sub

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSave.Click

Dim objRegKey As RegistryKey

Dim sKeyValue As String

Dim sSelectedNode As String

sSelectedNode = tvRegistry.SelectedNode.FullPath

sSelectedNode = Mid(sSelectedNode, Len("HKEY_LOCAL_MACHINE\") + 1)

MessageBox.Show(sSelectedNode)

objRegKey = Registry.LocalMachine.OpenSubKey(sSelectedNode, True)

'read data from the data grid and write it to the registry

MessageBox.Show(dsConfiguration.Tables(0).Columns.Count)

'sKeyValue = objRegKey.GetValue("FromName", "HP")

End Sub

End Class
 
Back
Top