public partial class AdminUser : System.Web.UI.Page
{
//declaring static variables, my datatable from my database, which will fill in the gridview, and will adjust itself to also allow updating
static DataTable dt;
static string currentUserName;
//I need this button, don't ask me why, Eden said it wouldn't work without it. This button basically binds the datatable to the database of the cart, and then transports that unto the gridview for the first time in this page.
protected void ShowGridView_Click(object sender, EventArgs e)
{
ShowGridView.Visible = false;
string sql = "SELECT * FROM users";
Connect db = new Connect("userDB.accdb");
dt = db.MakeConnection(sql, "users");
UsersGridView.DataSource = dt;
UsersGridView.DataBind();
}
//the same one you saw previously
protected void RemoveUser(object sender, EventArgs e)
{
Connect db = new Connect("userDB.accdb");
LinkButton lb = (LinkButton)sender;
string id = lb.CommandArgument.ToString();
string deletesql = "DELETE FROM users WHERE userName='" + id + "';";
db.ConnectInsert(deletesql, "users");
Response.Redirect("AdminUser.aspx");
}
// What to do when you decide to stop editing.
//Also, some of the notes are written by Eden.
public void UsersGridView_RowCancelingEdit(Object sender, GridViewCancelEditEventArgs e)
{
UsersGridView.EditIndex = -1;//resets it to non-edit mode
UsersGridView.DataSource = dt; //rebinds the datatable
DataBind();
}
//What happens when you click the editing button. (In browser, it makes all the labels turn into textboxes and the checkboxes turn active. Then the edit button turns into an update button and a cancel button. Useful)
protected void UsersGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
//Set the edit index.
currentUserName = ((Label)UsersGridView.Rows[e.NewEditIndex].FindControl("UserNamelbl")).Text;
UsersGridView.EditIndex = e.NewEditIndex;
UsersGridView.DataSource = dt;
// I made my users dt static so i can re-bind it whenever i need
DataBind();
}
//Here's the kicker, this is where the updating happens. This annoyed me to no end until Eden my saviour (praise be unto him) guided me into the land of light and itemtemplates.
protected void UsersGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = UsersGridView.Rows[e.RowIndex]; //take the row that is updating
Connect db = new Connect("userDB.accdb");
string updatesql = "UPDATE users Set userName= '" + ((TextBox)UsersGridView.Rows[e.RowIndex].Cells[0].FindControl("UserNameEdit")).Text + "' , email= '" + ((TextBox)UsersGridView.Rows[e.RowIndex].Cells[1].FindControl("EmailEdit")).Text + "' , isAdmin= " + ((CheckBox)UsersGridView.Rows[e.RowIndex].Cells[3].FindControl("IsAdminBoxEdit")).Checked + " WHERE userName= '" + currentUserName.ToString() + "';"; // update this shit
db.ConnectInsert(updatesql, "users");
UsersGridView.EditIndex = -1;
string usersql = "SELECT * FROM users;";
dt = db.MakeConnection(usersql, "users");
UsersGridView.DataSource = dt; //after updating a user you need to re-bind you gridview with a refreshed and updated Dt.
DataBind();
}
}