在网格中选择的行中搜索 [英] search in grid selected row

查看:185
本文介绍了在网格中选择的行中搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
我有网格视图窗口表单,需要在每一行中进行搜索
当我输入产品名称或条形码时,第一个单元格中有很多单元格是搜索单元格,其他所有单元格在单击Enter或离开该单元格或移动任何合适的事件后,都会从表格加载的数据表加载中自动完成,然后移至第二行并做同样的事情,等等
问题是,当我移至第二行时,搜索会从第一行获取数据,因此重复第一行中的数据,即使我转到了10行,同样的事情发生了,它仍然在搜索第一行中的数据
什么是最好的解决方案?
我的搜索代码是


hi everybody
I have grid view windows form I need to make search in each row
I have many cells in the first cells is a search cell when I enter the product name or barcode all the other cells make autocomplete from datatable load in form load after click enter or leave the cell or moving any suitable event then I move to second row and do the same thing and so on
the problem is that when I move to the second row the search get data from the first row and so repeat the data in the first row and even I go to the 10 row the same thing happen it is still search with data from first row
what is the best soulation ?
my search code is


private void dataGridView1_CellValidated(object sender, DataGridViewCellEventArgs e)
    {
        int count = dataGridView1.Columns.Count;
        if (e.RowIndex >= 0 && dataGridView1.Rows.Count > e.RowIndex && count > 0)
        {
            if (dataGridView1.Rows[e.RowIndex].Cells[1].Value != null)
            {
                string cellValue = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
                if (!string.IsNullOrEmpty(cellValue))
                {
                    _bar = cellValue;
                    PopulateColumns();
                    dataGridView1.Rows[e.RowIndex].Cells[2].Value = _proname;
                    dataGridView1.Rows[e.RowIndex].Cells[3].Value = _price;
                    dataGridView1.Rows[e.RowIndex].Cells[4].Value = _unit;


                }
                dataGridView1.Refresh();
            }

            else
            {
                if (dataGridView1.Rows[e.RowIndex].Cells[2].Value != null)
                {
                    string cellValuebar = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
                    if (!string.IsNullOrEmpty(cellValuebar))
                    {

                        _id1 = cellValuebar;
                        PopulateColumns();
                        dataGridView1.Rows[e.RowIndex].Cells[1].Value = _barcode;
                        dataGridView1.Rows[e.RowIndex].Cells[3].Value = _price;
                        dataGridView1.Rows[e.RowIndex].Cells[4].Value = _unit;

                    }
                    dataGridView1.Refresh();
                }

            }
        }
    }

推荐答案

最好的解决方案是拥有一个单独的数据层,使用 binding 将数据源绑定到您的控件并执行您在数据中搜索,而不是在控制中.

例如,请参阅本教程(CodeProject):详细的数据绑定教程 [ ^ ].

另请参阅: https://msdn .microsoft.com/en-us/library/System.Windows.Forms.DataGridView.DataSource%28v = VS.110%29.aspx [ https://msdn.microsoft. com/en-us/library/system.windows.forms.datagridview.rows%28v = vs.110%29.aspx [ https://msdn.microsoft. com/en-us/library/system.windows.forms.datagridviewrow.cells%28v = vs.110%29.aspx [
The best solution would be having a separate data layer, bind data source to your control using binding and perform your search in data, not in control.

See, for example, this tutorial (CodeProject): A Detailed Data Binding Tutorial[^].

See also: https://msdn.microsoft.com/en-us/library/System.Windows.Forms.DataGridView.DataSource%28v=VS.110%29.aspx[^].

As to your code sample, it doesn''t search anything, because you never traverse any set of cells. It become apparent to anyone who can notice that you never use any for or foreach loops. At the same time, you get some cells by hard-coded indices, which is not only bad for maintenance, but is simply unsafe. I think, there is nothing to discuss here. You have the collection properties like
https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.rows%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewrow.cells%28v=vs.110%29.aspx[^].

You can do some search in either row loop or two nested loops, to access all cells, or do something according to you storage semantics. But working with data layer is the most robust and decent approach.

—SA


这篇关于在网格中选择的行中搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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