C#的GRIDVIEW问题

2025-06-29 13:25:47
推荐回答(6个)
回答1:

public partial class Look : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindData();
}
}

protected void BindData()
{
OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\release.mdb;");
con.Open();

OleDbCommand com = new OleDbCommand();
com.CommandText = "select * from leaveWord ";
com.Connection = con;

OleDbDataAdapter apter = new OleDbDataAdapter(com);
DataTable tab = new DataTable();
apter.Fill(tab);

GridView1.DataSource = tab;
GridView1.DataBind();

}
protected void Button2_Click(object sender, EventArgs e)
{//批量删除
for (int i = 0; i < GridView1.Rows.Count; i++)
{
if (((CheckBox)GridView1.Rows[i].FindControl("checkBox1")).Checked == true)
{
int id = int.Parse(GridView1.Rows[i].Cells[0].Text);
OleDbConnection olecon = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\release.mdb;");
olecon.Open();
OleDbCommand olecmd = new OleDbCommand("delete * from leaveWord where number=" + id + " ", olecon);
olecmd.ExecuteNonQuery();
olecon.Close();
}
}
BindData();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{//删除
int x = e.RowIndex;
string id = GridView1.Rows[x].Cells[0].Text;
OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\release.mdb;");
con.Open();

OleDbCommand com = new OleDbCommand();
com.CommandText = "Delete * from leaveWord where number=" + id + "";
com.Connection = con;
com.ExecuteNonQuery();
con.Close();
BindData();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{//翻页
GridView1.PageIndex = e.NewPageIndex;
BindData();
}

protected void LinkButton3_Click(object sender, EventArgs e)
{
string num = ((LinkButton)sender).CommandArgument;
Session["n"] = num;
Response.Redirect("Revert.aspx");
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
int i;
//执行循环,保证每条数据都可以更新
for (i = 0; i < GridView1.Rows.Count; i++)
{
//首先判断是否是数据行
if (e.Row.RowType == DataControlRowType.DataRow)
{
//当鼠标停留时更改背景色
e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FEFDEB'");
//当鼠标移开时还原背景色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
}
}
//如果是绑定数据行
if (e.Row.RowType == DataControlRowType.DataRow)
{
//鼠标经过时,行背景色变
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#FEFDEB'");
//鼠标移出时,行背景色变
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");
}
}

//protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
//{//更新

// int a = e.RowIndex;
// string id = GridView1.Rows[a].Cells[0].Text;
// //Response.Write(id);
// string zhi = ((TextBox)GridView1.Rows[a].Cells[1].Controls[1]).Text;
// string title = ((TextBox)GridView1.Rows[a].Cells[2].Controls[0]).Text;
// string manage = ((DropDownList)GridView1.Rows[a].Cells[4].Controls[1]).Text;

// OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\release.mdb;");
// con.Open();

// OleDbCommand com = new OleDbCommand();
// com.CommandText = "update leaveWord set Name='" + zhi + "',title='" + title + "',manage='" + manage + "' where number=" + id + "";
// com.Connection = con;

// com.ExecuteNonQuery();
// con.Close();

// GridView1.EditIndex = -1;
// BindData();

//}

//protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
//{//编辑
// GridView1.EditIndex = e.NewEditIndex;
// BindData();
// //OleDbConnection Con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\release.mdb;");
// //Con.Open();
// //OleDbCommand Com = new OleDbCommand();
// //Com.CommandText = "select * from leaveWord ";
// //Com.Connection = Con;
// //OleDbDataAdapter apter = new OleDbDataAdapter(Com);
// //DataTable table = new DataTable();
// //apter.Fill(table);

// DropDownList hh = (DropDownList)GridView1.Rows[e.NewEditIndex].Cells[4].Controls[1];
// hh.Items.Add("False");
// hh.Items.Add("True");
// //hh.Items = "false";
// //hh.Items = "True";
// //hh.DataTextField = "manage";
// //hh.DataSource = table;
// //hh.DataBind();
//}

//protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
//{//取消
// GridView1.EditIndex = -1;
// BindData();
//}

}

回答2:

gridview72般绝技 看了不后悔
http://wenku.baidu.com/view/d9aeafe9856a561252d36f8c.html

回答3:

access数据库,搞不出来!不过sql 2005或2008就可以!悲剧!

回答4:

access数据库,没用过!sql2000可以搞搞,我这有现成的类似例子,但是是用coolite控件中的gridpanel做的!有没兴趣?

回答5:

我做进销存的时候这些功能都需要的,不过你的内容太多了,建议慢慢来吧。

回答6:

你这种最好用实体来实现 这样方便 现在不能马上写出来