简体   繁体   中英

Data grid view not displaying the updated data in win form c#?

Okay so I have this datagridview that is connected to the database in access through the data source and it is fine but when I add another row in the table from a form and when I return back to the form having the datagridview it only shows the previous values, not the newly added values.

Even After I restart/ re-run the application. It keeps on happening.

Btw, I am using a AutoFilter feature by following this link AutoFilter in DataGridView

Here is the .cs file:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DataGridViewAutoFilter;

namespace Call_Logger
{
public partial class AutoFilter : Form
{
    public AutoFilter()
    {
        InitializeComponent();

    }

    private void AutoFilter_Load(object sender, EventArgs e)
    {
        // TODO: This line of code loads data into the    'datasetFilter.LogCall' table. You can move, or remove it, as needed.

       this.logCallTableAdapter.Fill(this.datasetFilter.LogCall);

    }

    private void showAllLabel_Click(object sender, EventArgs e)
    {
        DataGridViewAutoFilterTextBoxColumn.RemoveFilter(dataGridView1);
    }

    private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
    {

        String filterStatus = DataGridViewAutoFilterColumnHeaderCell.GetFilterStatus(dataGridView1);
        if (String.IsNullOrEmpty(filterStatus))
        {
            showAllLabel.Visible = false;
            filterStatusLabel.Visible = false;
        }
        else
        {
            showAllLabel.Visible = true;
            filterStatusLabel.Visible = true;
            filterStatusLabel.Text = filterStatus;
        }

    }

    private void button1_Click(object sender, EventArgs e)
    {
         dataGridView1.DataSource = null;
        dataGridView1.DataSource = logCallBindingSource; 
    }


}
}

Here is the form which performs the adding functionality

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Call_Logger
{
public partial class AddingFunctionality : Form
{
    private OleDbConnection con = new OleDbConnection();
    public AddingFunctionality()
    {
        InitializeComponent();
        con.ConnectionString = ConfigurationManager.ConnectionStrings["Connection"].ToString();
    }

    private void btn_Save_Click(object sender, EventArgs e)
    {



        if (comboCallLogBy.Text == "")
        {
            comboCallLogBy.BackColor = Color.LightSalmon;
            MessageBox.Show("Name of the person who is logging the call is required", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
            comboCallLogBy.Focus();
            return;
        }

        if (comboCallType.Text == "")
        {
            comboCallType.BackColor = Color.LightSalmon;
            MessageBox.Show("Specification of the type of call is required", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
            comboCallType.Focus();
            return;
        }

        if (comboCallLocation.Text == "")
        {
            comboCallLocation.BackColor = Color.LightSalmon;
            MessageBox.Show("Location of the call is required", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
            comboCallLocation.Focus();
            return;
        }

        if (txtIncidentNumber.Text == "")
        {
            txtIncidentNumber.BackColor = Color.LightSalmon;
            MessageBox.Show("Please enter the incident number", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
            txtIncidentNumber.Focus();
            return;
        }
        else {
            decimal OutNumber;
            if (!decimal.TryParse(txtIncidentNumber.Text, out OutNumber))
            {
                txtIncidentNumber.BackColor = Color.LightSalmon;
                MessageBox.Show("Value for Incident number should be numerical", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtIncidentNumber.Focus();
                return;

            }

        }

        if (comboCaller.Text == "")
        {
            comboCaller.BackColor = Color.LightSalmon;
            MessageBox.Show("Name of the caller is required", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
            comboCaller.Focus();
            return;
        }

        if (comboCallFor.Text == "")
        {
            comboCallFor.BackColor = Color.LightSalmon;
            MessageBox.Show("Name of person the call is made for is required", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
            comboCallFor.Focus();
            return;
        }

        if (comboAssignedBy.Text == "")
        {
            comboAssignedBy.BackColor = Color.LightSalmon;
            MessageBox.Show("Name of the person assigning call is required", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
            comboAssignedBy.Focus();
            return;
        }

        if (comboForwardTo.Text == "")
        {
            comboForwardTo.BackColor = Color.LightSalmon;
            MessageBox.Show("Name of the person call is forwarded to is required", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
            comboForwardTo.Focus();
            return;
        }

        if (comboContactPerson.Text == "")
        {
            comboContactPerson.BackColor = Color.LightSalmon;
            MessageBox.Show("Name of the person contacting is required", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
            comboContactPerson.Focus();
            return;
        }

        if (comboClosedBy.Text == "")
        {
            comboClosedBy.BackColor = Color.LightSalmon;
            MessageBox.Show("Name of the person call closed by is required", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
            comboClosedBy.Focus();
            return;
        }

        if (comboStatus.Text == "")
        {
            comboStatus.BackColor = Color.LightSalmon;
            MessageBox.Show("Status of the call is required", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
            comboStatus.Focus();
            return;
        }
        try
        {

            con.Open();
            OleDbCommand command = new OleDbCommand();
            command.Connection = con;
            command.CommandText = ("insert into LogCall (CallLogBy, CallLogDate, CallLogTime, CallType, CallLocation, ProblemDescription1, ProblemDescription2, IncidentNo, Caller, CallFor, AssignedBy, AssignedDate, AssignedTime, ForwardTo, ContactPerson, ForwardDate, ForwardTime, ActionTaken1, ActionTaken2, NextStep1, NextStep2, ClosedBy, CloseDate, CloseTime, Remarks1, Remarks2, Status) values ('" + comboCallLogBy.Text + "','" + dateTimePicker1.Text + "','" + dateTimePicker2.Text + "','" + comboCallType.Text + "','" + comboCallLocation.Text + "','"+ textBox1.Text + "','" + textBox2.Text + "','" + txtIncidentNumber.Text + "','" + comboCaller.Text + "','" + comboCallFor.Text + "','" + comboAssignedBy.Text + "','" + dateTimePicker3.Text + "','" + dateTimePicker4.Text + "','" + comboForwardTo.Text + "','" + comboContactPerson.Text + "','" + dateTimePicker5.Text + "','" + dateTimePicker6.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + comboClosedBy.Text + "','" + dateTimePicker7.Text + "','" + dateTimePicker8.Text + "','" + textBox8.Text + "','" + textBox9.Text + "','" + comboStatus.Text + "')");
            command.ExecuteNonQuery();

            MessageBox.Show("Data saved successfully", "Record Added", MessageBoxButtons.OK, MessageBoxIcon.Information);
            con.Dispose();
            this.Hide();
            Dashboard dboard = new Dashboard();
            dboard.ShowDialog();
            con.Close();
        }

        catch (Exception ex)
        {
            MessageBox.Show("Error " + ex);
            con.Close();
        }




    }

    private void AddingFunctionality_Load(object sender, EventArgs e)
    {
        try
        {
            con.Open();
            OleDbCommand command = new OleDbCommand();
            command.Connection = con;
            string query = "select * from MiscData";
            //string query1 = "select * from Try";
            command.CommandText = query;
            //command.CommandText = query1;

            OleDbDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                comboCallLogBy.Items.Add(reader["CallLoggedBy"].ToString());
                //comboCallType.Items.Add(reader["CallType"].ToString());
                //comboCallLocation.Items.Add(reader["CallLocation"].ToString());
                comboCaller.Items.Add(reader["Employees"].ToString());
                comboCallFor.Items.Add(reader["Employees"].ToString());
                comboAssignedBy.Items.Add(reader["Employees"].ToString());
                comboForwardTo.Items.Add(reader["Employees"].ToString());
                comboContactPerson.Items.Add(reader["Employees"].ToString());
                comboClosedBy.Items.Add(reader["Employees"].ToString());
                //comboStatus.Items.Add(reader["Status"].ToString());

            }
            con.Close();

        }

        catch (Exception ex)
        {
            MessageBox.Show("Error " + ex);

        }

        try
        {
            con.Open();
            OleDbCommand command = new OleDbCommand();
            command.Connection = con;
            string query = "select * from CallType";

            command.CommandText = query;


            OleDbDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {

                comboCallType.Items.Add(reader["CallingType"].ToString()+ " " + reader["CallDesc"].ToString());

            }
            con.Close();

        }

        catch (Exception ex)
        {
            MessageBox.Show("Error " + ex);

        }

        try
        {
            con.Open();
            OleDbCommand command = new OleDbCommand();
            command.Connection = con;
            string query = "select * from Location";

            command.CommandText = query;


            OleDbDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {

                comboCallLocation.Items.Add(reader["LocationType"].ToString() + " " + reader["Description"].ToString());

            }
            con.Close();

        }

        catch (Exception ex)
        {
            MessageBox.Show("Error " + ex);

        }


        try
        {
            con.Open();
            OleDbCommand command = new OleDbCommand();
            command.Connection = con;
            string query = "select * from Status";

            command.CommandText = query;


            OleDbDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {

                comboStatus.Items.Add(reader["StatusType"].ToString() + " " + reader["Description"].ToString());

            }
            con.Close();

        }

        catch (Exception ex)
        {
            MessageBox.Show("Error " + ex);

        }



    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.Hide();
        Dashboard Add = new Dashboard();
        Add.ShowDialog();
    }








}
}

DataGridView.Refresh and DataGridView.Update are methods that are inherited from Control. So there work is to do with redrawing the control which is why new rows don't appear.

You need to rebind data to DataGridView like below:

private void button1_Click(object sender, EventArgs e)
{
    dataGridView1.DataSource = null;
    dataGridView1.DataSource = dtData; //add you data here
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM