hi!
i have a DataList view in created in vb.net (4 columns)
how can i make this work?:

the data from the first two columns will be from the database
and the next two columns will be inputted by the user.
and when i click save, all of the data in the datalist view will
be save again in another table in the database.

sorry..new to vb.net.
tnx in advance.

by the way im using MYSQL.:-/

Hey there,
You first have to download the MYSQL Connector for .NET, from the official site:

MySQL :: Download

When you do that you need to write a few lines of code to interact with database, like this:

dim _cn as New MySqlConnection([I][B]Connection String[/B][/I]) 
dim cmd as new MySqlCommand 

With cmd 
.Command = [B][I]Insert Command[/I][/B]
.CommandType = CommandType.Text 
.Connection = cn 
End With 

cn.Open() 

cmd.ExecuteNonQuery 

cn.Close

Hope i helped,
Alex. :)

I did an example code for you only, so you can see how this should be done:

Private dgv As DataGridView
Private tableMain As DataTable
Public Sub New()
	InitializeComponent()

	'creating and adding columns to dgv:
	dgv = New DataGridView()
	dgv.Width = 470
	Me.Controls.Add(dgv)

	'get data from DB:
	tableMain = GetDataFromDB()
	'creating two more column to dataTable:
	tableMain.Columns.AddRange(New DataColumn() {New DataColumn("column 3", GetType(String)), New DataColumn("column 4", GetType(String))})
	'bind table to dgv:
	dgv.DataSource = tableMain.DefaultView
End Sub

Private Function GetDataFromDB() As DataTable
	Dim table As New DataTable()
	'get data from database...

	'I will only create two columns and add some example rows..
	table.Columns.AddRange(New DataColumn() {New DataColumn("column 1", GetType(Integer)), New DataColumn("column 2", GetType(String))})
	For i As Integer = 1 To 5
		table.Rows.Add(i, "item " & i)
	Next

	Return table
End Function

Private Sub button1_Click(sender As Object, e As EventArgs)
	'mow lets insert data to DB:
	Dim bOk As Boolean = True
	Using sqlConn As New SqlConnection("connString")
		Dim cmd As SqlCommand = Nothing
		For Each row As DataRow In tableMain.Rows
			cmd = New SqlCommand()
			cmd.CommandText = "INSERT INTO MyTable VALUES (@param1, @param2, @param3, @param4)"
			cmd.Parameters.Add("@param1", SqlDbType.Int).Value = Convert.ToInt32(row(0))
			cmd.Parameters.Add("@param2", SqlDbType.VarChar, 50).Value = row(1).ToString()
			cmd.Parameters.Add("@param3", SqlDbType.VarChar, 50).Value = row(2).ToString()
			cmd.Parameters.Add("@param4", SqlDbType.VarChar, 50).Value = row(3).ToString()
			Try
				cmd.ExecuteNonQuery()
			Catch ex As Exception
				MessageBox.Show(ex.Message)
				bOk = False
				Exit Try
			End Try
		Next
	End Using
	If bOk Then
		MessageBox.Show("All data inserted succesfully.")
	End If
End Sub

Hopeit helps.

I did an example code for you only, so you can see how this should be done:

Private dgv As DataGridView
Private tableMain As DataTable
Public Sub New()
	InitializeComponent()

	'creating and adding columns to dgv:
	dgv = New DataGridView()
	dgv.Width = 470
	Me.Controls.Add(dgv)

	'get data from DB:
	tableMain = GetDataFromDB()
	'creating two more column to dataTable:
	tableMain.Columns.AddRange(New DataColumn() {New DataColumn("column 3", GetType(String)), New DataColumn("column 4", GetType(String))})
	'bind table to dgv:
	dgv.DataSource = tableMain.DefaultView
End Sub

Private Function GetDataFromDB() As DataTable
	Dim table As New DataTable()
	'get data from database...

	'I will only create two columns and add some example rows..
	table.Columns.AddRange(New DataColumn() {New DataColumn("column 1", GetType(Integer)), New DataColumn("column 2", GetType(String))})
	For i As Integer = 1 To 5
		table.Rows.Add(i, "item " & i)
	Next

	Return table
End Function

Private Sub button1_Click(sender As Object, e As EventArgs)
	'mow lets insert data to DB:
	Dim bOk As Boolean = True
	Using sqlConn As New SqlConnection("connString")
		Dim cmd As SqlCommand = Nothing
		For Each row As DataRow In tableMain.Rows
			cmd = New SqlCommand()
			cmd.CommandText = "INSERT INTO MyTable VALUES (@param1, @param2, @param3, @param4)"
			cmd.Parameters.Add("@param1", SqlDbType.Int).Value = Convert.ToInt32(row(0))
			cmd.Parameters.Add("@param2", SqlDbType.VarChar, 50).Value = row(1).ToString()
			cmd.Parameters.Add("@param3", SqlDbType.VarChar, 50).Value = row(2).ToString()
			cmd.Parameters.Add("@param4", SqlDbType.VarChar, 50).Value = row(3).ToString()
			Try
				cmd.ExecuteNonQuery()
			Catch ex As Exception
				MessageBox.Show(ex.Message)
				bOk = False
				Exit Try
			End Try
		Next
	End Using
	If bOk Then
		MessageBox.Show("All data inserted succesfully.")
	End If
End Sub

Hopeit helps.

hey mitja!!
tnks for the reply..
i tried first the displaying part not yet the saving.. :-)
it worked for me..
but how can i set the position of the datalistview..
because by default, it is placed on top of the form.

tnx so much..

hey mitja!!
tnks for the reply..
i tried first the displaying part not yet the saving.. :-)
it worked for me..
but how can i set the position of the datalistview..
because by default, it is placed on top of the form.

tnx so much..

additional request:
when i input data from the 3rd column a computation
will be done and it will be displayed in the fourth
column after hitting enter.
how can i do this??

tnx soooooooo much.. ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.