Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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


C#
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();
                }

            }
        }
    }
Posted

1 solution

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
 
Share this answer
 

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