I have a datagridview and 5 ComboBoxes and I am trying to filter the grid’s rows with ComboBox’s text values. The grid is filled with stored procedure depends on some conditions in FormLoad event. Should I fill it again in the btn_filter click event?
Anyway this exception is catched at code below: cannot bind to datatable with no name.
here is the code for 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;
}
and here is the storedProcedure related functions (in Production class):
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);
}
Try specify Name explicitly. use
DataTable.TableName="Name"before you create your dataview.You btnFilter_Click method should look like this:
To not reload grid with values from DB again you can cast your existing datasource to DataView like this:
Read more in this question