public void T() { myConnection.Open(); string sql = "Select * From Test"; SqlDataAdapter da = new SqlDataAdapter(sql, myConnection); DataSet ds = new DataSet(); da.Fill(ds, "Test"); GridView1.DataSource = ds; GridView1.DataKeyNames = new string[] { "ID" }; GridView1.DataBind(); myConnection.Close(); }
public void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) {
myConnection.Open(); GridViewRow row = GridView1.Rows[e.RowIndex]; int ID = int.Parse(GridView1.DataKeys[e.RowIndex].Values[0].ToString()); string userName = ((TextBox)row.FindControl("UserName")).Text.ToString(); double Money = Convert.ToDouble(((TextBox)row.FindControl("Money")).Text.ToString()); SqlDataReader dr; string sql = "Update Test Set UserName=‘" + userName + "‘,Money=" + Money + " Where ID=" + ID + ""; SqlCommand myCommand = new SqlCommand(sql, myConnection); dr = myCommand.ExecuteReader(); GridView1.EditIndex = -1; myConnection.Close(); T(); }
public void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { myConnection.Open(); GridViewRow row = GridView1.Rows[e.RowIndex]; int ID = int.Parse(GridView1.DataKeys[e.RowIndex].Values[0].ToString()); SqlDataReader dr; string sql = "Delete From Test Where ID=" + ID + ""; SqlCommand myCommand = new SqlCommand(sql, myConnection); dr = myCommand.ExecuteReader(); GridView1.EditIndex = -1; myConnection.Close(); T(); }