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