• telerik:RadGrid 学习笔记1 (增删查改)


    <fieldset style=" 97%">
    
                <telerik:RadGrid ID="RadGrid1" runat="server" GridLines="None"
                    AllowPaging="True" PageSize="20" AllowSorting="True" Width="100%" AutoGenerateColumns="False"
                    EnableAJAX="True" ShowStatusBar="True" CellSpacing="0" Skin="Web20" 
                    OnUpdateCommand="RadGrid1_UpdateCommand" OnInsertCommand="RadGrid1_InsertCommand"
                    OnDeleteCommand="RadGrid1_DeleteCommand" OnNeedDataSource="RadGrid1_NeedDataSource">
                    <SortingSettings SortedAscToolTip="顺序排序" SortedDescToolTip="倒序排序" SortToolTip="点击排序" />
                    <PagerStyle Mode="NextPrevAndNumeric" FirstPageToolTip="第一页" LastPageToolTip="最后一页"  NextPageToolTip="下一页" PrevPageToolTip="上一页" PageSizeLabelText="每页显示:" ShowPagerText="False"  />
                    <MasterTableView CommandItemDisplay="Top" GridLines="None" DataKeyNames="序号" EditMode="InPlace" NoMasterRecordsText="没有您要查询的数据">
                    <CommandItemSettings  AddNewRecordText="新增机柜" RefreshText="刷新">
                        </CommandItemSettings>
                        <Columns>
                            <telerik:GridTemplateColumn HeaderText="机柜标识" UniqueName="机柜标识" SortExpression="机柜标识">
                                <ItemTemplate>
                                    <asp:Label runat="server" ID="lbljgbs" Text='<%# Eval("机柜标识") %>'></asp:Label></ItemTemplate>
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtjgbs" runat="server" Text='<%# Eval("机柜标识") %>' Width="70%"></asp:TextBox></EditItemTemplate>
                            </telerik:GridTemplateColumn>
                            <telerik:GridTemplateColumn HeaderText="规格" UniqueName="规格" SortExpression="规格">
                                <ItemTemplate>
                                    <asp:Label runat="server" ID="lbljgg" Text='<%# Eval("规格") %>'></asp:Label></ItemTemplate>
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtgg" runat="server" Text='<%# Eval("规格") %>' Width="70%"></asp:TextBox></EditItemTemplate>
                            </telerik:GridTemplateColumn>
                            <telerik:GridTemplateColumn HeaderText="备注" UniqueName="备注">
                                <ItemTemplate>
                                    <asp:Label runat="server" ID="lbljbzz" Text='<%# Eval("备注") %>'></asp:Label></ItemTemplate>
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtbz" runat="server" Text='<%# Eval("备注") %>' TextMode="MultiLine" Width="70%"></asp:TextBox></EditItemTemplate>
                            </telerik:GridTemplateColumn>
                            
                           <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn" HeaderText="修改"
                                EditText="修改">
                                <HeaderStyle Width="60px" HorizontalAlign="Center"></HeaderStyle>
                                <ItemStyle HorizontalAlign="Center" />
                            </telerik:GridEditCommandColumn>
    
                            <telerik:GridClientDeleteColumn ConfirmTextFields="机柜标识" ConfirmTextFormatString="您确定删除 {0} ? "
                                ButtonType="ImageButton" HeaderText="删除" Text="删除">
                                <HeaderStyle Width="40px" HorizontalAlign="Center"></HeaderStyle>
                                <ItemStyle HorizontalAlign="Center" />
                            </telerik:GridClientDeleteColumn>
    
                        </Columns>
                        
                        
                    </MasterTableView>
                    <ClientSettings AllowColumnsReorder="True" ReorderColumnsOnClient="True" EnableRowHoverStyle="true">
                        <Selecting AllowRowSelect="True" />
                        <ClientEvents OnRowDblClick="RowDblClick" />
                        <Resizing EnableRealTimeResize="True" ResizeGridOnColumnResize="True" AllowColumnResize="True">
                        </Resizing>
                    </ClientSettings>
                </telerik:RadGrid>
    
        </fieldset>
     1 <fieldset style=" 97%">
     2       <telerik:RadAjaxPanel runat="server" ID="p1" Width="100%">
     3             <telerik:RadGrid ID="RadGrid1" runat="server" GridLines="None"
     4                 AllowPaging="True" PageSize="1" AllowSorting="True" Width="100%" AutoGenerateColumns="False"
     5                 EnableAJAX="True" ShowStatusBar="True" CellSpacing="0" Skin="Web20" 
     6                 OnUpdateCommand="RadGrid1_UpdateCommand" OnInsertCommand="RadGrid1_InsertCommand"
     7                 OnDeleteCommand="RadGrid1_DeleteCommand" OnNeedDataSource="RadGrid1_NeedDataSource">
     8                 <PagerStyle Mode="NextPrevAndNumeric"></PagerStyle>
     9                 <MasterTableView CommandItemDisplay="Top" GridLines="None" DataKeyNames="序号" EditMode="InPlace" NoMasterRecordsText="没有您要查询的数据">
    10                 <PagerStyle FirstPageToolTip="第一页" LastPageToolTip="最后一页"  NextPageToolTip="下一页" PrevPageToolTip="上一页" PageSizeLabelText="每页显示:" ShowPagerText="False" />
    11                 <CommandItemSettings ExportToPdfText="导出PDF" AddNewRecordText="添加新项" RefreshText="刷新">
    12                     </CommandItemSettings>
    13                     <Columns>
    14                         <telerik:GridTemplateColumn HeaderText="机柜标识" UniqueName="机柜标识" SortExpression="机柜标识">
    15                             <ItemTemplate>
    16                                 <asp:Label runat="server" ID="lbljgbs" Text='<%# Eval("机柜标识") %>'></asp:Label></ItemTemplate>
    17                             <EditItemTemplate>
    18                                 <asp:TextBox ID="txtjgbs" runat="server" Text='<%# Eval("机柜标识") %>' Width="70%"></asp:TextBox></EditItemTemplate>
    19                         </telerik:GridTemplateColumn>
    20                         <telerik:GridTemplateColumn HeaderText="规格" UniqueName="规格" SortExpression="规格">
    21                             <ItemTemplate>
    22                                 <asp:Label runat="server" ID="lbljgg" Text='<%# Eval("规格") %>'></asp:Label></ItemTemplate>
    23                             <EditItemTemplate>
    24                                 <asp:TextBox ID="txtgg" runat="server" Text='<%# Eval("规格") %>' Width="70%"></asp:TextBox></EditItemTemplate>
    25                         </telerik:GridTemplateColumn>
    26                         <telerik:GridTemplateColumn HeaderText="备注" UniqueName="备注">
    27                             <ItemTemplate>
    28                                 <asp:Label runat="server" ID="lbljbzz" Text='<%# Eval("备注") %>'></asp:Label></ItemTemplate>
    29                             <EditItemTemplate>
    30                                 <asp:TextBox ID="txtbz" runat="server" Text='<%# Eval("备注") %>' TextMode="MultiLine" Width="70%"></asp:TextBox></EditItemTemplate>
    31                         </telerik:GridTemplateColumn>
    32                         
    33                        <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn" HeaderText="修改"
    34                             EditText="修改">
    35                             <HeaderStyle Width="60px" HorizontalAlign="Center"></HeaderStyle>
    36                             <ItemStyle HorizontalAlign="Center" />
    37                         </telerik:GridEditCommandColumn>
    38 
    39                         <telerik:GridClientDeleteColumn ConfirmTextFields="机柜标识" ConfirmTextFormatString="您确定删除 {0} ? "
    40                             ButtonType="ImageButton" HeaderText="删除" Text="删除">
    41                             <HeaderStyle Width="40px" HorizontalAlign="Center"></HeaderStyle>
    42                             <ItemStyle HorizontalAlign="Center" />
    43                         </telerik:GridClientDeleteColumn>
    44 
    45                     </Columns>
    46                     
    47                     
    48                 </MasterTableView>
    49                 <ClientSettings AllowColumnsReorder="True" ReorderColumnsOnClient="True" EnableRowHoverStyle="true">
    50                     <Selecting AllowRowSelect="True" />
    51                     <ClientEvents OnRowDblClick="RowDblClick" />
    52                     <Resizing EnableRealTimeResize="True" ResizeGridOnColumnResize="True" AllowColumnResize="True">
    53                     </Resizing>
    54                 </ClientSettings>
    55             </telerik:RadGrid>
    56         </telerik:RadAjaxPanel>
    57     </fieldset>
    CS Code
     1 protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
     2         {
     3             try
     4             {
     5                 IList<机柜表> jgb = BLL.机柜类.根据区域ID查询所有机柜信息(73);
     6                 RadGrid1.DataSource = jgb;
     7                 //RadGrid1.DataSource = ddal.GetAll();
     8             }
     9             catch (Exception ex)
    10             {
    11                 Console.WriteLine(ex.Message);
    12             }
    13 
    14         }
    15         protected void RadGrid1_DeleteCommand(object source, GridCommandEventArgs e)
    16         {
    17             //Get the GridDataItem of the RadGrid    
    18             
    19             //Get the primary key value using the DataKeyValue.    
    20             //int xuhao = Convert.ToInt32(item.OwnerTableView.DataKeyValues[item.ItemIndex]["序号"]);
    21             try
    22             {
    23                 GridDataItem dataItem = (GridDataItem)e.Item;
    24                 int xuhao = Convert.ToInt32(dataItem.GetDataKeyValue("序号").ToString());
    25                 BLL.机柜类.根据序号删除机柜(xuhao);
    26             }
    27             catch (Exception ex)
    28             {
    29                 RadGrid1.Controls.Add(new LiteralControl("Unable to delete . Reason: " + ex.Message));
    30                 e.Canceled = true;
    31             }
    32 
    33         }
    34         protected void RadGrid1_UpdateCommand(object source, GridCommandEventArgs e)
    35         {
    36             //Get the GridEditableItem of the RadGrid    
    37             //GridEditableItem editedItem = e.Item as GridEditableItem;
    38             //Get the primary key value using the DataKeyValue.    
    39            // int xuhao = Convert.ToInt32(editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["序号"]);
    40             //Access the textbox from the edit form template and store the values in string variables.    
    41 
    42             GridDataItem dataItem = (GridDataItem)e.Item;
    43 
    44            
    45             try
    46             {
    47                 int xuhao = Convert.ToInt32(dataItem.GetDataKeyValue("序号").ToString());
    48                 string jgbs = (dataItem.FindControl("txtjgbs") as TextBox).Text;
    49                 string gg = (dataItem.FindControl("txtgg") as TextBox).Text;
    50                 string bz = (dataItem.FindControl("txtbz") as TextBox).Text;
    51 
    52                 机柜表 jgb = new 机柜表();
    53                 jgb.序号 = xuhao;
    54                 jgb.备注 = bz;
    55                 jgb.规格 = gg;
    56                 jgb.机柜标识 = jgbs;
    57                 jgb.所在区域序号 = 73;
    58                 BLL.机柜类.修改机柜信息(jgb);
    59             }
    60             catch (Exception ex)
    61             {
    62                 RadGrid1.Controls.Add(new LiteralControl("Unable to update . Reason: " + ex.Message));
    63                 e.Canceled = true;
    64             }
    65 
    66         }
    67         protected void RadGrid1_InsertCommand(object source, GridCommandEventArgs e)
    68         {
    69             //Get the GridEditFormInsertItem of the RadGrid    
    70             //GridEditFormInsertItem dataItem = (GridEditFormInsertItem)e.Item;
    71 
    72             //string EmployeeID  = (insertedItem["EmployeeID"].Controls[0] as TextBox).Text;  
    73 
    74 
    75             try
    76             {
    77                 GridDataItem dataItem = (GridDataItem)e.Item;
    78                 string jgbs = (dataItem.FindControl("txtjgbs") as TextBox).Text;
    79                 string gg = (dataItem.FindControl("txtgg") as TextBox).Text;
    80                 string bz = (dataItem.FindControl("txtbz") as TextBox).Text;
    81                 机柜表 jgb = new 机柜表();
    82                 jgb.备注 = bz;
    83                 jgb.规格 = gg;
    84                 jgb.机柜标识 = jgbs;
    85                 jgb.所在区域序号 = 73;
    86 
    87                 BLL.机柜类.新增机柜(jgb);
    88 
    89 
    90             }
    91             catch (Exception ex)
    92             {
    93                 RadGrid1.Controls.Add(new LiteralControl("Unable to insert . Reason: " + ex.Message));
    94                 e.Canceled = true;
    95             }
    96 
    97         }

    效果预览

  • 相关阅读:
    Mongo简单查询总结
    将对象转换成Dictionary 字典
    C#调用NPOI组件导出Excel表格
    Lambda中的一些方法的总结
    LinQ总结
    简单的爬虫 一
    Python 中的注释规范
    在VM上配置一个能上网的网络设置
    Python 中新式类的内置方法
    Python 中的locals()
  • 原文地址:https://www.cnblogs.com/zengxiangjian/p/2681386.html
Copyright © 2020-2023  润新知