Click here to Skip to main content
15,894,041 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i am trying to reload/refresh my winform can any one please suggest me how to procede
my winform includes a data grid, text boxes, combobox. i want to reset them at there orignal position as they were when the form load.

What I have tried:

i made a button name new and on the new_click event i cleared all fields. it works fine but my datagrid looses its property.

this method i used to clear the data
private void ClearData()
        {
            _dtgrid.Rows.Clear();
            txtaddressline1.Text = "";
            txtaddressline2.Text = "";
            txtBillValue.Text = "";
            txtCUSTOMERNAMETEXT.Text = "";
            txtDiscountAmt.Text = "";
            txtDisPer.Text = "";
            txtgrn.Text = "";
            txtGSTNO.Text = "";
            txtINVOICENO.Text = "";
            txtname.Text = "";
            txtRoundOf.Text = "";
            txtTRANSPORT.Text = "";
            txtvichle.Text = "";
            btnCustomer.Focus();
            this.Refresh();
        }

this is the buttion click event
private void NEW_Click(object sender, EventArgs e)
        {
            ClearData();
            cmbINVOICETYPE.Focus();
        }
Posted
Updated 21-Jun-19 19:34pm
Comments
CHill60 21-Jun-19 10:15am    
You must have some code to populate the controls when you load the form - just call that again from the button
MukulMohal 21-Jun-19 10:35am    
it gives me error like 'A column named 'Item' already belongs to this DataTable.'

Quote:
it works fine but my datagrid loses its property.

Well yes, it will:
C#
private void ClearData()
{
    _dtgrid.Rows.Clear();

If you specifically remove all data from it, it will indeed lose all data...
 
Share this answer
 
Comments
MukulMohal 21-Jun-19 10:36am    
it even losses it data binding
Define a default name for the DataTable, and save the existing DataGreidView structure and data:
// required: using System.Data;

public const string DTBackUp = "DTBackUp";

// in the Form Load Event, or in another method
dataGridView1.DataSource = dt;

WriteXML(dt);
When you want to restore the DataGridView:
DataTable dt = this.ReadXML();
this.dataGridView1.DataSource = dt;
Using these two methods:
private DataTable ReadXML()
{
    OpenFileDialog ofd = new OpenFileDialog();
    ofd.Filter = "XML|*.xml";

    DataSet dt = new DataSet();

    if (ofd.ShowDialog() == DialogResult.OK)
    {
        try
        {
            dt.ReadXml(ofd.FileName);

            return dt.Tables[0];
        }
        catch (Exception ex)
        {
            throw new DataException("Fail to read");
        }
    }

    return null;
}

private void WriteXML(DataTable dt)
{
    // must have a name to serialize !
    if (dt.TableName == "") dt.TableName = DTBackUp;

    SaveFileDialog sfd = new SaveFileDialog();
    sfd.Filter = "XML|*.xml";

    if (sfd.ShowDialog() == DialogResult.OK)
    {
        try
        {
            dt.WriteXml(sfd.FileName);
        }
        catch (Exception ex)
        {
            throw new DataException("Fail to write");
        }
    }
}
 
Share this answer
 

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