none
How to programmatically create datagridview with empty columns and rows

    Question

  • I am designing a form in VB.net where by a user is required to input the number of rows and the number of columns that he want the datagridview to have. Once the user inputs the  number of rows and the number of columns, the datagridview should be created with the number of rows and columns which are empty.

    Thanks in advance

    Wednesday, October 10, 2012 3:09 PM

Answers

  • Hi

    Here is one way. Beware of errors as they are not catered for (eg user inputs).

    ' NOTE: no error checking taken care of (and they are lurking)
    ' this code is just to show one way of answering the question.
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
            Dim dgv As New DataGridView
            Dim c As Integer = CInt(InputBox("How many columns", "Columns", "3"))
            Dim r As Integer = CInt(InputBox("How many rows", "Rows", "3"))
            For cc As Integer = 0 To c - 1
                Dim nc As New DataGridViewTextBoxColumn
                nc.Name = "Column" & cc.ToString
                dgv.Columns.Add(nc)
            Next
            dgv.Rows.Add(r - 1)
            Me.Controls.Add(dgv)
        End Sub
    End Class
    
    
    
    


    Regards Les, Livingston, Scotland

    • Marked as answer by Paul Anim Wednesday, October 10, 2012 4:21 PM
    Wednesday, October 10, 2012 3:27 PM

All replies

  • Hi Paul,

    take help from below said - 

    http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview(v=vs.90).aspx#Y0


    Pl. Mark/Proposed as Answer if found your solution Vote as Helpful if related to your topic. Always Motivate others by performing this Action.

    Wednesday, October 10, 2012 3:16 PM
  • Hi

    Here is one way. Beware of errors as they are not catered for (eg user inputs).

    ' NOTE: no error checking taken care of (and they are lurking)
    ' this code is just to show one way of answering the question.
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
            Dim dgv As New DataGridView
            Dim c As Integer = CInt(InputBox("How many columns", "Columns", "3"))
            Dim r As Integer = CInt(InputBox("How many rows", "Rows", "3"))
            For cc As Integer = 0 To c - 1
                Dim nc As New DataGridViewTextBoxColumn
                nc.Name = "Column" & cc.ToString
                dgv.Columns.Add(nc)
            Next
            dgv.Rows.Add(r - 1)
            Me.Controls.Add(dgv)
        End Sub
    End Class
    
    
    
    


    Regards Les, Livingston, Scotland

    • Marked as answer by Paul Anim Wednesday, October 10, 2012 4:21 PM
    Wednesday, October 10, 2012 3:27 PM
  • Hi Arif,

    Thanks for the link recommended. From there i manage to get a code like this for the start.

    Dim dgview As New DataGridView
    Controls.Add(dgview)
    dgview.Location = New Point(12, 12)
    dgview.Size = New Size(250, 250)
    
    dgview.ColumnCount = 5
    dgview.RowCount = 5

    Thanks

    Wednesday, October 10, 2012 4:21 PM
  • Thanks Leshay,

    This is more like what I was looking for. It works fine.

    May I ask one more question?

    How do I append rows and columns later on to the same datagridview starting from where the previous rows and column ended?

    thanks


    • Edited by Paul Anim Wednesday, October 10, 2012 5:30 PM
    Wednesday, October 10, 2012 4:24 PM