首先创建一个类
class ComboBoxItem { public string Text { get; set; } public object Value { get; set; } //这个方法必须要有没有的话ComboBox下拉能正常显示 public override string ToString() { return Text; } }
初始化ComboBox控件 一定要把这个方法
InitializeControls() 放到构造函数里 如
public Filter()
{
InitializeComponent();
InitializeControls();
}
public void InitializeControls() { //CmbAudit 赋值方法 CmbAudit.Items.Add(new ComboBoxItem { Text = "全部", Value = -1 }); CmbAudit.Items.Add(new ComboBoxItem { Text = "未审核", Value = 0 }); CmbAudit.Items.Add(new ComboBoxItem { Text = "已审核", Value = 1 }); CmbAudit.SelectedIndex = 0; //作废赋值 CmbFCancellation.Items.Add(new ComboBoxItem { Text="正常", Value =0 }); CmbFCancellation.Items.Add(new ComboBoxItem { Text="已作废", Value=1 }); CmbFCancellation.Items.Add(new ComboBoxItem { Text = "全部", Value=-1 }); //状态赋值 CmbCloseed.Items.Add(new ComboBoxItem { Text = "关闭", Value=3 }); CmbCloseed.Items.Add(new ComboBoxItem { Text = "未关闭", Value = 1 }); CmbCloseed.Items.Add(new ComboBoxItem { Text = "全部", Value = -1 }); }
/// <summary> /// 获取条件搜索的单据头集合 /// </summary> /// <param name="txt">条件</param> /// <returns>DataTable</returns> public static DataTable Gettable(string FiterId,int auditFlag, int ZuoF,int FCloseed ) { DataTable tb = new DataTable(); List<SqlParameter> parametertemp = new List<SqlParameter>(); //定义泛型集合 string queryText = @"select top 200 v1.FDate as FDateTime, u1.FQty as FCount, t25.FName as Pre, t17.FShortNumber as GuiG, v1.FInterID AS FInterID, u1.FEntryID AS FEntryID, case when v1.FCheckerID>0 then 'Y' when v1.FCheckerID<0 then 'Y' else '' end AS FCheck, v1.Fdate AS Fdate, t4.FName AS FCustIDName, v1.FBillNo AS FBillNo, t11.FName AS FDeptIDName, t12.FName AS FEmpIDName, t17.Fname AS FItemName, u1.Fauxqty AS Fauxqty from SEOutStock v1 INNER JOIN SEOutStockEntry u1 ON v1.FInterID = u1.FInterID AND u1.FInterID <>0 INNER JOIN t_Organization t4 ON v1.FCustID = t4.FItemID AND t4.FItemID <>0 LEFT OUTER JOIN t_Stock t9 ON u1.FStockID = t9.FItemID AND t9.FItemID <>0 LEFT OUTER JOIN t_Department t11 ON v1.FDeptID = t11.FItemID AND t11.FItemID <>0 LEFT OUTER JOIN t_Emp t12 ON v1.FEmpID = t12.FItemID AND t12.FItemID <>0 INNER JOIN t_Currency t15 ON v1.FCurrencyID = t15.FCurrencyID AND t15.FCurrencyID <>0 INNER JOIN t_ICItem t17 ON u1.FItemID = t17.FItemID AND t17.FItemID <>0 INNER JOIN t_MeasureUnit t20 ON u1.FUnitID = t20.FItemID AND t20.FItemID <>0 LEFT OUTER JOIN t_MeasureUnit t25 ON t17.FUnitID = t25.FMeasureUnitID AND t25.FMeasureUnitID <>0 LEFT OUTER JOIN t_MeasureUnit t500 ON t17.FSaleUnitID = t500.FItemID AND t500.FItemID <>0 LEFT OUTER JOIN t_Currency t504 ON v1.FCurrencyID = t504.FCurrencyID AND t504.FCurrencyID <>0 LEFT OUTER JOIN t_MeasureUnit t506 ON t17.FSecUnitID = t506.FItemID AND t506.FItemID <>0 where 1=1 "; //单号和供应商 if (!string.IsNullOrEmpty(FiterId)) { queryText += " and v1.FBillNo like '%'+@FiterId or t4.FName like '%'+@FiterId+'%'"; parametertemp.Add(new SqlParameter("@FiterId", FiterId)); } //全部 -1 //审核 1,未审核 0 if(auditFlag>=0) { queryText += " and v1.FStatus = @auditFlag"; parametertemp.Add(new SqlParameter("@auditFlag", auditFlag)); } //全部-1 //作废1,未作废0 if (ZuoF>=0) { queryText += " and v1.FCancellation = @Zuofei"; parametertemp.Add(new SqlParameter("@Zuofei", ZuoF)); } if (FCloseed>=0) { queryText += " and v1.FStatus = @FCloseed"; parametertemp.Add(new SqlParameter("@FCloseed", FCloseed)); }
//时间
if (FTime==1)
{
queryText += " and v1.FDate = CONVERT(varchar(10), GETDATE(), 23) ";
}
if (FTime==2)
{
queryText += " and datediff(week,v1.FDate,getdate())=0 ";
}
if (FTime==3)
{
queryText += " and datediff(month,v1.FDate,getdate())=0 ";
}
SqlParameter[] parameters = parametertemp.ToArray(); tb = ExecuteDBQueryWithParameters(queryText, parameters); return tb; }
/// <summary> /// 数据库查询动态参数配制方法 /// </summary> /// <param name="query">带参数查询语句</param> /// <param name="parameters">动态参数列表</param> /// <returns></returns> private static DataTable ExecuteDBQueryWithParameters(string query,params SqlParameter[] parameters) { DataTable dt = new DataTable(); string constr = ConfigurationManager.ConnectionStrings["constr"].ToString(); using (SqlConnection Connection = new SqlConnection(constr)) { SqlCommand SqlCmd = new SqlCommand(query, Connection); //params 的意思就是允许传空参数组 foreach (SqlParameter item in parameters) { SqlCmd.Parameters.Add(item); } SqlDataAdapter dpt = new SqlDataAdapter(SqlCmd); dpt.Fill(dt); } return dt; }
private button1_click ( objet sender button_click EverAges e) { this.gridcontrol.datasource=sqlhelp.getdata(gys.Text,(int)((comboitems)cmbcheck.selecteditems).value,后续参数不写了) }
一般类型,都是继承自System.Object类,默认情况下,object类的ToString方法会返回当前类的类型的字符串表达形式。但也有例外!! DateTime,它就重写ToString方法,DateTime类型的实例的ToString方法返回的就是一个日期的字符串表达形式,而不是DateTime类型的字符串表达形式。 还有一种使用的情况就是,当我们有了一个Student类时,我们使用Console.WriteLine(student),将会输出 "命名空间.Student"。这是我们不想要的,我们想要输出时显示这个学生的名字,那么我们就需要重写ToString方法。 public override string ToString() { reutrn "张三";