|
-
Sep 24th, 2002, 12:16 PM
#1
Thread Starter
Fanatic Member
Adding a row to a datagrid
I added the following 2 lines to public class of a form:
Dim dsPick As New DataSet()
Dim dtPick As New DataTable("Pick")
I then added a grid to a form and in the form load() event added the following code:
dsPick.Tables.Add(dtPick)dtPick.Columns.Add(New DataColumn("Code",
GetType(String)))
dtPick.Columns.Add(New DataColumn("Description", GetType(String)))
dtPick.Columns.Add(New DataColumn("Quantity", GetType(String)))
Me.datagrid1.DataSource = dtPick
When the user clicks a button on the form the following code is executed.
The code is supposed to create a record but when I click the button nothing
happens.
Public Sub AddToTable()
Dim row As DataRow
row = dtPick.NewRow()
row(0) = "TEST STRING 1"
row(1) = "TEST STRING 2"
row(2) = "TEST STRING 3"
dtPick.Rows.Add(row)
End Sub
I am a bit inexperienced in this .Net business but I like to think that I am slowly getting there but I have to admit I am finding all these Datatables, Datasets e.t.c a bit hard.
Thanks in Advance
-
Sep 24th, 2002, 02:27 PM
#2
Member
Hi
I CHANGED
dsPick.Tables.Add(dtPick)dtPick.Columns.Add(New DataColumn("Code",
GetType(String)))
TO
dsPick.Tables.Add(dtPick)
dtPick.Columns.Add(New DataColumn("Code",
GetType(String)))
I tried your code with this change and it worked perfectly.
Harold Hoffman
-
Sep 25th, 2002, 08:18 AM
#3
Thread Starter
Fanatic Member
Cheers Harold
Thanks Harold, much appreciated.
I made a big mistake and have sorted it out now.
A further question I wouldnt mind asking is how to set the column widths in my datagrid in code.
Thanks again Harold. Your a genius.
-
Sep 25th, 2002, 08:47 AM
#4
Fanatic Member
Lots of examples of manipulating datagrids from code at:
ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfSystemWindowsFormsDataGridClassTopic.htm
-
Sep 25th, 2002, 09:57 AM
#5
Member
Basically, you must create ColumnStyles and add it to a TableStyle. Then, add the TableStyle to your datagrid.
Here is your code with some added code to set the column width.
Public Class Form1
Inherits System.Windows.Forms.Form
Dim dsPick As New DataSet()
Dim dtPick As New DataTable("Pick")
Dim dgts As New DataGridTableStyle()
Dim dgcs1 As New DataGridTextBoxColumn()
Dim dgcs2 As New DataGridTextBoxColumn()
Dim dgcs3 As New DataGridTextBoxColumn()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
dsPick.Tables.Add(dtPick)
dtPick.Columns.Add(New DataColumn("Code", GetType(String)))
dtPick.Columns.Add(New DataColumn("Description", GetType(String)))
dtPick.Columns.Add(New DataColumn("Quantity", GetType(String)))
Me.DataGrid1.DataSource = dtPick
dgcs1.MappingName = "Code"
dgcs1.Width = 75
dgcs2.MappingName = "Description"
dgcs2.Width = 125
dgcs3.MappingName = "Quantity"
dgcs3.Width = 75
dgts.GridColumnStyles.Add(dgcs1)
dgts.GridColumnStyles.Add(dgcs2)
dgts.GridColumnStyles.Add(dgcs3)
dgts.MappingName = "Pick"
Me.DataGrid1.TableStyles.Add(dgts)
Me.DataGrid1.Refresh()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim row As DataRow
row = dtPick.NewRow()
row(0) = "TEST STRING 1"
row(1) = "TEST STRING 2"
row(2) = "TEST STRING 3"
dtPick.Rows.Add(row)
End Sub
End Class
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
|