ASP.net gridview之性别
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.SqlClient; using MySql.Data.MySqlClient; public partial class GridView_分页_ : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { ViewState["SortOrder"] = "stu_id"; ViewState["OrderDire"] = "ASC"; Bind(); } } public void Bind() { DropDownList ddl; string sqlstr = "select * from t_student"; DataSet myds = DBHelper.SqlHelper.ExecuteDataSetText(sqlstr); DataView view = myds.Tables[0].DefaultView; string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"]; view.Sort=sort; GridView1.DataSource=view; ////GridView1.DataSource = myds; GridView1.DataKeyNames = new string[] { "stu_id" };//主键 GridView1.DataBind(); for (int i = 0; i <= GridView1.Rows.Count - 1; i++) { //当前页从那条开始 int page = GridView1.PageIndex; int size=GridView1.PageSize; int pageCount = page * size; DataRowView mydrv = myds.Tables[0].DefaultView[pageCount+i]; if (Convert.ToString(mydrv["stu_sex"]).Trim() == "1") { ddl = (DropDownList)GridView1.Rows[i].FindControl("DropDownList1"); ddl.SelectedIndex = 0; } if (Convert.ToString(mydrv["stu_sex"]).Trim() == "0") { ddl = (DropDownList)GridView1.Rows[i].FindControl("DropDownList1"); ddl.SelectedIndex = 1; } } } protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; Bind(); } protected void GridView1_Sorting(object sender, GridViewSortEventArgs e) { string sPage = e.SortExpression; if (ViewState["SortOrder"].ToString() == sPage) { if (ViewState["OrderDire"].ToString() == "Desc") ViewState["OrderDire"] = "ASC"; else ViewState["OrderDire"] = "Desc"; } else { ViewState["SortOrder"] = e.SortExpression; } Bind(); } protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex;//把那一行设为编辑状态 Bind(); } protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView1.EditIndex = -1; Bind(); } protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { string sqlstr = "update t_student set stu_code='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',stu_name='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "',stu_sex='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim() + "',age='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim() + "',stu_dept='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text.ToString().Trim() + "',stu_admin='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[7].Controls[0])).Text.ToString().Trim() + "',stu_provinces='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[8].Controls[0])).Text.ToString().Trim() + "',stu_city='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[9].Controls[0])).Text.ToString().Trim() + "',stu_districts='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[10].Controls[0])).Text.ToString().Trim() + "' where stu_id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'"; DBHelper.SqlHelper.ExecteNonQueryText(sqlstr); GridView1.EditIndex = -1; Bind(); } protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { string sqlstr = "delete from t_student where stu_id='"+ GridView1.DataKeys[e.RowIndex].Value.ToString() + "'"; DBHelper.SqlHelper.ExecteNonQueryText(sqlstr); Response.Write("<script>alert('删除成功')</script>"); GridView1.EditIndex = -1; Bind(); } protected void CheckBox3_CheckedChanged(object sender, EventArgs e) { for (int i = 0; i <= GridView1.Rows.Count - 1; i++) { CheckBox CheckBox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox2"); if (CheckBox3.Checked == true) { CheckBox.Checked = true; } else { CheckBox.Checked = false; } } CheckBox4.Checked = false; } protected void CheckBox4_CheckedChanged(object sender, EventArgs e) { for (int i = 0; i <= GridView1.Rows.Count - 1; i++) { CheckBox CheckBox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox2"); if (CheckBox4.Checked == false) { CheckBox.Checked = true; } else { CheckBox.Checked = false; } } CheckBox3.Checked = false; } protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { } protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e) { GridView1.SelectedIndex = e.NewSelectedIndex; int index = e.NewSelectedIndex; string key = GridView1.DataKeys[index].Value.ToString(); CheckBox check = (CheckBox)GridView1.Rows[index].FindControl("CheckBox2"); check.Checked = true; } public MySqlDataReader ddbind() { string sql = " select distinct stu_sex,case(stu_sex) when 1 then '男' else '女' end as stu_sex_ch from t_student"; return DBHelper.SqlHelper.ExecuteReaderText(sql, null); } }
<asp:DropDownList ID="DropDownList1" runat="server"
DataValueField="stu_sex" Height="19px" Width="67px" DataTextField="stu_sex_ch" DataSource="<%#ddbind() %>">
</asp:DropDownList>