Click here to Skip to main content
15,915,869 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
This is the link of the app
https://imgur.com/a/sB1eE[^]

I have a form with two datagridview the first one is to add items from csv or manually from textbox. What i want to do is i want to search for a item in the first gridview using the search button and the textbox and add quantity of that item and if the item matches the data from the first gridview it must add the item name , category,quantity and price of the item into the second gridview and calculate the total which is Price * Quantity how do i do this i just did a basic validation if the textbox's data is matching the gridview or not.

What I have tried:

for (int i = 0; i < dataGridView1.Rows.Count; i++)

            {

                for (int j = 0; j < dataGridView1.Columns.Count; j++)

                {

                    if (dataGridView1.Rows[i].Cells[j].Value != null && 
                       searchTextBox.Text == 
                     dataGridView1.Rows[i].Cells[j].Value.ToString())

                    {

                        MessageBox.Show("The value already existed in DataGridView.");
                        searchTextBox.Text = "";

                        break;

                    }

                }
Posted
Updated 5-Jan-18 18:37pm
v3

1 solution

try this, modify it according to your need

public class SearchItem
      {
          public string Item_Name { get; set; }
          public string Category { get; set; }
          public double Price { get; set; }
          public double Quantity { get; set; }
          public double Total { get; set; }
      }

      private void btnSearch_Click(object sender, EventArgs e)
      {
          string search = txtkeyword.Text.Trim();
          double quantity;
          if (!double.TryParse(txtQty.Text.Trim(), out quantity))
          {
              MessageBox.Show("Please enter a valid quantity");
              return;
          }

          foreach (DataGridViewRow row in dataGridView1.Rows)
          {
              string item_Name = row.Cells[0].Value.ToString();
              if (search == item_Name)
              {
                  SearchItem item = new SearchItem()
                  {
                      Item_Name = item_Name,
                      Category = row.Cells[1].Value.ToString(),
                      Quantity = quantity,
                      Price = Convert.ToInt32(row.Cells[2].Value.ToString())
                  };
                  item.Total = item.Quantity * item.Price;
                  dataGridView2.Rows.Add(item.Item_Name, item.Category, item.Quantity, item.Price, item.Total);
                  break;
              }
          }

      }
 
Share this answer
 
Comments
MaddoxSt 6-Jan-18 9:55am    
Yeah it worked thanks a lot :)
Karthik_Mahalingam 6-Jan-18 10:32am    
welcome
MaddoxSt 6-Jan-18 20:54pm    
bro im new to this i dont see the tick as answer button here
Karthik_Mahalingam 6-Jan-18 22:04pm    
"Accept solution" button on top of this solution

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