Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Member
86 Points
130 Posts
Apr 21, 2012 07:46 PM|LINK
Need it this way... public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { bindgrid(); } } public void bindgrid() { SqlConnection con = new SqlConnection("Data Source=STD-258E51EA446\\SQLEXPRESS;Initial Catalog=Employee;Integrated Security=True"); SqlCommand cmd = new SqlCommand("Select * from emp", con); con.Open(); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); cmd.ExecuteScalar(); GridView1.DataSource = ds; GridView1.DataBind(); con.Close(); } protected void Button1_Click(object sender, EventArgs e) { SqlConnection con = new SqlConnection("Data Source=STD-258E51EA446\\SQLEXPRESS;Initial Catalog=Employee;Integrated Security=True"); SqlCommand cmd = new SqlCommand("insert into emp(name,desg,sal,city,email) values('" + TextBox1.Text + "','" + TextBox2.Text + "', '" + TextBox3.Text + "', '" + TextBox4.Text + "','" + TextBox5.Text + "')", con); con.Open(); cmd.ExecuteNonQuery(); bindgrid(); con.Close(); } protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; bindgrid(); } protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView1.EditIndex = -1; bindgrid(); } protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; bindgrid(); } protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { }
rookie tiro
Member
86 Points
130 Posts
Re: How to update GridView?
Apr 21, 2012 07:46 PM|LINK
Need it this way... public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { bindgrid(); } } public void bindgrid() { SqlConnection con = new SqlConnection("Data Source=STD-258E51EA446\\SQLEXPRESS;Initial Catalog=Employee;Integrated Security=True"); SqlCommand cmd = new SqlCommand("Select * from emp", con); con.Open(); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); cmd.ExecuteScalar(); GridView1.DataSource = ds; GridView1.DataBind(); con.Close(); } protected void Button1_Click(object sender, EventArgs e) { SqlConnection con = new SqlConnection("Data Source=STD-258E51EA446\\SQLEXPRESS;Initial Catalog=Employee;Integrated Security=True"); SqlCommand cmd = new SqlCommand("insert into emp(name,desg,sal,city,email) values('" + TextBox1.Text + "','" + TextBox2.Text + "', '" + TextBox3.Text + "', '" + TextBox4.Text + "','" + TextBox5.Text + "')", con); con.Open(); cmd.ExecuteNonQuery(); bindgrid(); con.Close(); } protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; bindgrid(); } protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView1.EditIndex = -1; bindgrid(); } protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; bindgrid(); } protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { }