【转载】gridview修改,添加,删除…

今天有人问起gridview修改的时候取值的问题,以前在2003里用GATAGRID换到2005后很少用了,今天在网上找一这个的修改,添加,删除等,自己有时间也可以看下

下面的gridview使用到了常用的基本功能:

1 dropdownlist的使用

2 新增行的绑定

3 新增行的修改,添加,删除

4 基本功能的编辑,删除

5 行的颜色改变(如编辑行)

6 girdview中时间的验证功能(如开始时间)

7 不可编辑列,不可编辑单元的设置(如建立日期)

代码中有些隐藏的和多余的部分,大家知道一下。最重要的演示gridview的使用。

代码如下:(数据库部分大家感觉一下,不能什么都弄出来。不好意思了。一般人都可以感觉出来的了) 

<%@  Register  TagPrefix="FCKeditorV2"  Namespace="FredCK.FCKeditorV2"  Assembly="FredCK.FCKeditorV2"  %>
<%@  Page  Language="C#"    CodeFile="PeiXunJinXiu.aspx.cs"  Inherits="InterFace_RenShi_PeiXunJinXiu"  ValidateRequest="false"  %>

<!DOCTYPE  html  PUBLIC  "-//W3C//DTD  XHTML  1.0  Transitional//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html  xmlns="http://www.w3.org/1999/xhtml"  >
<head  runat="server">
        <title>培训进修</title>
        <link  href="../../css/style.css"  rel="stylesheet"  type="text/css"  />
</head>
<body>
        <form  id="formPeiXunJinXiu"  runat="server">
        <div>
        <table>
                                <tr>
                                        <td>
                                                <asp:Label  ID="LabelStatus"  runat="server"  Font-Size="10pt"  Text="当前状态:"></asp:Label></td>
                                        <td>
                                                <asp:Menu  ID="MenuCaidan"  runat="server"  BackColor="#FFFFC0"  DynamicHorizontalOffset="2"
                                                        Font-Names="Verdana"  Font-Size="0.8em"  ForeColor="#990000"  OnMenuItemClick="MenuCaidan_MenuItemClick"
                                                        Orientation="Horizontal"  StaticSubMenuIndent="10px">
                                                        <StaticMenuItemStyle  Font-Size="10pt"  HorizontalPadding="5px"  VerticalPadding="2px"  />
                                                        <DynamicHoverStyle  BackColor="#990000"  ForeColor="White"  />
                                                        <DynamicMenuStyle  BackColor="#FFFBD6"  />
                                                        <StaticSelectedStyle  BackColor="Gold"  />
                                                        <DynamicSelectedStyle  BackColor="#FFCC66"  />
                                                        <DynamicMenuItemStyle  HorizontalPadding="5px"  VerticalPadding="2px"  />
                                                        <Items>
                                                                <asp:MenuItem  Selected="True"  Text="人事栏"  Value="客户栏"></asp:MenuItem>
                                                                <asp:MenuItem  Text="培训进修"  Value="培训进修"></asp:MenuItem>
                                                        </Items>
                                                        <StaticItemTemplate>
                                                                <%#  eval_r("Text")  %>
                                                        </StaticItemTemplate>
                                                        <StaticHoverStyle  BackColor="#FFC0C0"  Font-Overline="False"  Font-Underline="True"
                                                                ForeColor="White"  />
                                                </asp:Menu>
                                        </td>
                                </tr>
                        </table>
                        <table  style="border-top-width:  0px">
                                <tr  valign="top">
                                        <td  style="height:  7px">
                                                <img  alt="图象"  height="5"  src="../../Images/funcLine.JPG"  width="933"  /></td>
                                </tr>
                        </table>
                        <asp:MultiView  ID="MultiViewXinXiGuanLi"  runat="server"  ActiveViewIndex="0">
                                <asp:View  ID="ViewXinXiLan"  runat="server">
                                        <table>
                                                <tr>
                                                        <td>
                                                                <table>
                                                                        <tr>
                                                                                <td>
                                                                                        <asp:Image  ID="ImageShouYe"  runat="server"  ImageUrl="~/DatabaseImg/100.jpg"  /></td>
                                                                        </tr>
                                                                </table>
                                                        </td>
                                                        <td>
                                                                <table>
                                                                        <tr>
                                                                                <td  style="width:  99px">
                                                                                        选择类别名称
                                                                                </td>
                                                                                <td>
                                                                                        条件
                                                                                </td>
                                                                                <td>
                                                                                        输入要查询的条件值
                                                                                </td>
                                                                        </tr>
                                                                        <tr>
                                                                                <td  style="width:  99px">
                                                                                        <asp:DropDownList  ID="DropDownListLeiBeiOne"  runat="server"  Width="100px">
                                                                                        </asp:DropDownList></td>
                                                                                <td>
                                                                                        <asp:DropDownList  ID="DropDownListTiaoJianOne"  runat="server"  Width="80px">
                                                                                        </asp:DropDownList></td>
                                                                                <td>
                                                                                        <asp:TextBox  ID="TextBoxOne"  runat="server"  class="smallInput"></asp:TextBox></td>
                                                                        </tr>
                                                                        <tr>
                                                                                <td  style="width:  99px">
                                                                                        <asp:DropDownList  ID="DropDownListLeiBeiTwo"  runat="server"  Width="100px">
                                                                                        </asp:DropDownList></td>
                                                                                <td>
                                                                                        <asp:DropDownList  ID="DropDownListTiaoJianTwo"  runat="server"  Width="80px">
                                                                                        </asp:DropDownList></td>
                                                                                <td>
                                                                                        <asp:TextBox  ID="TextBoxTwo"  runat="server"  class="smallInput"></asp:TextBox></td>
                                                                        </tr>
                                                                        <tr>
                                                                                <td  style="width:  99px">
                                                                                        <asp:DropDownList  ID="DropDownListLeiBeiThree"  runat="server"  Width="100px">
                                                                                        </asp:DropDownList></td>
                                                                                <td>
                                                                                        <asp:DropDownList  ID="DropDownListTiaoJianThre e"  runat="server"  Width="80px">
                                                                                        </asp:DropDownList></td>
                                                                                <td>
                                                                                        <asp:TextBox  ID="TextBoxThree"  runat="server"  class="smallInput"></asp:TextBox></td>
                                                                        </tr>
                                                                        <tr>
                                                                                <td  style="width:  99px">
                                                                                        <asp:DropDownList  ID="DropDownListLeiBeiFour"  runat="server"  Width="100px">
                                                                                        </asp:DropDownList></td>
                                                                                <td>
                                                                                        <asp:DropDownList  ID="DropDownListTiaoJianFour runat="server"  Width="80px">
                                                                                        </asp:DropDownList></td>
                                                                                <td>
                                                                                        <asp:TextBox  ID="TextBoxFour"  runat="server"  class="smallInput"></asp:TextBox></td>
                                                                        </tr>
                                                                        <tr>
                                                                                <td  style="width:  99px">
                                                                                        <font  color="blue">
                                                                                                <asp:Label  ID="Labelchaxunfangshi"  runat="server"  Text="查询排序方式:"></asp:Label></font></td>
                                                                                <td>
                                                                                        <asp:DropDownList  ID="DropDownListPaiXuFangShi runat="server"  BackColor="Transparent"
                                                                                                ForeColor="Blue"  Width="80px">
                                                                                        </asp:DropDownList></td>
                                                                                <td>
                                                                                </td>
                                                                        </tr>
                                                                </table>
                                                        </td>
                                                        <td  style="height:  176px">
                                                                <table>
                                                                        <tr>
                                                                                <td  style="height:  50px">
                                                                                        <asp:Button  ID="ButtonTiJianChaXun"  runat="server"  Height="50px"  OnClick="ButtonTiJianChaXun_Click"
                                                                                                Text="条件查询"  /></td>
                                                                        </tr>
                                                                </table>
                                                        </td>
                                                </tr>
                                        </table>
                                        <img  alt="图象"  height="5"  src="../../Images/funcLine.JPG"  width="933"  /><br  />
                                        <table>
                                                <tr>
                                                        <td>
                                                                <asp:Button  ID="ButtonChaKan"  runat="server"  Font-Size="12pt"  OnClick="ButtonTianJia_Click"
                                                                        Text="查看"  />
                                                        </td>
                                                </tr>
                                        </table>
                                        <asp:GridView  ID="GridViewXinXiLan"  runat="server"  AllowPaging="True" 
                                                CellPadding="4"  DataKeyNames="SN"  ForeColor="#333333"  GridLines="None"  OnPageIndexChanging="GridViewXinXiLan_PageIndexChanging">
                                                <FooterStyle  BackColor="#507CD1"  Font-Bold="True"  Font-Size="Small"  ForeColor="White"  />
                                                <RowStyle  BackColor="#EFF3FB"  />
                                                <EditRowStyle  BackColor="#2461BF"  />
                                                <SelectedRowStyle  BackColor="DodgerBlue"  Font-Bold="False"  ForeColor="#333333"  />
                                                <PagerStyle  BackColor="#2461BF"  ForeColor="White"  HorizontalAlign="Center"  />
                                                <HeaderStyle  BackColor="#507CD1"  Font-Bold="True"  Font-Size="10pt"  ForeColor="White"  />
                                                <AlternatingRowStyle  BackColor="White"  />
                                                <Columns>
                                                        <asp:TemplateField  HeaderText="选择"  ShowHeader="False">
                                                                <ItemTemplate>
                                                                        <asp:LinkButton  ID="LinkButton1"  runat="server"  CausesValidation="False"  CommandName="Select"
                                                                                Text="选择"></asp:LinkButton>
                                                                </ItemTemplate>
                                                                <ItemStyle  Wrap="False"  />
                                                        </asp:TemplateField>
                                                        <asp:BoundField  DataField="建立日期"  HeaderText="建立日期">
                                                                <ItemStyle  Wrap="False"  />
                                                                <HeaderStyle  Wrap="False"  />
                                                        </asp:BoundField>
                                                        <asp:BoundField  DataField="员工编号"  HeaderText="员工编号">
                                                                <ItemStyle  Wrap="False"  />
                                                                <HeaderStyle  Wrap="False"  />
                                                        </asp:BoundField>
                                                        <asp:BoundField  DataField="姓名"  HeaderText="姓名"  NullDisplayText="  ">
                                                                <ItemStyle  Wrap="False"  />
                                                                <HeaderStyle  Wrap="False"  />
                                                        </asp:BoundField>
                                                        <asp:BoundField  DataField="性别"  HeaderText="性别">
                                                                <ItemStyle  Wrap="False"  />
                                                                <HeaderStyle  Wrap="False"  />
                                                        </asp:BoundField>
                                                        <asp:BoundField  DataField="民族"  HeaderText="民族"  NullDisplayText="  ">
                                                                <ItemStyle  Wrap="False"  />
                                                                <HeaderStyle  Wrap="False"  />
                                                        </asp:BoundField>
                                                        <asp:BoundField  DataField="籍贯"  HeaderText="籍贯"  NullDisplayText="  ">
                                                                <HeaderStyle  Wrap="False"  />
                                                        </asp:BoundField>
                                                        <asp:BoundField  DataField="出生日期"  HeaderText="出生日期">
                                                                <HeaderStyle  Wrap="False"  />
                                                        </asp:BoundField>
                                                        <asp:BoundField  DataField="政治面貌"  HeaderText="政治面貌">
                                                                <HeaderStyle  Wrap="False"  />
                                                        </asp:BoundField>
                                                        <asp:BoundField  DataField="婚姻状况"  HeaderText="婚姻状况"  />
                                                        <asp:BoundField  DataField="学历"  HeaderText="学历"  />
                                                        <asp:BoundField  DataField="户口"  HeaderText="户口"  />
                                                        <asp:BoundField  DataField="身份证号"  HeaderText="身份证号"  />
                                                        <asp:BoundField  DataField="职称"  HeaderText="职称"  />
                                                        <asp:BoundField  DataField="所属部门"  HeaderText="所属部门"  />
                                                        <asp:BoundField  DataField="岗位"  HeaderText="岗位"  />
                                                        <asp:BoundField  DataField="入厂时间"  HeaderText="入厂时间"  />
                                                        <asp:BoundField  DataField="转正时间"  HeaderText="转正时间"  />
                                                        <asp:BoundField  DataField="合同到期"  HeaderText="合同到期"  />
                                                        <asp:BoundField  DataField="用工形式"  HeaderText="用工形式"  />
                                                        <asp:BoundField  DataField="档案资料"  HeaderText="档案资料"  />
                                                        <asp:BoundField  DataField="工作状态"  HeaderText="工作状态"  />
                                                        <asp:BoundField  DataField="是否涉密"  HeaderText="是否涉密"  />
                                                        <asp:BoundField  DataField="薪金"  HeaderText="薪金"  />
                                                        <asp:BoundField  DataField="电话"  HeaderText="电话"  />
                                                        <asp:BoundField  DataField="手机"  HeaderText="手机"  />
                                                        <asp:BoundField  DataField="电子邮件"  HeaderText="电子邮件"  />
                                                        <asp:BoundField  DataField="QQ号"  HeaderText="QQ号"  />
                                                        <asp:BoundField  DataField="经办人"  HeaderText="经办人"  />
                                                        <asp:BoundField  DataField="SN"  HeaderText="SN"  Visible="False"  />
                                                </Columns>
                                                <PagerSettings  Visible="False"  />
                                        </asp:GridView>
                                        &nbsp;
                                        <table>
                                                <tr>
                                                        <td>
                                                                &nbsp;<asp:LinkButton  ID="lnkbtnFirst"  runat="server"  OnClick="PagerButtonClick">首页</asp:LinkButton>
                                                                <asp:LinkButton  ID="lnkbtnPre"  runat="server"  OnClick="PagerButtonClick">上一页</asp:LinkButton>
                                                                <asp:LinkButton  ID="lnkbtnNext"  runat="server"  OnClick="PagerButtonClick">下一页</asp:LinkButton>
                                                                <asp:LinkButton  ID="lnkbtnLast"  runat="server"  OnClick="PagerButtonClick">尾页</asp:LinkButton>
                                                                <asp:Label  ID="lblCurrentIndex"  runat="server"  Text="第?页"></asp:Label>
                                                                <asp:Label  ID="lblPageCount"  runat="server"  Text="共?页"></asp:Label>
                                                                <asp:Label  ID="lblRecordCount"  runat="server"  Text="记录数"></asp:Label>
                                                                <asp:TextBox  ID="txtJumpPage"  runat="server"  Width="24px">1</asp:TextBox>
                                                                <asp:LinkButton  ID="lnkbtnJumpPage"  runat="server"  OnClick="lnkbtnJumpPage_Click">跳转</asp:LinkButton></td>
                                                </tr>
                                        </table>
                                </asp:View>
                                <asp:View  ID="ViewXiangXiXinXi"  runat="server">
                                        <asp:GridView  ID="GridViewTianJia"  runat="server"    CellPadding="4"
                                                ForeColor="#333333"  OnRowDataBound="GridViewTianJia_RowDataBound"  OnRowCancelingEdit="GridViewTianJia_RowCancelingEdit"  OnRowDeleting="GridViewTianJia_RowDeleting"  OnRowEditing="GridViewTianJia_RowEditing"  DataKeyNames="SN"  OnRowUpdating="GridViewTianJia_RowUpdating">
                                                <FooterStyle  BackColor="#507CD1"  Font-Bold="True"  ForeColor="White"  />
                                                <Columns>
                                                        <asp:BoundField  DataField="ID"  HeaderText="ID"  Visible="False"  >
                                                                <ItemStyle  Wrap="False"  />
                                                        </asp:BoundField>
                                                        <asp:BoundField  DataField="建立日期"  HeaderText="建立日期"  ReadOnly="True">
                                                                <ItemStyle  Wrap="False"  />
                                                        </asp:BoundField>
                                                        <asp:TemplateField  HeaderText="姓名">
                                                                <EditItemTemplate>
                                                                        <asp:TextBox  ID="TextBoxXinMing"  runat="server"  Text='<%#  Bind("姓名")  %>'  Width="60"></asp:TextBox>
                                                                </EditItemTemplate>
                                                                <ItemStyle  Wrap="False"  />
                                                                <ItemTemplate>
                                                                        <asp:Label  ID="Label2"  runat="server"  Text='<%#  Bind("姓名")  %>'></asp:Label>
                                                                </ItemTemplate>
                                                        </asp:TemplateField>
                                                        <asp:TemplateField  HeaderText="类型">
                                                                <ItemStyle  Wrap="False"  />
                                                                <ItemTemplate>
                                                                        <asp:HiddenField  ID="HDLeiXing"  runat="server"  Value='<%#  eval_r("类型")  %>'  />
                                                                        <asp:DropDownList  ID="DropDownListLeiXing"  runat="server"  Width="60">
                                                                        </asp:DropDownList>
                                                                </ItemTemplate>
                                                        </asp:TemplateField>
                                                        <asp:TemplateField  HeaderText="名称">
                                                                <EditItemTemplate>
                                                                        <asp:TextBox  ID="TextBoxMingCheng"  runat="server"  Text='<%#  Bind("名称")  %>'  Width="60"></asp:TextBox>
                                                                </EditItemTemplate>
                                                                <ItemStyle  Wrap="False"  />
                                                                <ItemTemplate>
                                                                        <asp:Label  ID="Label3"  runat="server"  Text='<%#  Bind("名称")  %>'></asp:Label>
                                                                </ItemTemplate>
                                                        </asp:TemplateField>
                                                        <asp:TemplateField  HeaderText="开始时间">
                                                                <EditItemTemplate>
                                                                        <asp:TextBox  ID="TextBoxKaiShiShiJian"  runat="server"  Text='<%#  Bind("开始时间")  %>'  Width="70"  CausesValidation="True"  ValidationGroup="vgpKaiShiShiJian"></asp:TextBox>
                                                                        <asp:RequiredFieldValidator  ID="reqFromDate"  runat="server"  ControlToValidate="TextBoxKaiShiShiJian"  ErrorMessage="请输入开始时间"  ValidationGroup="vgpKaiShiShiJian"  Display="Dynamic"    />
                                                                        <asp:CustomValidator  ID="CVKaiShiShiJian"  runat="server"  ErrorMessage="日期格式错误()"  ControlToValidate="TextBoxKaiShiShiJian"  Display="Dynamic"  OnServerValidate="CVKaiShiShiJian_ServerValidate"></asp:CustomValidator>
                                                                </EditItemTemplate>
                                                                <ItemStyle  Wrap="False"  />
                                                                <ItemTemplate>
                                                                        <asp:Label  ID="Label4"  runat="server"  Text='<%#  Bind("开始时间")  %>'></asp:Label>
                                                                </ItemTemplate>
                                                        </asp:TemplateField>
                                                        <asp:TemplateField  HeaderText="结束时间">
                                                                <EditItemTemplate>
                                                                        <asp:TextBox  ID="TextBoxJieShuShiJian"  runat="server"  Text='<%#  Bind("结束时间")  %>'  Width="70"></asp:TextBox>
                                                                </EditItemTemplate>
                                                                <ItemStyle  Wrap="False"  />
                                                                <ItemTemplate>
                                                                        <asp:Label  ID="Label5"  runat="server"  Text='<%#  Bind("结束时间")  %>'></asp:Label>
                                                                </ItemTemplate>
                                                        </asp:TemplateField>
                                                        <asp:TemplateField  HeaderText="取得证书">
                                                                <EditItemTemplate>
                                                                        <asp:TextBox  ID="TextBoxZhengShu"  runat="server"  Text='<%#  Bind("取得证书")  %>'  Width="60"></asp:TextBox>
                                                                </EditItemTemplate>
                                                                <ItemStyle  Wrap="False"  />
                                                                <ItemTemplate>
                                                                        <asp:Label  ID="Label6"  runat="server"  Text='<%#  Bind("取得证书")  %>'></asp:Label>
                                                                </ItemTemplate>
                                                        </asp:TemplateField>
                                                        <asp:TemplateField  HeaderText="备注">
                                                                <EditItemTemplate>
                                                                        <asp:TextBox  ID="TextBoxBeiZhu"  runat="server"  Text='<%#  Bind("备注")  %>'  Width="60"></asp:TextBox>
                                                                </EditItemTemplate>
                                                                <ItemStyle  Wrap="False"  />
                                                                <ItemTemplate>
                                                                        <asp:Label  ID="Label7"  runat="server"  Text='<%#  Bind("备注")  %>'></asp:Label>
                                                                </ItemTemplate>
                                                        </asp:TemplateField>
                                                        <asp:BoundField  DataField="经办人"  HeaderText="经办人"  ReadOnly="True"  >
                                                                <ItemStyle  Wrap="False"  />
                                                        </asp:BoundField>
                                                        <asp:BoundField  DataField="SN"  HeaderText="SN"  Visible="False"  />
                                                        <asp:TemplateField  HeaderText="编辑"  ShowHeader="False">
                                                                <EditItemTemplate>
                                                                        <asp:LinkButton  ID="LinkButton1"  runat="server"  CausesValidation="True"  CommandName="Update"
                                                                                Text="更新"></asp:LinkButton>
                                                                        <asp:LinkButton  ID="LinkButton2"  runat="server"  CausesValidation="False"  CommandName="Cancel"
                                                                                Text="取消"></asp:LinkButton>
                                                                </EditItemTemplate>
                                                                <ItemStyle  Wrap="False"  />
                                                                <ItemTemplate>
                                                                        <asp:LinkButton  ID="LinkButton1"  runat="server"  CausesValidation="False"  CommandName="Edit"
                                                                                Text="编辑"></asp:LinkButton>&nbsp;
                                                                </ItemTemplate>
                                                        </asp:TemplateField>
                                                        <asp:TemplateField  HeaderText="操作"  ShowHeader="False">
                                                                <ItemStyle  Wrap="False"  />
                                                                <ItemTemplate>
                                                                        <asp:LinkButton  ID="LinkButton3"  runat="server"  CausesValidation="False"  CommandName="Delete"
                                                                                Text="删除"></asp:LinkButton>
                                                                        <asp:LinkButton  ID="LinkButtonTianJia"  runat="server"  Visible="False"  OnClick="LinkButtonTianJia_Click">添加保存</asp:LinkButton>
                                                                        <asp:LinkButton  ID="LinkButtonQuXiao"  runat="server"  OnClick="LinkButtonQuXiao_Click"
                                                                                Visible="False">取消</asp:LinkButton>
                                                                </ItemTemplate>
                                                        </asp:TemplateField>
                                                </Columns>
                                                <RowStyle  BackColor="#EFF3FB"  />
                                                <EditRowStyle  BackColor="#2461BF"  />
                                                <SelectedRowStyle  BackColor="#D1DDF1"  Font-Bold="True"  ForeColor="#333333"  />
                                                <PagerStyle  BackColor="#2461BF"  ForeColor="White"  HorizontalAlign="Center"  />
                                                <HeaderStyle  BackColor="#507CD1"  Font-Bold="True"  ForeColor="White"  />
                                                <AlternatingRowStyle  BackColor="White"  />
                                                <EmptyDataTemplate>
                                                        建立日期  姓名  类型  名称  开始时间  结束时间  取得证书  备注  经办人
                                                </EmptyDataTemplate>
                                        </asp:GridView>
                                        <asp:Button  ID="ButtonXinZen"  runat="server"  Text="新增"  OnClick="ButtonXinZen_Click"  />
                                        <asp:Button  ID="ButtonBaoCun"  runat="server"  OnClick="ButtonBaoCun_Click"  Text="保存"  Visible="False"  Enabled="False"  />
                                        <asp:Button  ID="ButtonQuXiao"  runat="server"  OnClick="ButtonQuXiao_Click"  Text="取消"  Visible="False"  Enabled="False"  />
                                        <asp:Label  ID="LabelZhuJian"  runat="server"  Text="隐藏主键"  Visible="False"></asp:Label></asp:View>
          </asp:MultiView>
          </div>
        </form>
</body>
</html>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值