简体   繁体   中英

C# refresh Datagridview when logout from an account to another

I'm trying to change the data into the datagridview when I log for second time and more.

The problem is: when I log in from a textbox, the data from SQL Server are presented in the Datagridview but after I logout and log in again, the details of preliminary account remains into datagridview. How can I replace or refresh datagridview, so that other account data to replace the previous account details?

Thank you.

Code:

private void btn_qkyqu_Click(object sender, EventArgs e)
{
    DialogResult dialog = MessageBox.Show("Deshironi te dilni?", "Mbylle", MessageBoxButtons.YesNo);

    if (dialog == DialogResult.Yes)
    {
                textBox1_Pin.Text = null;
                groupBox2.Visible = false;
                btn_shiko.Visible = false;
                btn_terhek.Visible = false;
                btn_depozito.Visible = false;
                dataGridView1.DataMember = null;
    }
    else if (dialog == DialogResult.No)
    {
                groupBox2.Visible = true;
                btn_shiko.Visible = true;
                btn_terhek.Visible = true;
                btn_depozito.Visible = true;  
    }
}

我还没有测试过,但是这样做吗?

dataGridView1.Refresh();

This happens because of ReturnUrl presence in your query string. You have to override it.

UPDATE:

Everytime you login, you have to get the values from the database and bind it again to the grid. Or you could bind an empty dataset to the gridview first every time you click the button and then bind the actual values.

grid.DataSource = null;
grid.DataBind();

Your code doesn't show much... How do you fill the DataGridView?

Do you have a DataSource, DataSet some Adapter? You may need to re-fill your DataSet.

Sometimes you also needs to call

dataGridView1.EndEdit();

// Before...
dataGridView1.Refresh();

as it wont update anything if it believes your in edit mode.

Also sometimes you have to refresh the parent as well (drawing problems)

// Last thing to call
dataGridView1.Parent.Refresh();

UPDATE

You could try to clear the DataSet with

ds.Clear();

// Then refresh the gridview
dataGridView1.Refresh();

The GridView should now be empty...

Now try to refill the DataSet with new data

// Clear the DataSet...
ds.Clear();

// Fill the DataSet...
sda.Fill(ds);

// Then refresh the GridView
dataGridView1.Refresh();

Without knowing more of your code, I guess you make a new query when you login again so your SqlDataAdapter has the new data?

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