一尘不染

过滤网格视图时无法绑定到没有名称错误的数据表

sql

我有一个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;    
    }

这是与storageProcedure相关的函数(在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);
    }

阅读 124

收藏
2021-05-16

共1个答案

一尘不染

尝试显式指定名称。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;    
    }

要不再使用数据库中的值重新加载网格,可以将现有的数据源像这样转换为DataView:

(dataGridViewFields.DataSource as DataTable).DefaultView.RowFilter
2021-05-16