[Solved] Datagridview Add Empty Rows ? - CodeProject

Click here to Skip to main content 15,918,007 members Sign in Email Password Forgot your password? Sign in with
  • home
  • articles
    • Browse Topics>
    • Latest Articles
    • Top Articles
    • Posting/Update Guidelines
    • Article Help Forum
    • Submit an article or tip
    • Import GitHub Project
    • Import your Blog
  • quick answersQ&A
    • Ask a Question
    • View Unanswered Questions
    • View All Questions
    • View C# questions
    • View C++ questions
    • View Javascript questions
    • View Visual Basic questions
    • View Python questions
  • discussionsforums
    • CodeProject.AI Server
    • All Message Boards...
    • Application Lifecycle>
      • Running a Business
      • Sales / Marketing
      • Collaboration / Beta Testing
      • Work Issues
    • Design and Architecture
    • Artificial Intelligence
    • ASP.NET
    • JavaScript
    • Internet of Things
    • C / C++ / MFC>
      • ATL / WTL / STL
      • Managed C++/CLI
    • C#
    • Free Tools
    • Objective-C and Swift
    • Database
    • Hardware & Devices>
      • System Admin
    • Hosting and Servers
    • Java
    • Linux Programming
    • Python
    • .NET (Core and Framework)
    • Android
    • iOS
    • Mobile
    • WPF
    • Visual Basic
    • Web Development
    • Site Bugs / Suggestions
    • Spam and Abuse Watch
  • featuresfeatures
    • Competitions
    • News
    • The Insider Newsletter
    • The Daily Build Newsletter
    • Newsletter archive
    • Surveys
    • CodeProject Stuff
  • communitylounge
    • Who's Who
    • Most Valuable Professionals
    • The Lounge  
    • The CodeProject Blog
    • Where I Am: Member Photos
    • The Insider News
    • The Weird & The Wonderful
  • help?
    • What is 'CodeProject'?
    • General FAQ
    • Ask a Question
    • Bugs and Suggestions
    • Article Help Forum
    • About Us
Search within: Articles Quick Answers Messages Use my saved content filters Ask a Question All Questions All Unanswered FAQ Vb.net datagridview add empty rows ? Please Sign up or sign in to vote. 1.00/5 (1 vote) See more: VBDataGridView I might call this more of a rant than a question To Skip the RANT go to OK lets get to the questions VS 2019 the DataGridView (DGV) is like that old saying "You Can Have Your Cake and Eat To" EXCEPT with the DGV you only get the cake NO eating it After a lot of searching and trial and error playing with DGV it is so wonderful to just write dgvOne.DataSource = dt and you have data displayed My issue is when you use this technique NO manually adding to the DGV>br/> Why the OCD rant unless you fill it with data the DGV has this ugly gray space below my once a month entry really NOT a nice looing user EX OK lets get to the questions I know how to manually add the column headers with code and properties I am assuming I can SELECT FROM the TABLE put these values in a StringArray I seldom embrace Arrays I always thought of them as DB that disappear So how do I loop through the String Array and add the values as Rows to the DGB AND have the DGV pre populated with 12 empty rows? Code Below is what I have tried What I have tried: VB 'Private Sub aA(getListOfMotonum() As List(Of String)) Public Sub aA() 'Code Below makes empty ROWS in DGV ''dgvOne.Columns.AddRange(Enumerable.Range(0, 2).Select(Function(x) New DataGridViewTextBoxColumn).ToArray) ''dgvOne.Rows.AddRange(Enumerable.Range(0, 10).Select(Function(x) New DataGridViewRow).ToArray) Dim SQL As String = "Select * FROM ParentTable" 'Dim SQL As String = "SELECT COUNT(*) FROM ParentTable" Dim output As New List(Of String)() Dim dbName As String = "Word.db" 'Dim cmd As New SQLiteCommand ' Set the connection string in the Solutions Explorer/Properties/Settings object (double-click) Using conn = New SQLiteConnection("Data Source =" & dbName & ";Version=3;") Using cmd = New SQLiteCommand(SQL, conn) conn.Open() Try Dim dr = cmd.ExecuteReader() While dr.Read() output.Add(dr("NAME").ToString()) 'Dim output As New List(Of String)() rtbEnter.Text = dr("NAME").ToString() 'rtbEnter.Text = output.ToArray() End While Catch e As SqlClient.SqlException ' Do some logging or something. MessageBox.Show("There was an error accessing your data. DETAIL: " & e.ToString()) End Try End Using End Using Dim str1 As String, str2 As String Dim L As Integer For Each item As String In output lbOne.Items.Add(item) L = Len(item) str1 = CStr(L) str2 = output(0) Next 'dgvOne.ColumnCount = 3 'dgvOne.Columns(0).Name = "PID" 'dgvOne.Columns(1).Name = "Entry Data" Dim dt As New DataTable dt.Columns.Add("ID") dt.Columns.Add("Name") dt.Columns(0).AutoIncrement = True Dim R As DataRow = dt.NewRow R("Name") = str1 & " " & str2 dt.Rows.Add(R) dgvOne.DataSource = dt 'Return output End Sub Posted 13-Jul-20 12:57pm Updated 14-Jul-20 5:49am v2 Add a Solution Comments Richard MacCutchan 14-Jul-20 4:07am I am not sure I understand what you are complaining about, but filling DataGridView controls manually or via a binding source works fine. But trying to do both makes no sense since one will overwite the other.

2 solutions

  • Top Rated
  • Most Recent
Please Sign up or sign in to vote.

Solution 1

Accept Solution Reject Solution In your example code you appear to be trying to do both methods. Pick one. See the comment from @Richard-MacCutchan. If you go down the datatable route VB dgvOne.DataSource = dtthen add 12 blank rows to the datatable before assigning it to the DataSource. If you go down the route of manually adding the rows to the datagridview then just add 12 more blank rows in a separate loop after adding your data. One or the other. Not both. Other comments - if you are going to the trouble of stepping through each record in your result set then do all the work within that loop. Not sure why you have a another for-each loop. - In that for-each loop For Each item As String In outputyou are continuously overwriting L, str1 and str2. I suspect that you wanted the VB R("Name") = str1 & " " & str2 dt.Rows.Add(R) actually inside that loop - No need for arrays anywhere in this. Nor have you actually used one. Finally - a bit of advice for getting quicker and better answers ... get rid of commented out lines in the code you present to us here. Especially if you are not using the correct lang="VB" in your post. It just clutters up your question, making it more difficult for people to read. Many members will just move on to the next question, which doesn't help you very much. Permalink Share this answer Posted 14-Jul-20 2:39am Comments Choroid 14-Jul-20 11:48am @Richard-MacCutchan Thanks I agree less commented out code would be ideal Guess I have become complaint shy from StackOverflow use where You did not show us enough of what you tried. I will post my completed code below for others that might want to see the finished product My frustration with this issue is I did this in JavaFX in about 10 min as a novas Please Sign up or sign in to vote.

Solution 2

Accept Solution Reject Solution Here is the completed code from the Solution Above Private Sub PopulateDGV() Dim str2 As String Dim s1 As Integer Dim dbName As String = "Word.db" Dim conn As New SQLiteConnection("Data Source =" & dbName & ";Version=3;") Dim valuesList As ArrayList = New ArrayList() conn.Open() 'Read from the database Dim cmd As SQLiteCommand = New SQLiteCommand("Select * FROM ParentTable", conn) Dim rdr As SQLite.SQLiteDataReader = cmd.ExecuteReader 'Set Design of the DataGridView dgvOne.DefaultCellStyle.Font = New Font("Tahoma", 10) dgvOne.ColumnCount = 2 dgvOne.Columns(0).Width = 60 dgvOne.Columns(1).Width = 420 'DGV Header Names dgvOne.Columns(0).Name = "PID" dgvOne.Columns(1).Name = "Entry Data" 'Read from DB Table add to DGV row While rdr.Read() valuesList.Add(rdr(1)).ToString() lbOne.Items.Add(rdr(1)).ToString() s1 = rdr(0).ToString str2 = rdr(1) dgvOne.Rows.Add(s1, str2) End While 'Add Blank rows to DGV For iA = 1 To 4 dgvOne.Rows.Add(" ") Next conn.Close() End Sub Permalink Share this answer Posted 14-Jul-20 5:49am Add a Solution

Add your solution here

B I U S small BIG code Plain TextASMASPASP.NETBASICBATC#C++COBOLCoffeeScriptCSSDartdbaseF#FORTRANHTMLJavaJavascriptKotlinLuaMIDLMSILObjectiveCPascalPERLPHPPowerShellPythonRazorRubyScalaShellSLNSQLSwiftT4TerminalTypeScriptVBVBScriptXMLYAMLvar < > & link [^] encode untab case indent outdent OKPaste as
Strip HTML
Encode HTML
Paste as-is
Code block
Quoted Text
Best guess
To display as Treat my content as plain text, not as HTML

Preview

Existing Members

Sign in to your account

...or Join us

Download, Vote, Comment, Publish.
Your Email
Password
Forgot your password?
Your Email
This email is in use. Do you need your password?
Optional Password
I have read and agree to the Terms of Service and Privacy Policy Please subscribe me to the CodeProject newsletters Submit your solution When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Print Answers RSS
Top Experts
Last 24hrsThis month
Rick York 45
CPallini 45
Graeme_Grant 35
Gbenbam 25
merano99 25
Pete O'Hanlon 700
OriginalGriff 483
Dave Kreskowiak 405
Richard MacCutchan 310
merano99 260
Related Questions Add New Empty row to gridview control databound datagridview add empty row deleting empty row of datagridview Add empty columns and empty rows to a report while printing a dgv Exportar excel desde el datagridview en VB.NET Fill DataGridview with empty rows Vb.net - how do I add a row in a datagridview to an existing object? Bound datagridview keeps trying to add empty row How can I add rows to a datagridview while the datagridview is bounded to an empty datatable How to add rows in datagridview? Advertise Privacy Cookies Terms of Use Last Updated 14 Jul 2020 Layout: fixed | fluid Copyright © CodeProject, 1999-2024 All Rights Reserved. Web04 2.8:2024-06-13:1 CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900

Từ khóa » Visual Basic Datagridview Insert Row