<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" PageSize="10"
Lebar="542px" AllowPaging="Benar" AllowSorting="Benar"
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" >
<Kolom>
<asp:TemplateField HeaderText="序号">
<Templat Barang>
<%# this.GridView1.PageIndex * this.GridView1.PageSize + this.GridView1.Rows.Count + 1%>
</Templat Barang>
</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>
<Templat Barang>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("xueliText") %>'></asp:Label>
</Templat Barang>
</asp:TemplateField>
<asp:TemplateField HeaderText="学历名称" SortExpression="DB1_2">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("DB1_2") %>'></asp:TextBox>
</EditItemTemplate>
<Templat Barang>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("DB1_2") %>'></asp:Label>
</Templat Barang>
</asp:TemplateField>
<asp:TemplateField HeaderText="操作" ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="Benar" CommandName="Perbarui"
Teks="更新"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Batal"
Teks="取消"></asp:LinkButton>
</EditItemTemplate>
<Templat Barang>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit"
Text="编辑" OnClientClick="return konfirmasi('确认要编辑吗?');"></asp:LinkButton>
<asp:LinkButton ID="LinkButton3" runat="server" CausesValidation="False" CommandName="Hapus"
Text="删除" OnClientClick="return konfirmasi('确认要删除吗?');"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Pilih"
Teks="选择"></asp:LinkButton>
</Templat Barang>
</asp:TemplateField>
</Kolom>
<AlternatingRowStyle BackColor="Aquamarine" />
</asp:Tampilan Grid>
/// <ringkasan>
/// 定数据到GridView
/// </ringkasan>
kekosongan pribadi GridViewBind()
{
检索数据库
string strSql = "PILIH * DARI DB1";
得到数据集
this.GridView1.DataSource=sambungan.GetDs(strSql).Tables[0].DefaultView;
ini.GridView1.DataBind();
}
/// <ringkasan>
/// 编辑当前行
/// </ringkasan>
/// <param nama="pengirim"></param>
/// <param nama="e"></param>
dilindungi kekosongan GridView1_RowEditing(pengirim objek, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
//当前编辑行背景色高亮
this.GridView1.EditRowStyle.BackColor = Warna.FromName("#F7CE90");
GridViewBind();
}
/// <ringkasan>
/// 取消编辑状态
/// </ringkasan>
/// <param nama="pengirim"></param>
/// <param nama="e"></param>
protected void GridView1_RowCancelingEdit(pengirim objek, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
GridViewBind();
}
/// <ringkasan>
/// 删除记录过程
/// </ringkasan>
/// <param nama="pengirim"></param>
/// <param nama="e"></param>
dilindungi kekosongan GridView1_RowDeleting (pengirim objek, GridViewDeleteEventArgs e)
{
//得到单位编号
string rowToDelete = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
//转换为整数
//int ID=Konversi.ToInt32(rowToDelete);
//从数据库中删除
string str = "HAPUS DARI DB1 di mana DB1_1=" + "'" + rowToDelete + "'" + "";
mencoba
{
samb.RunSql(str);
//重新绑定数据
GridViewBind();
}
menangkap (Pengecualian ex)
{
Response.Write("数据库错误,错误原因:" + ex.Message);
Respon.End();
}
}
/// <ringkasan>
/// 更新记录过程
/// </ringkasan>
/// <param nama="pengirim"></param>
/// <param nama="e"></param>
dilindungi kekosongan GridView1_RowUpdating(pengirim objek, GridViewUpdateEventArgs e)
{
string ID = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
string DB1_1 = ((Kotak Teks)GridView1.Rows[e.RowIndex].FindControl("Kotak Teks1")).Teks;
//string DB1_2 = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2")).Teks;
string DB1_2 = (((DropDownList))GridView1.Rows[e.RowIndex].FindControl("ddlXL")).SelectedItem.Text;
//判断表单项是否有空并给出提示信息
jika (DB1_1 == "" || DB1_2 == "")
{
conn.Alert("请输入完整信息!", Halaman);
kembali;
}
mencoba
{
conn.BuilderEdit("pilih * dari DB1 di mana DB1_1 ='" + ID + "'");
samb.dr["DB1_1"] = DB1_1;
samb.dr["DB1_2"] = DB1_2;
samb.BuilderEditClose();
}
tangkap (OracleException salah)
{
if (err.Code.ToString() == "1")
conn.Alert("错误:已存在具有相同主键的记录", Halaman);
kalau tidak
samb.Alert("错误:未能添加记录", Halaman);
}
Response.Write("<script bahasa='javascript'>alert('数据已被保存!');</script>");
//返回浏览状态
GridView1.EditIndex = -1;
GridViewBind();
}
/// <ringkasan>
/// 分页事件
/// </ringkasan>
/// <param nama="pengirim"></param>
/// <param nama="e"></param>
dilindungi kekosongan GridView1_PageIndexChanging (pengirim objek, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridViewBind();
}
/// <ringkasan>
/// 加入鼠标效果及为DropDownList绑定值
/// </ringkasan>
/// <param nama="pengirim"></param>
/// <param nama="e"></param>
dilindungi kekosongan GridView1_RowDataBound (pengirim objek, GridViewRowEventArgs e)
{
//为DropDownList
if (((DropDownList)e.Row.FindControl("ddlXL")) != null)
{
DropDownList ddlXL = (DropDownList)e.Row.FindControl("ddlXL");
ddlXL.Item.Hapus();
ddlXL.Items.Add(ListItem baru("博士", "1"));
ddlXL.Items.Add(New ListItem("硕士", "2"));
ddlXL.Items.Add(ListItem baru("学士", "3"));
}
//加入鼠标滑过的高亮效果
if (e.Row.RowType == DataControlRowType.DataRow)//判定当前的行是否属于datarow类型的行
{
// 当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色
e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='kuning',this.style.fontWeight='';");
// 当鼠标离开的时候 将背景颜色还原的以前的颜色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=warna saat ini,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';");
}
}