如何使用文本框搜索datagridview的数据,如果匹配,则将其数据添加到第二个datagridview [英] How do I search data of a datagridview using a textbox and if it matches add its data to the second datagridview

查看:113
本文介绍了如何使用文本框搜索datagridview的数据,如果匹配,则将其数据添加到第二个datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是该应用的链接

https://imgur.com / a / sB1eE [ ^ ]



我有一个带有两个datagridview的表单,第一个是从csv添加项目或从textbox手动添加。我想要做的是我想使用搜索按钮和文本框在第一个gridview中搜索项目并添加该项目的数量,如果项目与第一个gridview中的数据匹配,则必须添加项目名称,类别,第二个网格视图中项目的数量和价格,并计算总价值*数量如何执行此操作我只是在文本框的数据与gridview匹配时进行了基本验证。



我尝试过:



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;

                    }

                }

推荐答案

试试这个,根据你的修改需要



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;
              }
          }

      }


这篇关于如何使用文本框搜索datagridview的数据,如果匹配,则将其数据添加到第二个datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆