0

I'm bit confuse with my code. I don't know what is the problem. I search on the internet about this and the answer said I need to FILTER the DataGridView and show the exact values from database. But nothing work if I put the code and change from DataSet, DataAdapter. How to filter the values?

Here my code.

Form 1, Code 1, DataGridView 1.

 public void DataView()
    {
        Connection.Open();
        MySqlCommand Command = new MySqlCommand("Select * From clientaccountcreditdebitdb", Connection);
        DataTable dt = new DataTable();
        MySqlDataAdapter da = new MySqlDataAdapter(Command);
        da.Fill(dt);
        dgvCreditList.DataSource = null;
        dgvCreditList.AllowUserToAddRows = false;
        dgvCreditList.AutoGenerateColumns = false;
        dgvCreditList.ColumnCount = 9;
        dgvCreditList.Columns[0].HeaderText = "id";
        dgvCreditList.Columns[0].DataPropertyName = "ID";
        dgvCreditList.Columns[1].HeaderText = "Account Name";
        dgvCreditList.Columns[1].DataPropertyName = "CreditAccountName";
        dgvCreditList.Columns[2].HeaderText = "Account Number";
        dgvCreditList.Columns[2].DataPropertyName = "CreditAccountNumber";
        dgvCreditList.Columns[3].HeaderText = "Description";
        dgvCreditList.Columns[3].DataPropertyName = "CreditDescription";
        dgvCreditList.Columns[4].HeaderText = "References / Cheque No";
        dgvCreditList.Columns[4].DataPropertyName = "CreditReferencesCheque";
        dgvCreditList.Columns[5].HeaderText = "Bank Name";
        dgvCreditList.Columns[5].DataPropertyName = "CreditBankName";
        dgvCreditList.Columns[6].HeaderText = "Date";
        dgvCreditList.Columns[6].DataPropertyName = "CreditDate";
        dgvCreditList.Columns[7].HeaderText = "Transaction Type";
        dgvCreditList.Columns[7].DataPropertyName = "CreditTransactionType";
        dgvCreditList.Columns[8].HeaderText = "Amount";
        dgvCreditList.Columns[8].DataPropertyName = "CreditAmount";
        dgvCreditList.Columns[0].Visible = false;
        Connection.Close();
    }

Form 2, Code 2, DataGrid 2.

public void DataView()
    {
        Connection.Open();
        MySqlCommand Command = new MySqlCommand("Select * From clientaccountcreditdebitdb", Connection);
        DataTable dtdebit = new DataTable();
        MySqlDataAdapter da = new MySqlDataAdapter(Command);
        da.Fill(dtdebit);
        dgvDebitList.DataSource = null;
        dgvDebitList.AllowUserToAddRows = false;
        dgvDebitList.AutoGenerateColumns = false;
        dgvDebitList.ColumnCount = 9;
        dgvDebitList.Columns[0].HeaderText = "id";
        dgvDebitList.Columns[0].DataPropertyName = "ID";
        dgvDebitList.Columns[1].HeaderText = "Account Name";
        dgvDebitList.Columns[1].DataPropertyName = "DebitAccountName";
        dgvDebitList.Columns[2].HeaderText = "Account Number";
        dgvDebitList.Columns[2].DataPropertyName = "DebitAccountNumber";
        dgvDebitList.Columns[3].HeaderText = "Description";
        dgvDebitList.Columns[3].DataPropertyName = "DebitDescription";
        dgvDebitList.Columns[4].HeaderText = "References / Cheque No";
        dgvDebitList.Columns[4].DataPropertyName = "DebitReferencesCheque";
        dgvDebitList.Columns[5].HeaderText = "Bank Name";
        dgvDebitList.Columns[5].DataPropertyName = "DebitBankName";
        dgvDebitList.Columns[6].HeaderText = "Date";
        dgvDebitList.Columns[6].DataPropertyName = "DebitDate";
        dgvDebitList.Columns[7].HeaderText = "Transaction Type";
        dgvDebitList.Columns[7].DataPropertyName = "DebitTransactionType";
        dgvDebitList.Columns[8].HeaderText = "Amount";
        dgvDebitList.Columns[8].DataPropertyName = "DebitAmount";
        dgvDebitList.Columns[0].Visible = false;
        dgvDebitList.DataSource = dtdebit;
        Connection.Close();
    }

And here my form.

Is there something wrong here?

  • 3
    Do not bind to the DataTable directly but insert e.g. a DataView to each of the DGVs.. DataView has filter and also sorting capabilties. – TaW May 02 '18 at 20:14
  • Do you mean, drag and drop the DGV and call the Data from the form? Like, using Object? – Thoiruddin May 03 '18 at 01:50
  • Huh? No I guess not. I mean, replace `dgvDebitList.DataSource = dtdebit;` by `DataView dvdebit = new DataView(dtdebit,someFilterString, "", DataViewRowState.Added);dgvDebitList.DataSource =dvdebit ;` etc.. – TaW May 03 '18 at 02:37
  • Hi, I trying to use your given code and check references for DataView and DataViewRowState but all not showing the result I need. Can you share some code with us here? – Thoiruddin May 03 '18 at 07:33
  • Well, not sure which part is the issue. Try to create the DataViews by the simpler constructor: `DataView dvdebit = new DataView(dtdebit);..` - Now the rows should all show up. Do they? Next create a RowFilter and assign it; does it work? As expected? Yes, done; no:show the filter string. The correct RowStateFilter can be tricky, depending on the source of the data.. Default is `CurrentRows` - Also: I just noticed that on code 1 you never assign a DataSource to dgvCreditList. – TaW May 03 '18 at 07:40
  • I may not have gotten the whole issue; upon rereading and looking at the image I wonder: Do you have 2 sources and want to filter them and merge the result into a 3rd dgv? – TaW May 03 '18 at 08:33
  • Sorry if I trouble you so much. Thanks for the hint about the dgvCreditList. I didn't plan to show the result into 3rd dgv, what I need is right now, this is the last part of my project that I can't solve, – Thoiruddin May 03 '18 at 08:33
  • No problem, I have fun solving problems. So: How many Tables do you have? 2? And how many DGVs do you want to show? 1? 2? Also: In the image: Do the lines point to different cells or to different rows? – TaW May 03 '18 at 08:35
  • Sorry if I trouble you so much. I only have 1 database which contains Credit and Debit tables and want to filter Credit just for Credit form, Debit just for Debit Form. But as you see the screenshot before, that the result from Credit Form and the Debit values appear there and I want to filter them. Here is my screenshot. Credit Form [link]https://imgur.com/4nryQMo Debit Form [link]https://imgur.com/UGXx01H Database [link]https://imgur.com/1UsfZB9 Code (Credit and Debit Form using Same code but different DGV) [link]https://pastebin.com/SvCbanUf – Thoiruddin May 03 '18 at 08:56
  • OK. So do you need any filtering? If so a DataView is what you want. But DataViews can't be merged directly. So you need to create a mergeTable like the anser [here](https://stackoverflow.com/questions/7614150/how-to-merge-multiple-dataviews-into-one/28815653#28815653) shows (Yan's answer). This should then be the DataSource for the DGV. But: after copying the filtered rows into a new table I doubt you will be able to write any changes back directly.. – TaW May 03 '18 at 09:21
  • I will test it at home tonight. I will update you as soon as possible. – Thoiruddin May 03 '18 at 10:14
  • Hi. I test the code with DataView, merge table. But I still can't hide or remove the blank rows because the rows contains data from other form. Can you check the the screenshot and my code? [link]https://imgur.com/a/NQ0sMKa and my code for this form only. [link]https://pastebin.com/FRmuX8Ge And here is my Database Table for this Form. [link]https://imgur.com/a/puq4j4P – Thoiruddin May 16 '18 at 04:33

0 Answers0