Search a datagridview without doing an sql query

[亡魂溺海] 提交于 2020-02-08 05:16:45

问题


I have two DataGridview's, dgvProducts and dgvCart. When I transfer a product to from dgvProducts to dgvCart, the specified quantity will deduct from the first datagridview.

But the problem is my textbox search code, it is using a query so the datagridview quantity are reset everytime even when the transaction is not finish.

This is the code for the search of textbox.

     private void textOrderSearch_TextChanged(object sender, EventArgs e)
    {
        if (textOrderSearch.Text != "")
        {
            crud.FillDataGrid("Select ProductID,BrandName,GenericName,Form,Dosage,Quantity,SellingPrice,D,VE  from Products where Status = 'Active' and Quantity > 0 and (ProductID Like  '%" + textOrderSearch.Text + "%' or BrandName Like '%" + textOrderSearch.Text + "%'  or GenericName Like  '%" + textOrderSearch.Text + "%' or Form Like  '%" + textOrderSearch.Text + "%' or Dosage Like  '%" + textOrderSearch.Text + "%'  )   ", ref dgvPOSproduct);             
            dgvPOSproduct.Columns[0].HeaderText = "ProductID";
            dgvPOSproduct.Columns[1].HeaderText = "Brand";
            dgvPOSproduct.Columns[2].HeaderText = "Generic";
            dgvPOSproduct.Columns[3].HeaderText = "Form";
            dgvPOSproduct.Columns[4].HeaderText = "Dosage";
            dgvPOSproduct.Columns[5].HeaderText = "Qty";
            dgvPOSproduct.Columns[6].HeaderText = "Price";
            dgvPOSproduct.Columns[7].HeaderText = "D";
            dgvPOSproduct.Columns[8].HeaderText = "VE";

            dgvPOSproduct.Columns[0].Width = 65;
            dgvPOSproduct.Columns[1].Width = 80;
            dgvPOSproduct.Columns[2].Width = 80;
            dgvPOSproduct.Columns[3].Width = 58;
            dgvPOSproduct.Columns[4].Width = 58;
            dgvPOSproduct.Columns[5].Width = 45;
            dgvPOSproduct.Columns[6].Width = 55;
            dgvPOSproduct.Columns[7].Width = 35;
            dgvPOSproduct.Columns[8].Width = 35;          
        }
        else
        {
            dgvProductSettings();
        }

    }

And the code for the method of populating the datagridview.

    private void dgvProductSettings()
    {
        crud.FillDataGrid("Select ProductID,BrandName,GenericName,Form,Dosage,Quantity,SellingPrice,D,VE from Products where Status = 'Active' and Quantity > 0", ref dgvPOSproduct);

        dgvPOSproduct.Columns[0].HeaderText = "ProductID";
        dgvPOSproduct.Columns[1].HeaderText = "Brand";
        dgvPOSproduct.Columns[2].HeaderText = "Generic";
        dgvPOSproduct.Columns[3].HeaderText = "Form";
        dgvPOSproduct.Columns[4].HeaderText = "Dosage";
        dgvPOSproduct.Columns[5].HeaderText = "Qty";
        dgvPOSproduct.Columns[6].HeaderText = "Price";
        dgvPOSproduct.Columns[7].HeaderText = "D";
        dgvPOSproduct.Columns[8].HeaderText = "VE";

        dgvPOSproduct.Columns[0].Width = 65;
        dgvPOSproduct.Columns[1].Width = 80;
        dgvPOSproduct.Columns[2].Width = 80;
        dgvPOSproduct.Columns[3].Width = 58;
        dgvPOSproduct.Columns[4].Width = 58;
        dgvPOSproduct.Columns[5].Width = 45;
        dgvPOSproduct.Columns[6].Width = 55;
        dgvPOSproduct.Columns[7].Width = 35;
        dgvPOSproduct.Columns[8].Width = 35;
    }

Is there a way to search the datagridview only so it will not need to do a query that is reseting the quantity everytime i do a search? Thank you.

EDIT: added crud method

       public crud()
    {
        cnString = "Data Source=BENJOPC\\SQLEXPRESS;Initial Catalog=MARISCHELLdatabase;Integrated Security=True";
        cn = new SqlConnection(cnString);
    }




    public void FillDataGrid(string sql, ref ns1.BunifuCustomDataGrid dg)
    {
        try
        {
            DataSet ds = new DataSet();
            cn.Open();
            cmd = new SqlCommand(sql, cn);
            adptr = new SqlDataAdapter(cmd);
            adptr.Fill(ds);
            dg.DataSource = "";
            dg.DataSource = ds.Tables[0];


        }
        catch (Exception e)
        {
            MessageBox.Show("" + e.Message);
        }
        cn.Close();
    }

回答1:


Yes, there is. You can loop through the rows and set unwanted:

Me.dgwList.Rows(0).Visible = False 

However, I would strongly advice to avoid it. A better way would be to storing your dataset into a Datatable1 like:

' define those as *global* variables (outside subs and functions)
Dim dtMyTable1 as New Datatable
Dim dtMyTable2 as New Datatable

' Load your data from database into first dtMyTable and assign it to DGW
' instead of datasource
dtMyTable1 = ds.tables(0)
Me.dgwList.datasource = dtMyTable1

If you needed to update a list of second DGW every time user clicked a list, then you'd manipulate data into DataTable2 like:

Private Sub UpdateSelection()
    For ir = 0 to me.dtMyTable.Rows.Count - 1
        Dim dr as datagridviewRow = me.dtMyTable.Rows(ir)
        If dr("Brand") = BrandString Then
           dtMyTable2.ImportRow(dtMyTable.Item(I).Row)
        End If
    Next ir
End Sub

But to make a list to add up every next selected value, call this function:

Private Sub AddSelectedRowToSelection()
    If Me.dgwList.SelectedRows.Count > 0 Then   ' only if a row is selected
       ' add this row
       dtMyTable2.ImportRow(dtMyTable.Item(Me.dgwList.SelectedRows(0).Index).Row)  
    End If
End Sub

And then put the second db into second DataGridView as datasource:

Me.SeconddgwList.datasource = dtMyTable2

You can empty the second list (delete rows but keep columns) like this:

dtMyTable2.Clear()

EDIT 2: Ahh, I see you're using it for collecting the rows from the first DataGridView. Then it's even simpler, do as above, but always set first table as datasource for the first datagridview and second table as source for second DataGridView. And do not remove old rows, keep them in the second set.

EDIT: Sorry, C# should be like this:

this.dgwList.Rows(0).Visible == false

Datatable dtMyTable = new Datatable();
Datatable dtMyTable2 = new Datatable();


dtMyTable = ds.tables(0);


for (ir = 0; ir <= this.dtMyTable.Rows.Count - 1; ir++) {
    datagridviewRow dr = this.dtMyTable.Rows(ir);
    if (dr("Brand") == BrandString) {
        dtMyTable2.ImportRow(dtMyTable.Item(I).Row);
    }
}

private void AddSelectedRowToSelection()
{
    // only if a row is selected
    if (this.dgwList.SelectedRows.Count > 0) {
        // add this row
        dtMyTable2.ImportRow(dtMyTable.Item(this.dgwList.SelectedRows(0).Index).Row);
    }
}

this.dgwList.datasource = dtMyTable2;



回答2:


I manage to solve my problem here is the code i used.

 DataTable dt = new DataTable("Products");
    private void dgvProductNew()
    {
        try
        {
            using (SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["cnn"].ConnectionString))
            {
                if (cnn.State == ConnectionState.Closed)
                    cnn.Open();
                using (SqlDataAdapter da = new SqlDataAdapter("Select ProductID, BrandName, GenericName, Form, Dosage, Quantity, SellingPrice, D, VE from Products where Status = 'Active' and Quantity > 0", cnn))
                {          
                    da.Fill(dt);
                    dgvPOSproduct.DataSource = dt;               
                }
            }
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

and

      private void textOrderSearch_TextChanged(object sender, EventArgs e)
    {
        DataView dv = dt.DefaultView;
        dv.RowFilter = string.Format("BrandName like '%{0}%' ", textOrderSearch.Text);
        dgvPOSproduct.DataSource = dt;
        dgvPOSproduct.Columns[0].HeaderText = "ProductID";
        dgvPOSproduct.Columns[1].HeaderText = "Brand";
        dgvPOSproduct.Columns[2].HeaderText = "Generic";
        dgvPOSproduct.Columns[3].HeaderText = "Form";
        dgvPOSproduct.Columns[4].HeaderText = "Dosage";
        dgvPOSproduct.Columns[5].HeaderText = "Qty";
        dgvPOSproduct.Columns[6].HeaderText = "Price";
        dgvPOSproduct.Columns[7].HeaderText = "D";
        dgvPOSproduct.Columns[8].HeaderText = "VE";

        dgvPOSproduct.Columns[0].Width = 65;
        dgvPOSproduct.Columns[1].Width = 80;
        dgvPOSproduct.Columns[2].Width = 80;
        dgvPOSproduct.Columns[3].Width = 58;
        dgvPOSproduct.Columns[4].Width = 58;
        dgvPOSproduct.Columns[5].Width = 45;
        dgvPOSproduct.Columns[6].Width = 55;
        dgvPOSproduct.Columns[7].Width = 35;
        dgvPOSproduct.Columns[8].Width = 35;
    }


来源:https://stackoverflow.com/questions/45798703/search-a-datagridview-without-doing-an-sql-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!