Easy Question on Data Grid

ithunkvb

Member
Joined
Feb 17, 2006
Messages
6
Programming Experience
10+
Basically i have created a datagrid which will populate when i click on the get data button. That is fine but i need to know how changes in the data grid will reflect changes in the text box and that when i edit these text boxes the changes will be updated. editing directly on the datagrid work fine but i want the data to be editable from text box. i know its data binding and somewhere along that line.

Here is the code
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents SqlConnection1 As System.Data.SqlClient.SqlConnection
Friend WithEvents SqlDataAdapter1 As System.Data.SqlClient.SqlDataAdapter
Friend WithEvents SqlSelectCommand1 As System.Data.SqlClient.SqlCommand
Friend WithEvents SqlInsertCommand1 As System.Data.SqlClient.SqlCommand
Friend WithEvents SqlUpdateCommand1 As System.Data.SqlClient.SqlCommand
Friend WithEvents SqlDeleteCommand1 As System.Data.SqlClient.SqlCommand
Friend WithEvents DataSet11 As WindowsApplication1.DataSet1
Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents ListBox2 As System.Windows.Forms.ListBox
Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
Friend WithEvents Label4 As System.Windows.Forms.Label
Friend WithEvents TextBox3 As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.SqlConnection1 = New System.Data.SqlClient.SqlConnection
Me.SqlDataAdapter1 = New System.Data.SqlClient.SqlDataAdapter
Me.SqlDeleteCommand1 = New System.Data.SqlClient.SqlCommand
Me.SqlInsertCommand1 = New System.Data.SqlClient.SqlCommand
Me.SqlSelectCommand1 = New System.Data.SqlClient.SqlCommand
Me.SqlUpdateCommand1 = New System.Data.SqlClient.SqlCommand
Me.DataSet11 = New WindowsApplication1.DataSet1
Me.DataGrid1 = New System.Windows.Forms.DataGrid
Me.Button1 = New System.Windows.Forms.Button
Me.ListBox2 = New System.Windows.Forms.ListBox
Me.ComboBox1 = New System.Windows.Forms.ComboBox
Me.Label1 = New System.Windows.Forms.Label
Me.Button2 = New System.Windows.Forms.Button
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.Label2 = New System.Windows.Forms.Label
Me.Label3 = New System.Windows.Forms.Label
Me.TextBox2 = New System.Windows.Forms.TextBox
Me.Label4 = New System.Windows.Forms.Label
Me.TextBox3 = New System.Windows.Forms.TextBox
CType(Me.DataSet11, System.ComponentModel.ISupportInitialize).BeginInit()
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'SqlConnection1
'
Me.SqlConnection1.ConnectionString = "workstation id=RXAAT234;packet size=4096;integrated security=SSPI;data source=RXA" & _
"AT234;persist security info=False;initial catalog=Incident Reporting SystemSQL"
'
'SqlDataAdapter1
'
Me.SqlDataAdapter1.DeleteCommand = Me.SqlDeleteCommand1
Me.SqlDataAdapter1.InsertCommand = Me.SqlInsertCommand1
Me.SqlDataAdapter1.SelectCommand = Me.SqlSelectCommand1
Me.SqlDataAdapter1.TableMappings.AddRange(New System.Data.Common.DataTableMapping() {New System.Data.Common.DataTableMapping("Table", "Incident Table", New System.Data.Common.DataColumnMapping() {New System.Data.Common.DataColumnMapping("ReportID", "ReportID"), New System.Data.Common.DataColumnMapping("Report level", "Report level"), New System.Data.Common.DataColumnMapping("Report Type", "Report Type"), New System.Data.Common.DataColumnMapping("Time", "Time"), New System.Data.Common.DataColumnMapping("Date", "Date"), New System.Data.Common.DataColumnMapping("Location", "Location")})})
Me.SqlDataAdapter1.UpdateCommand = Me.SqlUpdateCommand1
'
'SqlDeleteCommand1
'
Me.SqlDeleteCommand1.CommandText = "DELETE FROM [Incident Table] WHERE (ReportID = @Original_ReportID) AND (Date = @O" & _
"riginal_Date) AND (Location = @Original_Location) AND ([Report Type] = @Original" & _
"_Report_Type) AND ([Report level] = @Original_Report_level) AND (Time = @Origina" & _
"l_Time)"
Me.SqlDeleteCommand1.Connection = Me.SqlConnection1
Me.SqlDeleteCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_ReportID", System.Data.SqlDbType.NVarChar, 50, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "ReportID", System.Data.DataRowVersion.Original, Nothing))
Me.SqlDeleteCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_Date", System.Data.SqlDbType.NVarChar, 50, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "Date", System.Data.DataRowVersion.Original, Nothing))
Me.SqlDeleteCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_Location", System.Data.SqlDbType.NVarChar, 50, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "Location", System.Data.DataRowVersion.Original, Nothing))
Me.SqlDeleteCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_Report_Type", System.Data.SqlDbType.NVarChar, 50, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "Report Type", System.Data.DataRowVersion.Original, Nothing))
Me.SqlDeleteCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_Report_level", System.Data.SqlDbType.NVarChar, 50, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "Report level", System.Data.DataRowVersion.Original, Nothing))
Me.SqlDeleteCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_Time", System.Data.SqlDbType.NVarChar, 50, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "Time", System.Data.DataRowVersion.Original, Nothing))
'
'SqlInsertCommand1
'
Me.SqlInsertCommand1.CommandText = "INSERT INTO [Incident Table] (ReportID, [Report level], [Report Type], Time, Date" & _
", Location) VALUES (@ReportID, @Param1, @Param2, @Time, @Date, @Location); SELEC" & _
"T ReportID, [Report level], [Report Type], Time, Date, Location FROM [Incident T" & _
"able] WHERE (ReportID = @ReportID)"
Me.SqlInsertCommand1.Connection = Me.SqlConnection1
Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@ReportID", System.Data.SqlDbType.NVarChar, 50, "ReportID"))
Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Param1", System.Data.SqlDbType.NVarChar, 50, "Report level"))
Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Param2", System.Data.SqlDbType.NVarChar, 50, "Report Type"))
Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Time", System.Data.SqlDbType.NVarChar, 50, "Time"))
Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Date", System.Data.SqlDbType.NVarChar, 50, "Date"))
Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Location", System.Data.SqlDbType.NVarChar, 50, "Location"))
'
'SqlSelectCommand1
'
Me.SqlSelectCommand1.CommandText = "SELECT ReportID, [Report level], [Report Type], Time, Date, Location FROM [Incide" & _
"nt Table]"
Me.SqlSelectCommand1.Connection = Me.SqlConnection1
'
'SqlUpdateCommand1
'
Me.SqlUpdateCommand1.CommandText = "UPDATE [Incident Table] SET ReportID = @ReportID, [Report level] = @Param3, [Repo" & _
"rt Type] = @Param4, Time = @Time, Date = @Date, Location = @Location WHERE (Repo" & _
"rtID = @Original_ReportID) AND (Date = @Original_Date) AND (Location = @Original" & _
"_Location) AND ([Report Type] = @Original_Report_Type) AND ([Report level] = @Or" & _
"iginal_Report_level) AND (Time = @Original_Time); SELECT ReportID, [Report level" & _
"], [Report Type], Time, Date, Location FROM [Incident Table] WHERE (ReportID = @" & _
"ReportID)"
Me.SqlUpdateCommand1.Connection = Me.SqlConnection1
Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@ReportID", System.Data.SqlDbType.NVarChar, 50, "ReportID"))
Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Param3", System.Data.SqlDbType.NVarChar, 50, "Report level"))
Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Param4", System.Data.SqlDbType.NVarChar, 50, "Report Type"))
Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Time", System.Data.SqlDbType.NVarChar, 50, "Time"))
Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Date", System.Data.SqlDbType.NVarChar, 50, "Date"))
Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Location", System.Data.SqlDbType.NVarChar, 50, "Location"))
Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_ReportID", System.Data.SqlDbType.NVarChar, 50, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "ReportID", System.Data.DataRowVersion.Original, Nothing))
Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_Date", System.Data.SqlDbType.NVarChar, 50, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "Date", System.Data.DataRowVersion.Original, Nothing))
Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_Location", System.Data.SqlDbType.NVarChar, 50, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "Location", System.Data.DataRowVersion.Original, Nothing))
Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_Report_Type", System.Data.SqlDbType.NVarChar, 50, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "Report Type", System.Data.DataRowVersion.Original, Nothing))
Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_Report_level", System.Data.SqlDbType.NVarChar, 50, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "Report level", System.Data.DataRowVersion.Original, Nothing))
Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_Time", System.Data.SqlDbType.NVarChar, 50, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "Time", System.Data.DataRowVersion.Original, Nothing))
'
'DataSet11
'
Me.DataSet11.DataSetName = "DataSet1"
Me.DataSet11.Locale = New System.Globalization.CultureInfo("en-US")
'
'DataGrid1
'
Me.DataGrid1.AlternatingBackColor = System.Drawing.SystemColors.Window
Me.DataGrid1.BackgroundColor = System.Drawing.Color.Lavender
Me.DataGrid1.DataMember = ""
Me.DataGrid1.DataSource = Me.DataSet11.Incident_Table
Me.DataGrid1.GridLineColor = System.Drawing.SystemColors.Control
Me.DataGrid1.HeaderBackColor = System.Drawing.SystemColors.Control
Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.DataGrid1.LinkColor = System.Drawing.SystemColors.HotTrack
Me.DataGrid1.Location = New System.Drawing.Point(8, 16)
Me.DataGrid1.Name = "DataGrid1"
Me.DataGrid1.SelectionBackColor = System.Drawing.SystemColors.ActiveCaption
Me.DataGrid1.SelectionForeColor = System.Drawing.SystemColors.ActiveCaptionText
Me.DataGrid1.Size = New System.Drawing.Size(528, 136)
Me.DataGrid1.TabIndex = 0
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(16, 168)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(72, 32)
Me.Button1.TabIndex = 1
Me.Button1.Text = "Get Data"
'
'ListBox2
'
Me.ListBox2.DataSource = Me.DataSet11.Incident_Table
Me.ListBox2.DisplayMember = "ReportID"
Me.ListBox2.Location = New System.Drawing.Point(552, 72)
Me.ListBox2.Name = "ListBox2"
Me.ListBox2.Size = New System.Drawing.Size(64, 82)
Me.ListBox2.TabIndex = 3
'
'ComboBox1
'
Me.ComboBox1.DataSource = Me.DataSet11.Incident_Table
Me.ComboBox1.DisplayMember = "ReportID"
Me.ComboBox1.Location = New System.Drawing.Point(552, 40)
Me.ComboBox1.Name = "ComboBox1"
Me.ComboBox1.Size = New System.Drawing.Size(104, 21)
Me.ComboBox1.TabIndex = 5
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(552, 16)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(104, 16)
Me.Label1.TabIndex = 6
Me.Label1.Text = "Select ReportID"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(112, 168)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(80, 32)
Me.Button2.TabIndex = 8
Me.Button2.Text = "Update"
'
'TextBox1
'
Me.TextBox1.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.DataSet11, "Incident Table.Report level"))
Me.TextBox1.Location = New System.Drawing.Point(112, 240)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(80, 20)
Me.TextBox1.TabIndex = 9
Me.TextBox1.Text = "Report Level"
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(112, 216)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(80, 23)
Me.Label2.TabIndex = 10
Me.Label2.Text = "Report Level"
'
'Label3
'
Me.Label3.Location = New System.Drawing.Point(16, 216)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(72, 23)
Me.Label3.TabIndex = 11
Me.Label3.Text = "ReportID"
'
'TextBox2
'
Me.TextBox2.Location = New System.Drawing.Point(16, 240)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.Size = New System.Drawing.Size(72, 20)
Me.TextBox2.TabIndex = 12
Me.TextBox2.Text = "ReportID"
'
'Label4
'
Me.Label4.Location = New System.Drawing.Point(208, 216)
Me.Label4.Name = "Label4"
Me.Label4.Size = New System.Drawing.Size(80, 23)
Me.Label4.TabIndex = 13
Me.Label4.Text = "Report Type"
'
'TextBox3
'
Me.TextBox3.Location = New System.Drawing.Point(208, 240)
Me.TextBox3.Name = "TextBox3"
Me.TextBox3.Size = New System.Drawing.Size(144, 20)
Me.TextBox3.TabIndex = 14
Me.TextBox3.Text = "Report Type"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(664, 389)
Me.Controls.Add(Me.TextBox3)
Me.Controls.Add(Me.Label4)
Me.Controls.Add(Me.TextBox2)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.ComboBox1)
Me.Controls.Add(Me.ListBox2)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.DataGrid1)
Me.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.DataSet11, "Incident Table.ReportID"))
Me.Name = "Form1"
Me.Text = "Incident App"
CType(Me.DataSet11, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SqlDataAdapter1.Fill(DataSet11)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
SqlDataAdapter1.Update(DataSet11)
End Sub
Private Sub DataGrid1_Navigate(ByVal sender As System.Object, ByVal ne As System.Windows.Forms.NavigateEventArgs) Handles DataGrid1.Navigate
End Sub
End Class
 
One quick tip, never post that much code. No one will bother reading it all.

You have not bound the textboxes to the fields of the dataset.

In the properties window set the databindings - text to the field of the dataset.
That will add a line of code to the textbox area simular to this:
Me.TextBox4.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.DataSet11, "Shop Floor.Part ID"))
DataSet11 is my dataset name.
Shop Floor is my table name.
Part ID is the field name.

Hope that helps,
Welcome to the forum!
 
ithunkvb said:
Americans are the best!!
Enough with the propaganda.
ithunkvb said:
How do i insert a row into the datagrid when i press the new entry button?
The DataGrid reflects what is in the DataSource. Add a row to the DataSource. Voila!
 
yeah ok this is the new entry button

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
'what do i write here'
End Sub

but what is the code to write in between the private and end sub statment
so that when i press the new entry button, the text fields will be blank and i can input data
 
Also, the DataGrid already has an empty row at the bottom for the user to enter a new record. If you add a row yourself then your table schema will either have to provide default values for all columns that do not allow nulls or else all columns will have to allow nulls.
 
hi thanks for the forms link it rocks
i do agree that ms sql server is better than ms access

btw how do i display the date and time in a vb.net application
 
ithunkvb said:
btw how do i display the date and time in a vb.net application
Please keep each thread to a single topic. If you have an unrelated question you should start a new thread. It keeps the forums more ordered and makes information easier to find.

As you've asked the question now, the DateTime structure has a methods to display a date and time as a string. Date.Now is the current date and time, while Date.Today is the current date with the time zeroed to midnight.
 
Back
Top