我有一个包含四个文本框和两个组合框的表单......
我正在过滤数据并在datagrid视图中显示数据取决于组合框中的选择和文本框中键入的文本..
因为我写了下面的代码
private void btnRunreports_Click(object sender, EventArgs e)
{
int agefrom = Convert.ToInt32(cbGEFrom.Text);
int ageto = Convert.ToInt32(cbGETo.Text);
DateTime today = DateTime.Today;
DateTime max = today.AddYears(-(agefrom + 1));
DateTime min = today.AddYears(-(ageto));
string maximum = Convert.ToString(max);
string minimum = Convert.ToString(min);
string gender = "";
gender = Classes.reportmembers.ConvertGender(cbGEGendertype.Text);
var mems = Classes.reportmembers
.getallreportmembers(gender,
cbGEMembershiptype.SelectedText,
txtlastname.Text,
txtpostcode.Text,
txtcardnum.Text,
txtreference.Text,
cbGEStatustype.SelectedText,
maximum, minimum);
BindingSource bs = new BindingSource();
bs.DataSource = mems;
dgvReportMembers.DataSource = bs;
}
这是我的班级reportmembers
:
class ReportMebers
{
public int MemberID { get; set; }
public string Lastname { get; set; }
public string Firstname { get; set; }
public string Postcode { get; set; }
public string Reference { get; set; }
public string CardNum { get; set; }
public string IsBiometric { get; set; }
public string DOB { get; set; }
public string MShipType { get; set; }
public string StatusType { get; set; }
public string EndDate { get; set; }
}
class reportmembers
{
public static List<ReportMebers> getallreportmembers(string gender, string membershiptype, string lastname,
string postcode,string cardnum,string refernce,
string membershipstatustypesa, string maxage, string minage)
{
//CultureInfo provider = CultureInfo.InvariantCulture;
EclipseEntities eclipse = new EclipseEntities();
List<ReportMebers> reporall = new List<ReportMebers>();
var memberreport = from report in eclipse.members
join memtomship in eclipse.membertomships on report.member_Id equals memtomship.member_Id
join mshoption in eclipse.mshipoptions on memtomship.mshipOption_Id equals mshoption.mshipOption_Id
join membershiptypes in eclipse.mshiptypes on mshoption.mshipType_Id equals membershiptypes.mshipType_Id
join membershipstatustypes in eclipse.mshipstatustypes on memtomship.mshipStatusType_Id equals membershipstatustypes.mshipStatusType_Id
where report.member_Lastname.Equals(lastname)
&& report.member_CardNum.Equals(cardnum)
&& report.member_Postcode.Equals(postcode)
&& report.member_Reference.Equals(refernce)
&& report.member_Gender.Equals(gender)
&& membershiptypes.mshipType_Name.Equals(membershiptype)
&& membershipstatustypes.mshipStatusType_Name.Equals(membershipstatustypesa)
&& string.Compare(report.member_Dob,maxage) >= 0
&& string.Compare(report.member_Dob, minage)< 0
select new
{
report.member_Id,
report.member_Lastname,
report.member_Firstname,
report.member_Postcode,
report.member_Reference,
report.member_CardNum,
report.member_IsBiometric,
report.member_Dob,
membershiptypes.mshipType_Name,
membershipstatustypes.mshipStatusType_Name,
memtomship.memberToMship_EndDate
};
try
{
foreach (var membe in memberreport)
{
ReportMebers allmembersrepor = new ReportMebers();
allmembersrepor.MemberID = membe.member_Id;
allmembersrepor.Lastname = membe.member_Lastname;
allmembersrepor.Firstname = membe.member_Firstname;
allmembersrepor.Postcode = membe.member_Postcode;
allmembersrepor.Reference = membe.member_Reference;
allmembersrepor.CardNum = membe.member_CardNum;
allmembersrepor.IsBiometric = membe.member_IsBiometric;
allmembersrepor.DOB = membe.member_Dob;
allmembersrepor.MShipType = membe.mshipType_Name;
allmembersrepor.StatusType = membe.mshipStatusType_Name;
allmembersrepor.EndDate = membe.memberToMship_EndDate;
reporall.Add(allmembersrepor);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return reporall;
}
如果我在txtlastname中输入robin,将显示姓氏为robin的详细信息......
我在数据库中检查过有姓罗宾的人.. 但它没有显示在数据网格视图中......
任何人都会帮忙解决这个问题......
非常感谢提前......
答案 0 :(得分:0)
您的问题是,您正在对所有字段进行AND比较。这意味着,只返回数据库中的条目,匹配所有输入的数据!如果您只输入知识作为姓氏而没有其他内容,则不会得到任何结果,因为所有其他字段都不匹配。更改您的查询以仅包括那些非空的字段。像这样:
var query = from report in eclipse.members
join memtomship in eclipse.membertomships on report.member_Id equals memtomship.member_Id
join mshoption in eclipse.mshipoptions on memtomship.mshipOption_Id equals mshoption.mshipOption_Id
join membershiptypes in eclipse.mshiptypes on mshoption.mshipType_Id equals membershiptypes.mshipType_Id
join membershipstatustypes in eclipse.mshipstatustypes on memtomship.mshipStatusType_Id equals membershipstatustypes.mshipStatusType_Id;
if(!string.IsNullOrEmpty(lastname))
query = query.Where(r => r.member_Lastname == lastname);
if(!string.IsNullOrEmptry(cardnum)
query = query.Where(r => r.member_CardNum == cardnum);
// and so on for all parameters