0

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

8
Contributors
9
Replies
25
Views
6 Years
Discussion Span
Last Post by naimerkallis
0

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

gridview1.databind = myds

0

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

0

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();
    }
0
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();
}

Edited by amol.manwatkar: editing

0
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.
0
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();
    }
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.