Click here to Skip to main content
16,018,973 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am developing one simple invoice application in which I am having a simple Customer screen module.A Customer can have any number of GST records in the Datagridview.The Screen is based on Customer Masters(Textboxes) and Customers Master Details(Datagridview) relationship through foreign keys. And I am Using Strongly Typed Dataset in this project as a xsd file.This Screen has 3 buttons i.e.New,Save and Find toolstrip menu.

When I start adding records after clicking on New Buttonstrip and and save ,it saves it successfully and when I press New Ideally it should clear the rows.The main issue here is that it is not clearing the rows??
I am enclosing the cs file here.

C#
using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;

    namespace GST.Masters
    {
    public partial class Customers : Heptanesia.Winforms.Ui.Forms.MainUC
    {

    public static string recCode
    { get; set; }
    public string RC;
    public bool insertFlag;
    public Data.GSTDb.CustomersRow r;
    public Data.GSTDb.CustomersGSTNosRow gr;
    public Data.GSTDb.CustomersDataTable dt = new Data.GSTDb.CustomersDataTable();

    public Customers()
    {
    InitializeComponent();
    recCode = Guid.NewGuid().ToString();
    this.Load += new EventHandler(this.Customers_Load);
    this.Ts.SaveClicked += new Heptanesia.Winforms.Ui.Menu.ToolStrip.SaveClickedEventHandler(this.Ts_SaveClicked);
    this.Ts.AddNewClicked += new Heptanesia.Winforms.Ui.Menu.ToolStrip.AddNewClickedEventHandler(this.Ts_AddNewClicked);
    this.Ts.SearchClicked += new Heptanesia.Winforms.Ui.Menu.ToolStrip.SearchClickedEventHandler(this.Ts_SearchClicked);
    this.DgGST.CellValidated += (sender, e) =>
    {
    string name = this.DgGST.Columns[e.ColumnIndex].Name;
    if (name == "DGCCountryCode")
    {
    name = this.DgGST.Rows[e.RowIndex].Cells[name].Value.ToString();
    this.statesBindingSource.Filter = "ParentCode = '" + name + "'";
    if (!this.FormIsLoading)
    {
    this.DgGST.Rows[e.RowIndex].Cells["DGCStateCode"].Value = null;
    this.DgGST.Rows[e.RowIndex].Cells["DGCCityCode"].Value = null;
    }
    }
    else if (name == "DGCStateCode")
    {
    name = this.DgGST.Rows[e.RowIndex].Cells[name].Value.ToString();
    this.citiesBindingSource.Filter = "ParentCode = '" + name + "'";
    if (!this.FormIsLoading)
    {
    this.DgGST.Rows[e.RowIndex].Cells["DGCCityCode"].Value = null;
    }
    }
    };
    }

    private void Ts_AddNewClicked(object sender, EventArgs e)
    {
    this.gSTDb.Customers.Rows.Clear();
    r = this.gSTDb.Customers.NewCustomersRow();
    this.gSTDb.Customers.AddCustomersRow(r);
    Data.GSTDbTableAdapters.CustomersGSTNosTableAdapter CGT = new Data.GSTDbTableAdapters.CustomersGSTNosTableAdapter();
    CGT.FillByParentCode(gSTDb.CustomersGSTNos, r.RecCode);
    }

    private void Ts_SearchClicked(object sender, EventArgs e)
    {
    Heptanesia.Winforms.Ui.Forms.SearchForm sf = new Heptanesia.Winforms.Ui.Forms.SearchForm();
    sf.DataSource = new Data.GSTDbTableAdapters.CustomersTableAdapter().GetData();
    sf.DisplayColumns = new string[] { "Name" };
    sf.DisplayHeaders = new string[] { "Customers Name" };
    sf.DisplayWidths = new int[] { 200 };
    if (sf.ShowDialog(this) == DialogResult.OK)
    {
    this.RC = sf.ReturnRow["RecCode"].ToString();
    this.customersTableAdapter.FillByRecCode(this.gSTDb.Customers, RC);
    }
    }

    private void Ts_SaveClicked(object sender, EventArgs e)
    {
    string message = "Error: ";
    try
    {
    this.customersBindingSource.EndEdit();
    //if (this.DgGST.CurrentRow.DataBoundItem != null)
    this.fkCustomerGSTNosCustomersBindingSource.EndEdit();
    if (this.gSTDb.Customers.HasErrors)
    message += Classes.Global.SetBindingSourcePositionAndGetError(this.customersBindingSource, this.gSTDb.Customers.GetErrors()[0]);
    else if (this.gSTDb.CustomersGSTNos.HasErrors)
    message += Classes.Global.SetBindingSourcePositionAndGetError(this.fkCustomerGSTNosCustomersBindingSource, this.gSTDb.CustomersGSTNos.GetErrors()[0]);
    else
    {
    this.gSTDb.Customers.BeforeSave();
    this.gSTDb.CustomersGSTNos.BeforeSave();
    Data.GSTDbTableAdapters.TableAdapterManager tm = new Data.GSTDbTableAdapters.TableAdapterManager();
    tm.CustomersTableAdapter = this.customersTableAdapter;
    tm.CustomersGSTNosTableAdapter = this.customersGSTNosTableAdapter;
    tm.UpdateAll(this.gSTDb);
    message = "Record(s) Saved Successfully";
    }
    }
    catch (Exception ex)
    {
    message += ex.Message;
    }
    MessageBox.Show(message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }


    private void Customers_Load(object sender, EventArgs e)
    {
    this.FormIsLoading = true;
    this.customersTableAdapter.Fill(this.gSTDb.Customers);
    this.customersGSTNosTableAdapter.Fill(this.gSTDb.CustomersGSTNos);
    this.countriesTableAdapter.Fill(this.gSTDb.Countries);
    this.statesTableAdapter.Fill(this.gSTDb.States);
    this.citiesTableAdapter.Fill(this.gSTDb.Cities);
    this.gSTDb.Customers.Rows.Clear();
    if (this.gSTDb.Customers.Rows.Count == 0)
    {
    Data.GSTDb.CustomersRow r = this.gSTDb.Customers.NewCustomersRow();
    this.gSTDb.Customers.AddCustomersRow(r);
    }
    this.FormIsLoading = false;
    }


    private void DgGST_DataError(object sender, DataGridViewDataErrorEventArgs e)
    {
    if (e.Exception is ArgumentException)
    {
    object value = DgGST.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
    if (!((DataGridViewComboBoxColumn)DgGST.Columns[e.ColumnIndex]).Items.Contains(value))
    {
    ((DataGridViewComboBoxCell)DgGST[e.ColumnIndex, e.RowIndex]).Value = DBNull.Value;
    //((DataGridViewComboBoxColumn)dgv.Columns[e.ColumnIndex]).Items.Add(value);
    e.ThrowException = false;
    }
    }
    else
    {
    MessageBox.Show(e.Exception.Message);
    e.Cancel = true;
    }
    }
    }
    }


What I have tried:

Everything as I can Do.
1)Clearing the Rows through Rows.Clear();
2)I tried clearing Binding Source and DataGridview after setting it null.
Nothing Worked.
Posted
Updated 23-Oct-17 3:25am
v4
Comments
Richard MacCutchan 23-Oct-17 8:59am    
Please indent your code properly so it is readable. Also explain exactly where the error is occurring, and what you mean by "Nothing Worked".
Member 12562978 23-Oct-17 9:15am    
Richard. Actually its not giving any error but my concern is on clicking on New button i.e.Ts_AddNewClicked method it should clear the datagridview rows after adding new records which is not happening here. Thats my worry.
Karthik_Mahalingam 23-Oct-17 23:06pm    
use  Reply  button, to post Comments/query to the user, so that the user gets notified and responds to your text.
Member 12562978 23-Oct-17 23:17pm    
Yes.Can you help??
Karthik_Mahalingam 23-Oct-17 23:20pm    
try
dataGridView1.Rows.Clear();

1 solution

Learn to indent properly your code, it show its structure and it helps reading and understanding.
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace GST.Masters
{
    public partial class Customers : Heptanesia.Winforms.Ui.Forms.MainUC
    {

        public static string recCode
        { get; set; }
        public string RC;
        public bool insertFlag;
        public Data.GSTDb.CustomersRow r;
        public Data.GSTDb.CustomersGSTNosRow gr;
        public Data.GSTDb.CustomersDataTable dt = new Data.GSTDb.CustomersDataTable();

        public Customers()
        {
            InitializeComponent();
            recCode = Guid.NewGuid().ToString();
            this.Load += new EventHandler(this.Customers_Load);
            this.Ts.SaveClicked += new Heptanesia.Winforms.Ui.Menu.ToolStrip.SaveClickedEventHandler(this.Ts_SaveClicked);
            this.Ts.AddNewClicked += new Heptanesia.Winforms.Ui.Menu.ToolStrip.AddNewClickedEventHandler(this.Ts_AddNewClicked);
            this.Ts.SearchClicked += new Heptanesia.Winforms.Ui.Menu.ToolStrip.SearchClickedEventHandler(this.Ts_SearchClicked);
            this.DgGST.CellValidated += (sender, e) =>
            {
                string name = this.DgGST.Columns[e.ColumnIndex].Name;
                if (name == "DGCCountryCode")
                {
                    name = this.DgGST.Rows[e.RowIndex].Cells[name].Value.ToString();
                    this.statesBindingSource.Filter = "ParentCode = '" + name + "'";
                    if (!this.FormIsLoading)
                    {
                        this.DgGST.Rows[e.RowIndex].Cells["DGCStateCode"].Value = null;
                        this.DgGST.Rows[e.RowIndex].Cells["DGCCityCode"].Value = null;
                    }
                }
                else if (name == "DGCStateCode")
                {
                    name = this.DgGST.Rows[e.RowIndex].Cells[name].Value.ToString();
                    this.citiesBindingSource.Filter = "ParentCode = '" + name + "'";
                    if (!this.FormIsLoading)
                    {
                        this.DgGST.Rows[e.RowIndex].Cells["DGCCityCode"].Value = null;
                    }
                }
            };
        }

        private void Ts_AddNewClicked(object sender, EventArgs e)
        {
            this.gSTDb.Customers.Rows.Clear();
            r = this.gSTDb.Customers.NewCustomersRow();
            this.gSTDb.Customers.AddCustomersRow(r);
            Data.GSTDbTableAdapters.CustomersGSTNosTableAdapter CGT = new Data.GSTDbTableAdapters.CustomersGSTNosTableAdapter();
            CGT.FillByParentCode(gSTDb.CustomersGSTNos, r.RecCode);
        }

        private void Ts_SearchClicked(object sender, EventArgs e)
        {
            Heptanesia.Winforms.Ui.Forms.SearchForm sf = new Heptanesia.Winforms.Ui.Forms.SearchForm();
            sf.DataSource = new Data.GSTDbTableAdapters.CustomersTableAdapter().GetData();
            sf.DisplayColumns = new string[] { "Name" };
            sf.DisplayHeaders = new string[] { "Customers Name" };
            sf.DisplayWidths = new int[] { 200 };
            if (sf.ShowDialog(this) == DialogResult.OK)
            {
                this.RC = sf.ReturnRow["RecCode"].ToString();
                this.customersTableAdapter.FillByRecCode(this.gSTDb.Customers, RC);
            }
        }

        private void Ts_SaveClicked(object sender, EventArgs e)
        {
            string message = "Error: ";
            try
            {
                this.customersBindingSource.EndEdit();
                //if (this.DgGST.CurrentRow.DataBoundItem != null)
                this.fkCustomerGSTNosCustomersBindingSource.EndEdit();
                if (this.gSTDb.Customers.HasErrors)
                    message += Classes.Global.SetBindingSourcePositionAndGetError(this.customersBindingSource, this.gSTDb.Customers.GetErrors()[0]);
                else if (this.gSTDb.CustomersGSTNos.HasErrors)
                    message += Classes.Global.SetBindingSourcePositionAndGetError(this.fkCustomerGSTNosCustomersBindingSource, this.gSTDb.CustomersGSTNos.GetErrors()[0]);
                else
                {
                    this.gSTDb.Customers.BeforeSave();
                    this.gSTDb.CustomersGSTNos.BeforeSave();
                    Data.GSTDbTableAdapters.TableAdapterManager tm = new Data.GSTDbTableAdapters.TableAdapterManager();
                    tm.CustomersTableAdapter = this.customersTableAdapter;
                    tm.CustomersGSTNosTableAdapter = this.customersGSTNosTableAdapter;
                    tm.UpdateAll(this.gSTDb);
                    message = "Record(s) Saved Successfully";
                }
            }
            catch (Exception ex)
            {
                message += ex.Message;
            }
            MessageBox.Show(message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }


        private void Customers_Load(object sender, EventArgs e)
        {
            this.FormIsLoading = true;
            this.customersTableAdapter.Fill(this.gSTDb.Customers);
            this.customersGSTNosTableAdapter.Fill(this.gSTDb.CustomersGSTNos);
            this.countriesTableAdapter.Fill(this.gSTDb.Countries);
            this.statesTableAdapter.Fill(this.gSTDb.States);
            this.citiesTableAdapter.Fill(this.gSTDb.Cities);
            this.gSTDb.Customers.Rows.Clear();
            if (this.gSTDb.Customers.Rows.Count == 0)
            {
                Data.GSTDb.CustomersRow r = this.gSTDb.Customers.NewCustomersRow();
                this.gSTDb.Customers.AddCustomersRow(r);
            }
            this.FormIsLoading = false;
        }


        private void DgGST_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            if (e.Exception is ArgumentException)
            {
                object value = DgGST.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
                if (!((DataGridViewComboBoxColumn)DgGST.Columns[e.ColumnIndex]).Items.Contains(value))
                {
                    ((DataGridViewComboBoxCell)DgGST[e.ColumnIndex, e.RowIndex]).Value = DBNull.Value;
                    //((DataGridViewComboBoxColumn)dgv.Columns[e.ColumnIndex]).Items.Add(value);
                    e.ThrowException = false;
                }
            }
            else
            {
                MessageBox.Show(e.Exception.Message);
                e.Cancel = true;
            }
        }
    }
}

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]

Quote:
The main issue here is that it is not clearing the rows??

Use the debugger to see what is done, then you will know what is missing or go wrong, it is the first step to correction.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]
Debugging C# Code in Visual Studio - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
Comments
Member 12562978 23-Oct-17 10:27am    
ppolymorphe. I have already debugged the code and I tried to find out why Rows are not clearing but its still not clearing in the data visualizer.I am not understanding the reason that's why I am asking.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900