在不执行SQL查询的情况下搜索datagridview [英] Search a datagridview without doing an sql query

查看:51
本文介绍了在不执行SQL查询的情况下搜索datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个 DataGridview dgvProducts dgvCart
当我将产品从 dgvProducts 转移到 dgvCart 时,指定的数量将从第一个datagridview中扣除。

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.

但是问题是我的文本框搜索代码,它使用查询,因此即使事务未完成,每次也会重置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.

这是代码

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

    }

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

有没有只搜索datagridview的方法,所以它不需要做我每次搜索时都会重置数量的查询?谢谢。

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.

编辑:添加了crud方法

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


推荐答案

是的。您可以遍历行并设置不需要的内容:

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

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

但是,我会强烈建议避免这种情况。
更好的方法是将数据集存储到Datatable1中,例如:

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

如果每次用户单击列表时都需要更新第二个DGW的列表,则可以将数据操作到DataTable2中,例如:

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

但是要创建一个列表,将每个下一个选定的val相加ue,调用此函数:

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

然后将第二个数据库作为数据源放入第二个DataGridView中:

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()

编辑2:啊,我看到您正在使用它来收集第一个DataGridView中的行。然后,这样做更加简单,如上所述,但始终将第一个表设置为第一个datagridview的数据源,将第二个表设置为第二个DataGridView的源。并且不要删除旧行,将它们保留在第二组中。

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.

编辑:对不起,C#应该是这样的:

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;

这篇关于在不执行SQL查询的情况下搜索datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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