0

我有一个datagridview和 5 ComboBoxes,我正在尝试使用 ComboBox 的文本值过滤网格的行。网格填充存储过程取决于FormLoad事件中的某些条件。btn_filter我应该在点击事件中再次填写它吗?

无论如何,这个异常在下面的代码中被捕获:cannot bind to datatable with no name.

这是代码btn_filter

    private void btnFilter_Click(object sender, EventArgs e)
    {
        String filterStr = "";

        if (cmbGrp.Text.Trim() != String.Empty)
            filterStr +=  "group ='" + cmbGrp.Text.Trim() + "' and";
        if(cmbMdl.Text.Trim() != String.Empty)
            filterStr += " model ='" + cmbMdl.Text.Trim() + "' and";
        if (cmbTrh.Text.Trim() != String.Empty)
            filterStr += " tarh ='" + cmbTrh.Text.Trim() + "' and";
        if (cmbSiz.Text.Trim() != String.Empty)
            filterStr += " size ='" + cmbSiz.Text.Trim() + "' and";
        if (cmbClr.Text.Trim() != String.Empty)
            filterStr += " color ='" + cmbClr.Text.Trim() + "'";            

        if (filterStr.LastIndexOf("and") == filterStr.Length - 3)
            filterStr = filterStr.Remove(filterStr.Length-3,3);


        DataView view = new DataView();
        if (condition1)
            view.Table = Production.usp1(txtProdCode.Text);
        else if (condition2)
            view.Table = Production.usp2();
        else
            return;
        view.RowFilter = filterStr;
        dgv.DataSource = view;    
    }

这是与storedProcedure相关的函数(在Production类中):

    public static DataTable usp2()
    {
        SqlCommand cmd = new SqlCommand("usp2");
        return callProc(cmd);
    }

    public static DataTable usp1(String prod_code)
    {
        SqlCommand cmd = new SqlCommand("usp1");
        cmd.Parameters.Add(new SqlParameter("@prod_code", prod_code));
        return callProc(cmd);
    }
4

2 回答 2

2

尝试明确指定名称。DataTable.TableName="Name"在创建数据视图之前使用。
您的 btnFilter_Click 方法应如下所示:

 private void btnFilter_Click(object sender, EventArgs e)
    {
        String filterStr = "";

        filterStr +=      "grp_name " + (cmbGrp.Text.Trim() == String.Empty ? " like '%'" : "='" + cmbGrp.Text.Trim()) + "'";
        filterStr += " and mdl_name " + (cmbMdl.Text.Trim() == String.Empty ? " like '%'" : "='" + cmbMdl.Text.Trim()) + "'";
        filterStr += " and trh_name " + (cmbTrh.Text.Trim() == String.Empty ? " like '%'" : "='" + cmbTrh.Text.Trim()) + "'";
        filterStr += " and siz_name " + (cmbSiz.Text.Trim() == String.Empty ? " like '%'" : "='" + cmbSiz.Text.Trim()) + "'";
        filterStr += " and clr_name " + (cmbClr.Text.Trim() == String.Empty ? " like '%'" : "='" + cmbClr.Text.Trim()) + "'";

        DataView view = new DataView();

        if (condition1)
           {
            var dt = Production.usp1(txtProdCode.Text);
            dt.TableName="usp1";
            view.Table = dt;
            }
        else if (condition2)
            {
            var dt = Production.usp2();
            dt.TableName="usp2";
            view.Table = dt;
            }
        else
            return;
        view.RowFilter = filterStr;
        dgv.DataSource = view;    
    }  

要不再使用 DB 中的值重新加载网格,您可以将现有数据源转换为 DataView,如下所示:

(dataGridViewFields.DataSource as DataTable).DefaultView.RowFilter  

在这个问题中阅读更多内容

于 2012-11-12T21:42:52.993 回答
0

感谢user1792936,您实现的方式是正确的,但是有一种更简单的方法:根据此链接和布拉德布鲁斯的回答

我不需要再次填充网格:

(dgv.DataSource as DataTable).DefaultView.RowFilter = filterStr;
于 2012-11-12T22:46:56.237 回答