Answered by:
filtering in binding source

Question
-
dear sir,
in my project, there are 4 types of users.. namely
1. Admin
2. Stockist
3. Sub Stockist
4. Agent
i place a single sales report for all of them.. but there are many types of filtering( search by name or SalesID or BillNo etc ).. can anyone suggest me simple way to achieve this. i will place my code here
private void bindgrid() { if (Class2.Role == "Stockist") { dsLotteryApplicationdataset.dtSalesByUsersRoleDataTable role = new LotteryApplication.dsLotteryApplicationdataset.dtSalesByUsersRoleDataTable(); dsLotteryApplicationdatasetTableAdapters.dtSalesByUsersRoleTableAdapter adaptr = new dsLotteryApplicationdatasetTableAdapters.dtSalesByUsersRoleTableAdapter(); adaptr.Fill(role, Class2.Name); dv = new DataView(role); bs1.DataSource = dv; dataGridView1.DataSource = bs1; calculatedatagrid(); } else { dsLotteryApplicationdataset.dtSalesDataTable sales = new LotteryApplication.dsLotteryApplicationdataset.dtSalesDataTable(); dsLotteryApplicationdatasetTableAdapters.dtSalesTableAdapter ada = new dsLotteryApplicationdatasetTableAdapters.dtSalesTableAdapter(); DateTime netdate = NetTime.dt(); if (f == true || t == true) { frmdate = Convert.ToDateTime(frmdate.ToString("dd/MM/yyyy")); todate = Convert.ToDateTime(todate.ToString("dd/MM/yyyy")); } netdate = Convert.ToDateTime(netdate.ToString("dd/MM/yyyy")); if (chkconsolidated.Checked != true) { ada.FillbySales(sales, netdate); dv = new DataView(sales); bs.DataSource = dv; dataGridView1.Columns[2].Visible = true; dataGridView1.Columns[6].Visible = true; dataGridView1.Columns[7].Visible = true; dataGridView1.Columns[10].Visible = true; dataGridView1[6, dataGridView1.Rows.Count - 1].Value = ""; dataGridView1[8, dataGridView1.Rows.Count - 1].Value = ""; dataGridView1[9, dataGridView1.Rows.Count - 1].Value = ""; if (txtcustmrname.Text != "" && txtlottery.Text != "" && cmbgrp.Text != "" && cmbusers.Text != "" && txtbillno.Text != "" && f == true && t == true) { bs.Filter = "UserName LIKE '%" + txtcustmrname.Text + "%' AND TicketName LIKE '%" + txtlottery.Text + "%' AND BillNo =" + txtbillno.Text + " AND TicketGroup LIKE '%" + grp + "%' AND Role LIKE '%" + cmb + "%' AND SalesDate >=#" + frmdate + "# AND SalesDate <= #" + todate + "#"; dataGridView1.DataSource = bs; calculatedatagrid(); } else if (f == true || t == true) { if (t != true) { ada.FillbySales(sales, frmdate); dv = new DataView(sales); bs.DataSource = dv; // bs.Filter = "SalesDate >=#" + frmdate + "#"; } //this.dtSalesTableAdapter.FillbySales(this.dsLotteryApplicationdataset.dtSales, frmdate); else bs.Filter = "SalesDate >=#" + frmdate.Date + "# AND SalesDate <= #" + todate.Date + "#"; dataGridView1.DataSource = bs; calculatedatagrid(); f = false; t = false; } else if (txtcustmrname.Text != "") { bs.Filter = "UserName LIKE '%" + txtcustmrname.Text + "%'"; dataGridView1.DataSource = bs; calculatedatagrid(); us = 1; txtcustmrname.Text = ""; } else if (c == true) { // this.dtSalesTableAdapter.FillByRole(this.dsLotteryApplicationdataset.dtSales, date.Date, cmb); bs.Filter = "Role LIKE '%" + cmb + "%'"; dataGridView1.DataSource = bs; calculatedatagrid(); c = false; } else if (txtbillno.Text != "") { //this.dtSalesTableAdapter.FillByBillNo(this.dsLotteryApplicationdataset.dtSales, Convert.ToInt32(txtbillno.Text),date.Date); bs.Filter = "BillNo =" + txtbillno.Text + ""; dataGridView1.DataSource = bs; calculatedatagrid(); txtbillno.Text = ""; } else if (txtlottery.Text != "") { //this.dtSalesTableAdapter.FillBylotteryname(this.dsLotteryApplicationdataset.dtSales, txtlottery.Text,date.Date); bs.Filter = "TicketName LIKE '%" + txtlottery.Text + "%'"; dataGridView1.DataSource = bs; calculatedatagrid(); lott = 1; txtlottery.Text = ""; } else if (grp != "") { //this.dtSalesTableAdapter.FillByGrp(this.dsLotteryApplicationdataset.dtSales, grp,date.Date); bs.Filter = "TicketGroup LIKE '%" + grp + "%'"; dataGridView1.DataSource = bs; calculatedatagrid(); } else if (cmbgrp.SelectedItem == "All" || cmbusers.SelectedItem == "") { this.dtSalesTableAdapter.FillbySales(this.dsLotteryApplicationdataset.dtSales, date.Date); dataGridView1.DataSource = bs; calculatedatagrid(); cmbgrp.Text = ""; } else { this.dtSalesTableAdapter.FillbySales(this.dsLotteryApplicationdataset.dtSales, date.Date); calculatedatagrid(); } } else if (chkconsolidated.Checked == true) { ada.FillByTotalSales(sales, netdate); dv = new DataView(sales); bs.DataSource = dv; if (txtcustmrname.Text == "" && txtbillno.Text == "" && txtlottery.Text == "" && cmb == null && grp == null) { this.dtSalesTableAdapter.FillByTotalSales(this.dsLotteryApplicationdataset.dtSales, date.Date); } else if (txtcustmrname.Text != "" && txtbillno.Text != "" && txtlottery.Text != "" && cmb != null && grp != "") { if (f == true || t == true) { bs.Filter = "UserName LIKE '%" + txtcustmrname.Text + "%' AND BillNo = " + txtbillno.Text + " AND TicketName LIKE '%" + txtlottery.Text + "%' AND Role LIKE '%" + cmb + "%' AND SalesDate >=#" + frmdate + "# AND SalesDate <= #" + todate + "# AND TicketGroup LIKE '%" + grp + "%'"; dataGridView1.DataSource = bs1; } } else if (txtcustmrname.Text != "" && txtbillno.Text != "" && txtlottery.Text != "" && cmb != null) { if (f == true || t == true) { bs.Filter = "UserName LIKE '%" + txtcustmrname.Text + "%' AND BillNo =" + txtbillno.Text + " AND TicketName LIKE '%" + txtlottery.Text + "%' AND Role LIKE '%" + cmb + "%' AND SalesDate >=#" + frmdate + "# AND SalesDate <= #" + todate + "#"; dataGridView1.DataSource = bs1; } } else if (txtcustmrname.Text != "" && txtbillno.Text != "" && txtlottery.Text != "") { if (f == true || t == true) { bs.Filter = "UserName LIKE '%" + txtcustmrname.Text + "%' AND BillNo =" + txtbillno.Text + " AND TicketName LIKE '%" + txtlottery.Text + "%' AND SalesDate >=#" + frmdate + "# AND SalesDate <= #" + todate + "#"; dataGridView1.DataSource = bs1; } } else if (txtcustmrname.Text != "" && txtbillno.Text != "") { if (f == true || t == true) { ada.FillByTotalSales(sales, frmdate); dv = new DataView(sales); bs.DataSource = dv; bs.Filter = "UserName LIKE '%" + txtcustmrname.Text + "%' AND BillNo = " + txtbillno.Text + " AND SalesDate >=#" + frmdate + "# AND SalesDate <= #" + todate + "#"; dataGridView1.DataSource = bs; } else { bs.Filter = "UserName LIKE '%" + txtcustmrname.Text + "%' AND BillNo = " + txtbillno.Text + ""; dataGridView1.DataSource = bs; } } else if (txtcustmrname.Text != "") { if (f == true || t == true) { ada.FillByTotalSales(sales, frmdate); dv = new DataView(sales); bs.DataSource = dv; bs.Filter = "UserName LIKE '%" + txtcustmrname.Text + "%' AND SalesDate >=#" + frmdate + "# AND SalesDate <= #" + todate + "#"; dataGridView1.DataSource = bs; } else { bs.Filter = "UserName LIKE '%" + txtcustmrname.Text + "%'"; dataGridView1.DataSource = bs; } } else if (f == true || t == true) { ada.FillByTotalSales(sales, frmdate); dv = new DataView(sales); bs.DataSource = dv; if (f == true) { bs.Filter = "SalesDate >=#" + frmdate + "#"; } else if (f == true && t == true) { bs.Filter = "SalesDate >=#" + frmdate + "# AND SalesDate <= #" + todate + "#"; } dataGridView1.DataSource = bs; } else if (txtbillno.Text != "") { bs.Filter = "BillNo =" + txtbillno.Text + ""; dataGridView1.DataSource = bs; } else if (txtlottery.Text != "") { bs.Filter = "TicketName LIKE '%" + txtlottery.Text + "%'"; dataGridView1.DataSource = bs; } else if (c == true) { bs.Filter = "Role LIKE '%" + cmb + "%'"; dataGridView1.DataSource = bs; cmb = ""; } else if (grp != "") { bs.Filter = "TicketGroup LIKE '%" + grp + "%'"; dataGridView1.DataSource = bs; grp = ""; } else if (txtcustmrname.Text != "" && cmbusers.Text != "" && txtbillno.Text != "" && f == true && t == true) { bs.Filter = "UserName LIKE '%" + txtcustmrname.Text + "%' AND BillNo =" + txtbillno.Text + " AND Role LIKE '%" + cmb + "%'"; dataGridView1.DataSource = bs; } else if (f == true || t == true) this.dtSalesTableAdapter.FillByTotalSales(this.dsLotteryApplicationdataset.dtSales, frmdate); else this.dtSalesTableAdapter.FillByTotalSales(this.dsLotteryApplicationdataset.dtSales, netdate); dataGridView1.Columns[2].Visible = false; dataGridView1.Columns[6].Visible = false; dataGridView1.Columns[7].Visible = false; dataGridView1.Columns[10].Visible = false; calculateconsolidated(); } else { MessageBox.Show("No Records Found!!!", "Lottery Application", MessageBoxButtons.OK, MessageBoxIcon.Information); } } }
Mousmi Nishad Working has a Junior Programmer in C#.NET
Monday, May 9, 2016 7:20 AM
Answers
-
Hi Mousmi,
>>"can anyone suggest me simple way to achieve this. i will place my code here"
We do not recommend that you put all the code into one method. Since you have 4 types of uses, I suggest you create 4 methods to get data source for your DataGridView.
static DataTable GetDataSourceForAdmin() { } static DataTable GetDataSourceForStockist() { }
....What you should do is make your code more simple and easy to understand.
Best Regards,
Li Wang
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.- Edited by DotNet Wang Tuesday, May 10, 2016 6:42 AM
- Proposed as answer by Moonlight ShengMicrosoft contingent staff Thursday, May 19, 2016 9:18 AM
- Marked as answer by Moonlight ShengMicrosoft contingent staff Friday, May 20, 2016 8:53 AM
Tuesday, May 10, 2016 6:42 AM