הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Sunday, June 18, 2017 12:25 AM
hello all
how to add text from textbox into datagridview in column(0)
All replies (18)
Sunday, June 18, 2017 12:48 AM
If the DataGridView does not have the DataSource property set the following will insert text into the first row, first column
DataGridView1.Rows(0).Cells(0).Value = TextBox1.Text
If the DataSource is set, e.g. you have loaded a DataTable into a BindingSource then set the BindingSource as the DataSource of the DataGridView then we would do the current row, first column were the first column for demoing is for Text with the Field Name of FirstName.
Declare Bindingsource
Private bsData As New BindingSource
Here dt is our DataTable
bsData.DataSource = dt
DataGridView1.DataSource = bsData
Set the value
CType(bsData.Current, DataRowView).Row.SetField(Of String)("FirstName", TextBox1.Text)
Or
CType(bsData.DataSource, DataTable).Rows(DataGridView1.CurrentRow.Index).SetField(Of String)("FirstName", TextBox1.Text)
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator
Sunday, June 18, 2017 1:10 AM | 1 vote
If the DataGridView does not have the DataSource property set the following will insert text into the first row, first column
DataGridView1.Rows(0).Cells(0).Value = TextBox1.Text
If the DataSource is set, e.g. you have loaded a DataTable into a BindingSource then set the BindingSource as the DataSource of the DataGridView then we would do the current row, first column were the first column for demoing is for Text with the Field Name of FirstName.
Declare Bindingsource
Private bsData As New BindingSource
Here dt is our DataTable
bsData.DataSource = dt DataGridView1.DataSource = bsData
Set the value
CType(bsData.Current, DataRowView).Row.SetField(Of String)("FirstName", TextBox1.Text)
Or
CType(bsData.DataSource, DataTable).Rows(DataGridView1.CurrentRow.Index).SetField(Of String)("FirstName", TextBox1.Text)
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator
karen
if use this code the value change not add
DataGridView1.Rows(0).Cells(0).Value = TextBox1.Text
Sunday, June 18, 2017 1:47 AM | 1 vote
Well I can't assist without more details e.g. how is the DataGridView loaded.
I will be out the rest of the evening and will look for your response tomorrow morning.
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator
Sunday, June 18, 2017 2:17 AM
If DataGridView1.DataSource Is Nothing Then
With DataGridView1
.Rows.Add(TextBox1.Text)
End With
End If
Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi
Sunday, June 18, 2017 7:53 AM
Set in top of your code Option Strict On
By the way, your code is complete nonsense, you cannot add a string to a row in a datagridview, only to a cell.
(If the row and cell already exist)
Success
Cor
Sunday, June 18, 2017 9:23 AM | 1 vote
i need to put the value from textbox to "QTY"
dataa = New MySqlDataAdapter("SELECT name, price FROM food_details WHERE name ='" & Tabel1.sname & "'", conn)
'datas = New DataSet()
dataa.Fill(datas)
Tabel1.DataGridView1.DataSource = datas.Tables(0)
'datas.Tables(0)
Tabel1.DataGridView1.Rows.Add(NumericUpDown1.Value)
'Tabel1.DataGridView1.Rows(0).Cells(0).Value = Tabel1.TextBox1.Text
'For c As Integer = 0 To Tabel1.DataGridView1.Rows.Count - 1
' Tabel1.DataGridView1.CurrentRow.Cells(c).Value = Me.NumericUpDown1.Value
' 'Tabel1.DataGridView1.Rows(0).Cells("amount").Value = "Any Thing"
'Next
If Tabel1.DataGridView1.RowCount > 1 Then
Dim qty As Integer
Dim price As Integer
Dim itemamount As Integer
qty = Tabel1.DataGridView1.CurrentRow.Cells(0).Value
price = Tabel1.DataGridView1.CurrentRow.Cells(3).Value
itemamount = qty * price
Tabel1.DataGridView1.CurrentRow.Cells(1).Value = itemamount
Dim amount As Decimal = 0
For index As Integer = 0 To Tabel1.DataGridView1.RowCount - 1
amount += Convert.ToDecimal(Tabel1.DataGridView1.Rows(index).Cells(1).Value)
Next
Tabel1.Label1.Text = amount
End If
conn.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
Sunday, June 18, 2017 9:41 AM
I see in your previous post that you use a datatable as datasource.
Then don't try to use the datagridview to enter data.
That is as going first to Baghdad if you want to change something where you live (I assume you don't live in Baghdad).
Simple change the value in the datatable and it will be shown in the DataGridView
Success
Cor
Sunday, June 18, 2017 10:11 AM
I see in your previous post that you use a datatable as datasource.
Then don't try to use the datagridview to enter data.
That is as going first to Baghdad if you want to change something where you live (I assume you don't live in Baghdad).
Simple change the value in the datatable and it will be shown in the DataGridView
Success
Cor
sir can you get me a simple or example bcoz im not perfect with vb.net
thanks
kind regards
Sunday, June 18, 2017 10:33 AM
Imports System.Configuration
Imports System.Data.Common
Public Class Form1
Private Da As OleDb.OleDbDataAdapter
Private Dt As New DataTable
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Button1.Text = "Start"
Button3.Text = "Change"
Button2.Text = "Update"
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim con As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Test\NwindCopy.MDB;")
Da = New OleDb.OleDbDataAdapter("Select EmployeeId, FirstName, LastName from Employees Where EmployeeId < 4", con)
Da.Fill(Dt)
DataGridView1.AllowUserToAddRows = False
DataGridView1.AllowUserToDeleteRows = False
DataGridView1.DataSource = Dt
Dim cmb As New OleDb.OleDbCommandBuilder(Da)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button2.Click
DataGridView1.EndEdit()
Da.Update(Dt)
DataGridView1.Refresh()
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dt.Rows(0).Item(2) = "Al Samir"
End Sub
End Class
(As you can see, I did only change the last name)
Success
Cor
Sunday, June 18, 2017 10:55 AM
Imports System.Configuration Imports System.Data.Common Public Class Form1 Private Da As OleDb.OleDbDataAdapter Private Dt As New DataTable Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load Button1.Text = "Start" Button3.Text = "Change" Button2.Text = "Update" End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim con As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Test\NwindCopy.MDB;") Da = New OleDb.OleDbDataAdapter("Select EmployeeId, FirstName, LastName from Employees Where EmployeeId < 4", con) Da.Fill(Dt) DataGridView1.AllowUserToAddRows = False DataGridView1.AllowUserToDeleteRows = False DataGridView1.DataSource = Dt Dim cmb As New OleDb.OleDbCommandBuilder(Da) End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click DataGridView1.EndEdit() Da.Update(Dt) DataGridView1.Refresh() End Sub Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click Dt.Rows(0).Item(2) = "Al Samir" End Sub End Class
(As you can see, I did only change the last name)
Success
Cor
i know this step but please look to my problem
Sunday, June 18, 2017 12:11 PM
Can you paste in the code like I did?
Using that button with <> upon it.
In this way, it goes simply to fast to look at it.
Success
Cor
Sunday, June 18, 2017 12:15 PM
Can you paste in the code like I did?
Using that button with <> upon it.
In this way, it goes simply to fast to look at it.
Success
Cor
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
'Dim i As Integer
'str = "SELECT * FROM food_details WHERE name = '" & Tabel1.sname & "'"
'With comm
' .CommandText = str
' .Connection = conn
'End With
'With dataa
' .SelectCommand = comm
' .Fill(datat)
'End With
'If Tabel1.ListView2.SelectedItems.Count > 0 Then
' For i = 0 To datat.Rows.Count - 1
' With Tabel1.ListView3
' .Items.Add(datat.Rows(i)("name"))
' With .Items(.Items.Count - 1).SubItems
' .Add(datat.Rows(i)("price"))
' .Add((NumericUpDown1.Value))
' .Add(datat.Rows(i)("categories"))
' End With
' End With
' Next
dataa = New MySqlDataAdapter("SELECT name, price FROM food_details WHERE name ='" & Tabel1.sname & "'", conn)
'datas = New DataSet()
dataa.Fill(datas)
Tabel1.DataGridView1.DataSource = datas.Tables(0)
'datas.Tables(0)
'Tabel1.DataGridView1.DataSource(NumericUpDown1.Value)
'Tabel1.DataGridView1.Rows(0).Cells(0).Value = Tabel1.TextBox1.Text
'For c As Integer = 0 To Tabel1.DataGridView1.Rows.Count - 1
' Tabel1.DataGridView1.CurrentRow.Cells(c).Value = Me.NumericUpDown1.Value
' 'Tabel1.DataGridView1.Rows(0).Cells("amount").Value = "Any Thing"
'Next
If Tabel1.DataGridView1.RowCount > 1 Then
Dim qty As Integer
Dim price As Integer
Dim itemamount As Integer
qty = Tabel1.DataGridView1.CurrentRow.Cells(0).Value
price = Tabel1.DataGridView1.CurrentRow.Cells(3).Value
itemamount = qty * price
Tabel1.DataGridView1.CurrentRow.Cells(1).Value = itemamount
Dim amount As Decimal = 0
For index As Integer = 0 To Tabel1.DataGridView1.RowCount - 1
amount += Convert.ToDecimal(Tabel1.DataGridView1.Rows(index).Cells(1).Value)
Next
Tabel1.Label1.Text = amount
End If
conn.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
'Me.Close()
End Sub
Sunday, June 18, 2017 12:24 PM
@Al Samir
You use a very weird way to fill your datagridview while it is about Database data.
It can be used in your way for non database data, but that is not for what it is meant with a database.
Try my sample with your own SQL command and connection in it.
Now it looks if you tell:
"I can not go on the highway" and then if people ask how you drive on that show you are walking on your hands.
Success
Cor
Sunday, June 18, 2017 12:55 PM
@Al Samir
You use a very weird way to fill your datagridview while it is about Database data.
It can be used in your way for non database data, but that is not for what it is meant with a database.
Try my sample with your own SQL command and connection in it.
Now it looks if you tell:
"I can not go on the highway" and then if people ask how you drive on that show you are walking on your hands.
Success
Cor
okay i like your code but can add manualy column , i get column "name" and "price" from database sql but i need to add column it has nothing to do with the database just for calculation "price" * Qty
Sunday, June 18, 2017 1:06 PM | 1 vote
Here is a working example for getting data by name (user selects from valid items in a ComboBox), update a field in the DataGridView by way of a BindingSource, get total for price field into a label.
To add a new row, cast the BindingSource.DataSource to a DataTable and then use DataTable.Rows.Add to do this. If more than one then use a for/next to do the add.
I used SQL-Server where the data provider is SqlClient, you would change to MySql data provider (I don't have MySql to test it with).
Class responsible for obtaining data from database.
Imports System.Data.SqlClient
Public Class Operations
Private ConnectionString As String =
"Data Source=KARENS-PC;Initial Catalog=ForumExamples;Integrated Security=True"
Public Function GetData(ByVal pName As String) As DataTable
Dim dt As New DataTable
Using cn As New SqlConnection With {.ConnectionString = ConnectionString}
Using cmd As New SqlCommand With {.Connection = cn}
cmd.CommandText = "SELECT id,Name,Price,Comment FROM food_details WHERE Name = @Name"
cmd.Parameters.AddWithValue("@Name", pName)
cn.Open()
dt.Load(cmd.ExecuteReader)
End Using
End Using
Return dt
End Function
Public Function FoodTypes() As List(Of String)
Dim foodTypeList As New List(Of String)
Using cn As New SqlConnection With {.ConnectionString = ConnectionString}
Using cmd As New SqlCommand With {.Connection = cn}
cmd.CommandText = "SELECT Name FROM food_details GROUP BY name"
cn.Open()
Dim reader As SqlDataReader = cmd.ExecuteReader
While reader.Read
foodTypeList.Add(reader.GetString(0))
End While
End Using
End Using
Return foodTypeList
End Function
End Class
Form code
Public Class AddToDataGridViewForm
Private ops As Operations = New Operations
Private bsData As New BindingSource
Private Sub AddToDataGridViewForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
cboFoodTypes.DataSource = ops.FoodTypes
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
bsData.DataSource = ops.GetData(cboFoodTypes.Text)
DataGridView1.DataSource = bsData
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If bsData.DataSource IsNot Nothing Then
If bsData.Current IsNot Nothing Then
CType(bsData.Current, DataRowView).Row.SetField(Of String)("Comment", TextBox1.Text)
End If
End If
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
If bsData.DataSource IsNot Nothing Then
If bsData.Count > 0 Then
Label1.Text = CType(bsData.DataSource, DataTable) _
.AsEnumerable.Select(Function(data) data.Field(Of Decimal)("price")) _
.Sum.ToString
End If
End If
End Sub
End Class
Screenshot
Table schema
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator
Sunday, June 18, 2017 9:29 PM
Set in top of your code Option Strict On
By the way, your code is complete nonsense, you cannot add a string to a row in a datagridview, only to a cell.
(If the row and cell already exist)
Success
Cor
youre talking to me?
Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi
Sunday, June 18, 2017 10:47 PM
okay i like your code but can add manualy column , i get column "name" and "price" from database sql but i need to add column it has nothing to do with the database just for calculation "price" * Qty
You can build a table from the database that has that column (values could be null) and link that table to your DGV. That is, make a data table that matches the data you want to use. But it seems from your description that the DGV should not be linked to a datatable. You were offered the option originally of linking the database to the table or building the DGV in code - it seems that the better choice would have been to construct the DGV with the columns you want and populate it row-by-row from the items in the database table.
Monday, June 19, 2017 6:44 AM
Hi AL SAMIR,
In my opinion, I think you want to calculate the Qty * Price value and assign this value to DataGridView1.CurrentRow.Cells(1).Value, you can refer to the code below, but when you have finished assigning, you need to update the value to the database.
Dim a As String
Dim b As String
Private Sub Form20_Load(sender As Object, e As EventArgs) Handles MyBase.Load
fun2()
End Sub
Private Sub fun2()
Dim dt As New DataTable()
Dim str As String = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=D:\C# and VB Support\Example(VB)\Sample\Data1.mdf;Integrated Security=True"
Using con As New SqlConnection(str)
Dim sql As String = "select * from test4"
Using com As New SqlCommand(sql, con)
Try
con.Open()
Dim data As New SqlDataAdapter(com)
data.Fill(dt)
DataGridView1.DataSource = dt
con.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Using
End Using
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells("qty").Value = TextBox1.Text.ToString()
Dim a As Decimal = Convert.ToDecimal(DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells("Price").Value.ToString())
DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells("Totalprice").Value = (a * Convert.ToInt32(TextBox1.Text)).ToString()
End Sub
About updating datagridview value into database, please refer to:
https://stackoverflow.com/questions/11379615/datagridview-save-changes-to-database-vb-net
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.