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 QuestionsAll 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))PublicSub 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 AsString = "Select * FROM ParentTable"'Dim SQL As String = "SELECT COUNT(*) FROM ParentTable"Dim output AsNew List(OfString)() Dim dbName AsString = "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() TryDim 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()EndWhileCatch e As SqlClient.SqlException ' Do some logging or something. MessageBox.Show("There was an error accessing your data. DETAIL: " & e.ToString()) EndTryEndUsingEndUsingDim str1 AsString, str2 AsStringDim L AsIntegerForEach item AsStringIn 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 AsNew DataTable dt.Columns.Add("ID") dt.Columns.Add("Name") dt.Columns(0).AutoIncrement = TrueDim R As DataRow = dt.NewRow R("Name") = str1 & " " & str2 dt.Rows.Add(R) dgvOne.DataSource = dt 'Return outputEndSubPosted 13-Jul-20 12:57pm Choroid Updated 14-Jul-20 5:49am CHill60 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 CHill60 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 Choroid 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 The content must be between 30 and 50000 characters.Treat my content as plain text, not as HTML
Preview 0
…
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:
Read the question carefully.
Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
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.
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)