• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 77
  • Last Modified:

How to generate dynamic Rows in DataGridview by using TextBox number

I have one TextBox and one Button. I want to generate number of rows according to number entered in TextBox by clicking the Button in vb.net.
Robins Harawa
Robins Harawa
  • 2
  • 2
1 Solution
Pawan KumarDatabase ExpertCommented:
Asp.NET ?
Robins HarawaFull Stack DeveloperAuthor Commented:
No windows form application
Pawan KumarDatabase ExpertCommented:
Dim endVal As Integer --- is the number you can read from the textbox.

Dim startVal As Integer
Dim endVal As Integer
startVal = 1
endVal = 10
For var = startVal To endVal
     Dim row As String() 
Next var

Open in new window

Another way of accomplishing this is to use some LINQ, e.g. -

Form1.vb -
Public Class Form1
    Private Sub OnLeave(sender As Object, e As EventArgs) Handles TextBox1.Leave
        Dim value = 0
        DataGridView1.DataSource = (From i In Enumerable.Range(0, If(Integer.TryParse(CType(sender, TextBox).Text, value), value, 0)) Select New With {.Key = i}).ToList()
    End Sub
End Class

Open in new window

Form1.Designer.vb -
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing AndAlso components IsNot Nothing Then
            End If
        End Try
    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.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.DataGridView1 = New System.Windows.Forms.DataGridView()
        Me.TextBox1 = New System.Windows.Forms.TextBox()
        CType(Me.DataGridView1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.DataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize
        Me.DataGridView1.Location = New System.Drawing.Point(13, 13)
        Me.DataGridView1.Name = "DataGridView1"
        Me.DataGridView1.Size = New System.Drawing.Size(259, 210)
        Me.DataGridView1.TabIndex = 0
        Me.TextBox1.Location = New System.Drawing.Point(171, 229)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(100, 20)
        Me.TextBox1.TabIndex = 1
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(284, 261)
        Me.Name = "Form1"
        Me.Text = "Form1"
        CType(Me.DataGridView1, System.ComponentModel.ISupportInitialize).EndInit()

    End Sub

    Friend WithEvents DataGridView1 As DataGridView
    Friend WithEvents TextBox1 As TextBox
End Class

Open in new window

Produces the following results -
Initial load -Capture.PNGEntering a value in the textbox -Capture.PNGAfter leaving the textbox -Capture.PNG
Robins HarawaFull Stack DeveloperAuthor Commented:
thanks guys
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now