//一加載就填充GridvIEw控件
public void Bin()
{
string strsql = "select id,title,context,datet From db_guanggao";
com = new SqlCommand(strsql, con);
com.CommandType = CommandType.Text;
da = new SqlDataAdapter();
da.SelectCommand = com;
ds = new DataSet();
try
{
da.Fill(ds, "table");
GridVIEw1.DataSource = ds;
GridVIEw1.AllowPaging = true;//啟動分頁
GridVIEw1.DataBind();
string info = "頁次:<font color=red>";
info += (this.GridView1.PageIndex + 1) + "</font>/" + this.GridVIEw1.PageCount + "頁";
this.Label1.Text = info;//當前頁/總頁如:1/5的形式顯示
//記算dataset的初始值
//計算數據集的下標,數據集的下標也是從0開始的,PageIndex * 2是每頁顯示2條數據,PageIndex下標也是從0開始的
int yeshu = this.GridVIEw1.PageIndex * 2;//2是每頁要顯示的數量是可以隨便改的
//記算最後一個值
int jIE = 0;
if ((ds.Tables[0].Rows.Count - yeshu) > 2)
{
jIE = yeshu + 2;
}
else
{
jIE = ds.Tables[0].Rows.Count;//讀出數據的總行數
}
for (int wu = yeshu; wu < jIE; wu++)
{
int i = 0;
if (wu >= 2)
{
int tempNew = wu % 2;
i = tempNew;
}
else
{
i = wu;
}
}
this.DropDownList1.Items.Clear();
for (int j = 0; j < this.GridVIEw1.PageCount; j++)
{
this.DropDownList1.Items.Add(Convert.ToString(j + 1));
}
}
catch (SqlException ex)
{
myLabel.Text = "數據庫出錯了";
}
finally
{
ds.Clone();
com.Clone();
con.Close();
}
//下面是把GvridVIEw控件的分頁事件,刪除事件已經更新事件
protected void GridView1_PageIndexChanging(object sender, GridVIEwPageEventArgs e)
{
this.GridVIEw1.PageIndex = e.NewPageIndex;
this.Bin();
}
protected void Button1_Click(object sender, EventArgs e)//跳到某一頁的button事件
{
if (DropDownList1.Items.Count != 0)
{
string class1 = this.DropDownList1.SelectedValue.ToString();
GridVIEw1.PageIndex = Convert.ToInt32(class1)-1;
this.Bin();
}
}
//刪除事件
protected void GridView1_RowDeleting(object sender, GridVIEwDeleteEventArgs e)
{
string del = "delete from db_fclass where classname=''"+GridVIEw1.DataKeys[e.RowIndex].Value.ToString()+"''";
com = new SqlCommand(del,con);
try
{
con.Open();
if (com.ExecuteNonQuery() > 0)
{
myLabel.Text = "刪除成功!";
Bin();
}
else
{
myLabel.Text = "刪除失敗!";
}
}
catch (SqlException ex)
{
myLabel.Text = "數據庫失敗!" + ex.Message;
}
finally
{
com.Clone();
con.Close();
}
}
//更新事件
protected void GridView1_RowUpdating(object sender, GridVIEwUpdateEventArgs e)
{
string up_str = Convert.ToString(GridVIEw1.DataKeys[e.RowIndex].Value.ToString().Trim());
string classm = Convert.ToString(GridVIEw1.Rows[e.RowIndex].Cells[0].Text.ToString().Trim());
if (up_str.Equals(classm))
{
myLabel.Text = "沒有更改名字";
}else{
string up_class = "update db_fclass set classname=''"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text.ToString().Trim() + "'' where classname=''"+GridVIEw1.DataKeys[e.RowIndex].Value.ToString()+"''";
com = new SqlCommand(up_class, con);
try
{
con.Open();
if (com.ExecuteNonQuery() > 0)
{
GridVIEw1.EditIndex = -1;
myLabel.Text = "更新成功!";
Bin();
}
else
{
myLabel.Text = "更新失敗!"; r /> }
}
catch (SqlException ex)
{
myLabel.Text = "數據庫錯誤" + ex.Message;
}
finally
{
com.Clone();
con.Close();
}
}
}
//和更新事件一起用的事件,這是必須的
protected void GridView1_RowEditing(object sender, GridVIEwEditEventArgs e)
{
GridVIEw1.EditIndex = e.NewEditIndex;
this.Bin();
}
//和更新事件一起用的事件,這是必須的
protected void GridView1_RowCancelingEdit(object sender, GridVIEwCancelEditEventArgs e)
{
GridVIEw1.EditIndex = -1;
this.Bin();
}
}
//用選擇按鈕激發事件做更新數據
protected void GridView1_SelectedIndexChanging(object sender, GridVIEwSelectEventArgs e)
{
con.Open();
SqlTransaction stran = con.BeginTransaction();//用到事物
string up_str = "update db_Theme set CurrenShow=0";
com = new SqlCommand(up_str, con,stran);
try
{
if (com.ExecuteNonQuery() > 0)/> {
com.CommandText = "update db_Theme set CurrenShow=1 where id="
+ GridVIEw1.DataKeys[e.NewSelectedIndex].Value.ToString();
if (com.ExecuteNonQuery() > 0)
{
Page.RegisterStartupScript("key", "<script>alert(''指定成功!'');</script>");
}
else
{
Page.RegisterStartupScript("key", "<script>alert(''指定失敗!'');</script>");
}
}
stran.Commit();//提交事務
}
catch (SqlException ex)
{
stran.Rollback();//回滾事務
Response.Write("數據庫錯誤!" + ex.Message);
}
finally
{
com.Clone();
con.Close();
}
}
/// <summary>
/// 用GRIDVIEW中的選擇按鈕進行參看用戶上傳的文件然後更新
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_SelectedIndexChanging(object sender, GridVIEwSelectEventArgs e)
{
string f_name=GridVIEw1.Rows[e.NewSelectedIndex].Cells[2].Text.ToString();//得到要查看的文件名
Response.Redirect("../second/file_sc/"+f_name,false);//跳轉查看此文件
//GridVIEw1.DataKeys[e.NewSelectedIndex].Value.ToString()是得到ID值進行更新
//查看後更新
string up_sql = "update ja_file_sc set f_zt=1 where id="+GridVIEw1.DataKeys[e.NewSelectedIndex].Value.ToString();
if (obJSql.RunSql(up_sql))
{
sel_c();//更新後刷新GRIDVIEW
}
}
//CheckBox多選框在GridvIEw中的使用,這裡是被選中的刪除
protected void LinkButton1_Click(object sender, EventArgs e)
{
for (int i = 0; i < GridVIEw1.Rows.Count; i++)
{
//下面是用FindControl("CheckBox1");得到制定控件的ID值,
CheckBox cbox = (CheckBox)GridVIEw1.Rows[i].FindControl("CheckBox1");
if (cbox.Checked == true)
{
string id=GridVIEw1.DataKeys[i].Value.ToString();
con = new SqlConnection(ConfigurationManager.ConnectionStrings["dns"].ConnectionString);
string delete_str = "delete grbg_dzyj_postmail from where id=@id";
com = new SqlCommand(delete_str, con);
com.Parameters.Add("@id", SqlDbType.Int);
com.Parameters["@id"].Value = id;
con.open();
com.com.ExecuteNonQuery();
con.clone();
}
}
Bin();//重新綁定GridevIEw保持數據更新
}
//在gridevIEw頭模板中添加一個CheckBox當選中的時候下面表中的CheckBox全選
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
CheckBox cbox_1 = (CheckBox)GridView1.HeaderRow.FindControl("CheckBox1");//("CheckBox1");//得到gridevIEw頭中的CheckBox控件
for (int i = 0; i < GridVIEw1.Rows.Count; i++)
{
CheckBox cbox = (CheckBox)GridVIEw1.Rows[i].FindControl("CheckBox3");
if (cbox_1.Checked == true)
{
cbox.Checked = true;
}
else
{
cbox.Checked = false;
}
}
}