Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have searched for code for long time and i found this Grid View add quantity if data already exists..[^]

\but code not vb.net i want help to convert this code to vb.net





C#
//Boolean to check if he has row has been
           bool Found = false;
           if (dataGridView.Rows.Count > 0)
           {
 
               //Check if the product Id exists with the same Price
               foreach (DataGridViewRow row in dataGridView.Rows)
               {
                   if (Convert.ToString(row.Cells[0].Value) == textBox_ProductId.Text && Convert.ToString(row.Cells[1].Value) == textBox_Price.Text)
                   {
                       //Update the Quantity of the found row
                       row.Cells[2].Value = Convert.ToString(1 + Convert.ToInt16(row.Cells[2].Value));
                       Found = true;
                   }
 
               }
               if (!Found)
               {
                   //Add the row to grid view
                   dataGridView.Rows.Add(textBox_ProductId.Text, textBox_Price.Text, 1);
               }
 
           }
           else
           {
               //Add the row to grid view for the first time
               dataGridView.Rows.Add(textBox_ProductId.Text, textBox_Price.Text, 1);
           }
Posted

1 solution

There are web apps which can do the conversion for you, such as Telerik Code Converter[^], which gives the following VB.NET code:
VB.NET
'Boolean to check if he has row has been
Dim Found As Boolean = False
If dataGridView.Rows.Count > 0 Then

	'Check if the product Id exists with the same Price
	For Each row As DataGridViewRow In dataGridView.Rows
		If Convert.ToString(row.Cells(0).Value) = textBox_ProductId.Text AndAlso Convert.ToString(row.Cells(1).Value) = textBox_Price.Text Then
			'Update the Quantity of the found row
			row.Cells(2).Value = Convert.ToString(1 + Convert.ToInt16(row.Cells(2).Value))
			Found = True

		End If
	Next
	If Not Found Then
		'Add the row to grid view
		dataGridView.Rows.Add(textBox_ProductId.Text, textBox_Price.Text, 1)

	End If
Else
	'Add the row to grid view for the first time
	dataGridView.Rows.Add(textBox_ProductId.Text, textBox_Price.Text, 1)
End If
 
Share this answer
 
Comments
OriginalGriff 29-Aug-15 8:19am    
The depressing bit is that he can't write that code on his own... :sigh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900