<asp:GridView ID="GridView1" runat="servidor" AutoGenerateColumns="False" PageSize="10"
Largura="542px" AllowPaging="True" AllowSorting="True"
DataKeyNames="DB31_1,DB31_2" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowDataBound="GridView1_ RowDataBound" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnSorting="GridView1_Sorting" >
<Colunas>
<asp:TemplateField HeaderText="序号">
<ItemTemplate>
<%# this.GridView1.PageIndex * this.GridView1.PageSize + this.GridView1.Rows.Count + 1%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="学历代码" SortExpression="DB1_1">
<EditItemTemplate>
<%--<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("DB1_1") %>'></asp:TextBox>--%>
<asp:DropDownList ID ="ddlXL" runat="server" DataValueField='<%# Bind("DB1_1") %>'></asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("xueliText") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="学历名称" SortExpression="DB1_2">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("DB1_2") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("DB1_2") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="操作" ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="servidor" CausesValidation="True" CommandName="Atualização"
Text="更新"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="servidor" CausesValidation="False" CommandName="Cancelar"
Text="取消"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="servidor" CausesValidation="False" CommandName="Editar"
Text="编辑" OnClientClick="return confirm('确认要编辑吗?');"></asp:LinkButton>
<asp:LinkButton ID="LinkButton3" runat="servidor" CausesValidation="False" CommandName="Excluir"
Text="删除" OnClientClick="return confirm('确认要删除吗?');"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="servidor" CausesValidation="False" CommandName="Selecionar"
Text="选择"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Colunas>
<AlternatingRowStyle BackColor="Aquamarine" />
</asp:GridView>
/// <resumo>
/// Atualizar o GridView
/// </sumário>
vazio privado GridViewBind()
{
检索数据库
string strSql = "SELECIONE * FROM DB1";
得到数据集
this.GridView1.DataSource=conn.GetDs(strSql).Tables[0].DefaultView;
this.GridView1.DataBind();
}
/// <resumo>
/// 编辑当前行
/// </sumário>
/// <param name="sender"></param>
/// <param name="e"></param>
void protegido GridView1_RowEditing (remetente do objeto, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
//当前编辑行背景色高亮
this.GridView1.EditRowStyle.BackColor = Color.FromName("#F7CE90");
GridViewBind();
}
/// <resumo>
/// 取消编辑状态
/// </sumário>
/// <param name="sender"></param>
/// <param name="e"></param>
void protegido GridView1_RowCancelingEdit (remetente do objeto, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
GridViewBind();
}
/// <resumo>
/// 删除记录过程
/// </sumário>
/// <param name="sender"></param>
/// <param name="e"></param>
void protegido GridView1_RowDeleting (remetente do objeto, GridViewDeleteEventArgs e)
{
//得到单位编号
string rowToDelete = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
//转换为整数
//int ID=Convert.ToInt32(rowToDelete);
//从数据库中删除
string str = "DELETE FROM DB1 onde DB1_1=" + "'" + rowToDelete + "'" + "";
tentar
{
conn.RunSql(str);
//重新绑定数据
GridViewBind();
}
pegar (exceção ex)
{
Response.Write("数据库错误,错误原因:" + ex.Message);
Response.End();
}
}
/// <resumo>
/// 更新记录过程
/// </sumário>
/// <param name="sender"></param>
/// <param name="e"></param>
void protegido GridView1_RowUpdating (remetente do objeto, GridViewUpdateEventArgs e)
{
string ID = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
string DB1_1 = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1")).Text;
//string DB1_2 = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2")).Text;
string DB1_2 = (((DropDownList))GridView1.Rows[e.RowIndex].FindControl("ddlXL")).SelectedItem.Text;
//判断表单项是否有空并给出提示信息
se (DB1_1 == "" || DB1_2 == "")
{
conn.Alert("请输入完整信息!", Página);
retornar;
}
tentar
{
conn.BuilderEdit("selecione * do DB1 onde DB1_1 ='" + ID + "'");
conn.dr["DB1_1"] = DB1_1;
conn.dr["DB1_2"] = DB1_2;
conn.BuilderEditClose();
}
pegar (erro OracleException)
{
se (err.Code.ToString() == "1")
conn.Alert("错误:已存在具有相同主键的记录", Página);
outro
conn.Alert("错误:未能添加记录", Página);
}
Response.Write("<script language='javascript'>alert('数据已被保存!');</script>");
//返回浏览状态
GridView1.EditIndex = -1;
GridViewBind();
}
/// <resumo>
/// 分页事件
/// </sumário>
/// <param name="sender"></param>
/// <param name="e"></param>
void protegido GridView1_PageIndexChanging (remetente do objeto, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridViewBind();
}
/// <resumo>
/// 加入鼠标效果及为DropDownList绑定值
/// </sumário>
/// <param name="sender"></param>
/// <param name="e"></param>
void protegido GridView1_RowDataBound (remetente do objeto, GridViewRowEventArgs e)
{
//为DropDownList绑定值
if (((DropDownList)e.Row.FindControl("ddlXL")) != nulo)
{
DropDownList ddlXL = (DropDownList)e.Row.FindControl("ddlXL");
ddlXL.Items.Clear();
ddlXL.Items.Add(new ListItem("博士", "1"));
ddlXL.Items.Add(new ListItem("硕士", "2"));
ddlXL.Items.Add(new ListItem("学士", "3"));
}
//加入鼠标滑过的高亮效果
if (e.Row.RowType == DataControlRowType.DataRow)//判定当前的行是否属于datarow类型的行
{
//当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色
e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='yellow',this.style.fontWeight='';");
//当鼠标离开的时候 将背景颜色还原的以前的颜色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=cor atual,this.style.fontWeight='';");
}
//单击行改变行背景颜色
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", "this.style.backgroundColor='#99cc00'; this.style.color='buttontext';this.style.cursor='default';");
}
}