-
Nov 9th, 2020, 04:07 PM
#1
Thread Starter
Hyperactive Member
Add New Entry To DataGridView
I am trying to add entries to my datagridview control but not sure how to make this code add one after another instead it is only adding the first entry which I understand this though as it makes perfect sense due to the fact of me specifying which Row I was adding in my code below, so how can I make it so that it can just incrementally add one Row after another but the same Cell Values?
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form1.DataGridView1.Rows(0).Cells(0).Value = TextBox1.Text
Form1.DataGridView1.Rows(0).Cells(1).Value = TextBox2.Text
Form1.DataGridView1.Rows(0).Cells(2).Value = TextBox3.Text
Form1.DataGridView1.Rows(0).Cells(3).Value = TextBox4.Text
Form1.DataGridView1.Rows(0).Cells(4).Value = TextBox5.Text
Form1.DataGridView1.Rows(0).Cells(5).Value = TextBox6.Text
Form1.DataGridView1.Rows(0).Cells(6).Value = TextBox7.Text
Form1.DataGridView1.Rows(0).Cells(7).Value = TextBox8.Text
'Clear All Textboxes After Add
TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
TextBox4.Clear()
TextBox5.Clear()
TextBox6.Clear()
TextBox6.Clear()
TextBox7.Clear()
TextBox8.Clear()
End Sub
-
Nov 9th, 2020, 04:13 PM
#2
Re: Add New Entry To DataGridView
Code:
DataGridView1.Rows.Add(TextBox1.Text, _
TextBox2.Text, _
TextBox3.Text, _
TextBox4.Text, _
TextBox5.Text, _
TextBox6.Text, _
TextBox7.Text, _
TextBox8.Text)
'Clear All Textboxes After Add
TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
TextBox4.Clear()
TextBox5.Clear()
TextBox6.Clear()
TextBox6.Clear() ???
TextBox7.Clear()
TextBox8.Clear()
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 9th, 2020, 04:30 PM
#3
Thread Starter
Hyperactive Member
Re: Add New Entry To DataGridView
I should have explained it better I am adding this to datagridview from another form, form2.. with textboxes 1 - 8 to add text to be added to the datagridview control..
it's just a form for me to type the data to be added to the datagridview control.
-
Nov 9th, 2020, 04:43 PM
#4
Re: Add New Entry To DataGridView
Ok... This is the pro way to use dialogs. For your dialog...
Code:
Public Class AddDialog
Public ReadOnly Property itemArray() As Object()
Get
Return New Object() {TextBox1.Text, _
TextBox2.Text, _
TextBox3.Text, _
TextBox4.Text, _
TextBox5.Text, _
TextBox6.Text, _
TextBox7.Text, _
TextBox8.Text}
End Get
End Property
Private Sub AddDialog_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
btnAdd.DialogResult = Windows.Forms.DialogResult.OK
btnCancel.DialogResult = Windows.Forms.DialogResult.Cancel
End Sub
End Class
And the dgv form...
Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
dim frm as new AddDialog
If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
DataGridView1.Rows.Add(frm.itemArray)
End If
End Sub
End Class
Edit... Updated...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 9th, 2020, 05:00 PM
#5
Thread Starter
Hyperactive Member
Re: Add New Entry To DataGridView
ok I am not sure I understand what you are saying exactly.
What is meant by dialogs in this context and why do I need to use them like this?
Can't I use an array in my example the way I was doing it?
How can I do it that way?
I would like to learn what you are trying to do and why if you can help me learn why to do it like that.
I realize to use an array makes sense but not sure of everything else.
I am still learning VB so I am sorry to seem ignorant.
i need to increase the datagridview rows automatically as it continues to add each entry via the Add Form.
-
Nov 9th, 2020, 05:11 PM
#6
Thread Starter
Hyperactive Member
Re: Add New Entry To DataGridView
I tried to use "add" method like you used but not in an array, like I had it, but it added the entries downward and not across like I needed.
I need them to go across instead like this - First Name - Last Name - Email - Phone - Etc etc..
it is not adding them correctly.
-
Nov 9th, 2020, 06:11 PM
#7
Re: Add New Entry To DataGridView
Add accepts a ParamArray, which is this...
Code:
DataGridView1.Rows.Add(TextBox1.Text, _
TextBox2.Text, _
TextBox3.Text, _
TextBox4.Text, _
TextBox5.Text, _
TextBox6.Text, _
TextBox7.Text, _
TextBox8.Text)
An array would be...
Code:
DataGridView1.Rows.Add(New Object() {TextBox1.Text, _
TextBox2.Text, _
TextBox3.Text, _
TextBox4.Text, _
TextBox5.Text, _
TextBox6.Text, _
TextBox7.Text, _
TextBox8.Text})
But you don't need that, as it will take a ParamArray.
If you're going to use a Dialog for adding values, you might as well learn how to use Dialogs correctly.
Here's an example in C# https://www.codeproject.com/Articles...r-applications
If you need help translating code to VB, try this... https://www.tangiblesoftwaresolution...converter.html
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 9th, 2020, 06:41 PM
#8
Thread Starter
Hyperactive Member
Re: Add New Entry To DataGridView
ok your Awesome, Thanks Paul, I tried ParamArray via the DataGridView Add Method and it works just as needed.
Thanks, this is great..
-
Nov 9th, 2020, 06:42 PM
#9
Re: Add New Entry To DataGridView
The way I showed you in post #4 is the correct way to use a dialog. That's how modal dialogs work. There's no need for Form1.DataGridView1.Rows.Add in your Form2 code. Simply use the code I posted and it'll add to your Form1.dgv
BTW...
DataGridView1.Rows.Add(TextBox1.Text)
is equivalent to...
DataGridView1.Rows.Add(New Object() {TextBox1.Text})
As you can see, calling Rows.Add adds a new row. If you Just pass in TextBox1.Text, it adds a new row with one column filled.
If you do this...
DataGridView1.Rows.Add(TextBox1.Text, TextBox2.Text)
Passing in 2 parameters (TextBox1.Text and TextBox2.Text), it'll add a new row with two columns filled.
I hope that helps...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 9th, 2020, 06:52 PM
#10
Re: Add New Entry To DataGridView
Originally Posted by DreamWarrior77
i need to increase the datagridview rows automatically as it continues to add each entry via the Add Form.
You want to keep your add form open and add multiple rows. before closing the dialog? I could rewrite my post #4 example for that scenario...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 9th, 2020, 07:18 PM
#11
Thread Starter
Hyperactive Member
Re: Add New Entry To DataGridView
Thanks ok, I am confused by a lot with vb unfortunately it has been difficult for me to understand certain things which is why I have been trying to keep it simple whenever possible.
I would like to learn but I have been getting off meds which has screwed with my memory and it has been difficult concentrating/learning things recently..
I do appreciate your help though Paul, Thanks
Not sure what you mean about rewriting post #4 but my add form does stay open by itself, it just adds those textboxes of text and then clears them so I can just re-type new text in them again and add them to the DGV with the button again.. it seems to be working fine.
Thanks Though
-
Nov 9th, 2020, 07:41 PM
#12
Re: Add New Entry To DataGridView
That’s not good OOP, adding data cross-forms that way. Ideally, you’d create a collection of arrays, which, as in my post #4 example, you’d add to the dgv when your Form2 is closed. If you want to continue adding the rows one by as you are, a better way is to pass a reference to the dgv into Form2 in the constructor. That way will work as well and in some cases better than your current method. Imagine Form1 was closed after opening Form2. Calling Form1.DataGridView1 would cause a runtime error, as it wouldn’t exist.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 11th, 2020, 02:41 PM
#13
Thread Starter
Hyperactive Member
Re: Add New Entry To DataGridView
Yes maybe but Form 1 will never be closed since it is the main form when closed it ends the program.
you may be right but I don't think it seems like a problem.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|