whenever i delete, insert or update my datasource, gridview does not refresh. However, when i manually refresh my page, then it does. How to make it such a way that it refreshes by it's own whenever I insert,delete or update anything from my datasource.
P.s: DataBind() method is not working

maske sure you are updating the datasource of the gridview before you databind.

gridview1.databind = myds

when you update your dataSource then before binding your datagrid to it make sure
its datasource is null and also delete already placed columns and rows from datagrid

Hi. Make sure you type "BindGrid()" in each and every function of either deleting, inserting or updating.

Note: GridView = mydg

Using System.Data.SqlClient;
     protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGrid(); //user defined 
        }
    }

    public void BindGrid()
    {
      SqlConnection sqlcon = new SqlConnection("Server=DEEPZ; uid=sa; pwd= ; Database=deepali");
        SqlDataAdapter sqladap = new SqlDataAdapter("select * from Student",sqlcon);
        DataSet dset = new DataSet();
        sqladap.Fill(dset,"Student");
        mydg.DataSource = dset.Tables["Student"].DefaultView;
        mydg.DataBind();
    }

    public void mydgDelete(object sender, DataGridCommandEventArgs e)
    {
        mydg.Columns[0].HeaderText = "Delete";

        string del = "delete from Student where StudentID=@StudentID";
        SqlCommand cmddel = new SqlCommand(del,sqlcon);

        cmddel.Parameters.Add(new SqlParameter("@StudentID", SqlDbType.Int));
        cmddel.Parameters["@StudentID"].Value = mydg.DataKeys[(int)e.Item.ItemIndex];
        
        sqlcon.Open();
        try
        {
            cmddel.ExecuteNonQuery();           
            msg.InnerHtml = "Record deleted!";
        }
        catch(SqlException)
        {
            
            msg.InnerHtml = "Could not delete";
        }

        sqlcon.Close();
        BindGrid();
    }

thanks friends...i will check your suggestions, and will let ya knw !

okay :)

protected void Page_Load(object sender, EventArgs e)
{  
    if (!IsPostBack)
    {
        GridViewDataBind();
    }
}

private void GridViewDataBind()
{
    SqlConnection con = new SqlConnection("data source=192.168.1.1;initial catalog=test;user id=sa;password=sa;integrated security=False");

    SqlCommand cmd = new SqlCommand("select * from testTable");
    con.Open();
    cmd.Connection = con;
    SqlDataReader dr = cmd.ExecuteReader();
    DataTable dt = new DataTable();
    dt.Load(dr);
    gridView.DataSource = dt
    gridView.DataBind();
}
simply follow the following steps:
1.Write a method containing gridview info
2.Second one put a button control on to webpage or webpage
3.Then double click the button control and call the gridview method.
You need to refresh the page and also the after an isert to the grid view.
Create a function and call it when page load after insert:

 private void BindData()
    {
        Your gridview data source.DataBind();
        GridView name.DataBind();
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.