文章出處
文章列表
作為一名.net小白,今天分享一下telerik知識的學習。熟悉ASP.NET Web Form的都知道Grid View或者是List View等表格控件,所以今天和大家分享一下telerik RadGrid的簡單應用。
和一般表格控件一樣,RadGrid有Datasource和一些點擊事件,一些屬性(例如分頁,編輯,刪除)
<telerik:RadGrid ID="RadGrid" runat="server" AllowPaging="true" AllowSorting="true" AllowCustomPaging="true" OnNeedDataSource="RadGrid_NeedDataSource" LoadingPanelID="RadAjaxLoadingPanel" AutoGenerateColumns="false" AllowAutomaticUpdates="True" AllowAutomaticDeletes="true" AllowMultiRowSelection="True"> <MasterTableView DataKeyNames="Account, Staff_ID, Manager_ID" NoMasterRecordsText="暫無數據"> <Columns> <telerik:GridClientSelectColumn UniqueName="ClientSelectColumn"> </telerik:GridClientSelectColumn> <telerik:GridBoundColumn UniqueName="Account" HeaderText="賬號" DataField="Account"> </telerik:GridBoundColumn> <telerik:GridBoundColumn UniqueName="Number" HeaderText="編號" DataField="Number"> </telerik:GridBoundColumn> <telerik:GridBoundColumn UniqueName="Name" HeaderText="姓名" DataField="Name"> </telerik:GridBoundColumn> <telerik:GridBoundColumn UniqueName="Staff_Name" HeaderText="員工" DataField="Staff_Name"> </telerik:GridBoundColumn> <telerik:GridBoundColumn UniqueName="Manager_Name" HeaderText="經理" DataField="Manager_Name"> </telerik:GridBoundColumn> <telerik:GridBoundColumn UniqueName="Department_Name" HeaderText="部門" DataField="Department_Name"> </telerik:GridBoundColumn> </Columns> </MasterTableView> <PagerStyle FirstPageToolTip="首頁" PrevPageToolTip="上一頁" NextPageToolTip="下一頁" LastPageToolTip="尾頁" PrevPagesToolTip="前10頁" NextPagesToolTip="后10頁" PageSizeLabelText="每頁顯示數" PagerTextFormat="{4}共 <strong>{5}</strong> 條數據" /> <ExportSettings Excel-Format="Biff" IgnorePaging="true" ExportOnlyData="true" OpenInNewWindow="true"></ExportSettings> <ClientSettings EnableRowHoverStyle="false"> <Selecting AllowRowSelect="true" UseClientSelectColumnOnly="true"></Selecting> </ClientSettings> </telerik:RadGrid>
DataKeyName:綁定字段值,可以綁定多個字段
OnNeedDataSource:數據源,用于綁定數據
DataField:對應數據庫字段也就是映射表里面的字段
PageStyle:設置可以配合linq來進行分頁
GridClientSelectColumn:設置表格單選
RadGrid后臺如何獲取字段的值(類似于GridView)
代碼如下:
1 private void GetSelectItems() 2 { 3 foreach (GridDataItem item in RadGrid.SelectedItems) 4 { 5 CustomerBatchChangeRecordVO record = new CustomerBatchChangeRecordVO(); 6 7 var editableItem = ((GridEditableItem)item); 8 var staffID = (int)editableItem.GetDataKeyValue("Staff_ID"); 9 var managerID = (int)editableItem.GetDataKeyValue("Manager_ID"); 10 11 record.Old_Staff_ID = staffID; 12 record.Old_Manager_ID = managerID; 13 record.Account = item["Account"].Text; 14 record.Account_Name = item["Name"].Text; 15 record.Customer_Number = item["Number"].Text; 16 record.Old_Manager_Name = item["Manager_Name"].Text; 17 record.Old_Staff_Name = item["Staff_Name"].Text; 18 record.Department_Name = item["Department_Name"].Text; 19 recordList.Add(record); 20 } 21 Session[Config.Seesion_CustomerBatchChangeRecord] = recordList; 22 23 }
獲取多行選中的值:
1 // 通過DataKeyValues獲取 2 foreach (GridDataItem item in rg.SelectedItems) 3 { 4 var value = item.GetDataKeyValue("Id"); 5 var value = rg.MasterTableView.DataKeyValues[item.ItemIndex]["Id"]; 6 } 7 8 foreach (string item in rg.SelectedIndexes) 9 { 10 var value = rg.MasterTableView.DataKeyValues[int.Parse(item)]["Id"]; 11 var value = rg.Items[int.Parse(item)].GetDataKeyValue("Id"); 12 } 13 14 // 通過ExtractValues獲取 15 foreach (GridDataItem item in rg.SelectedItems) 16 { 17 var dict = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); 18 item.ExtractValues(dict); 19 var value = dict["Id"]; 20 } 21 22 foreach (string item in rg.SelectedIndexes) 23 { 24 var dict = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); 25 rg.Items[int.Parse(item)].ExtractValues(dict); 26 var value = dict["Id"]; 27 } 28 29 // 通過TableCell獲取 30 foreach (GridDataItem item in rg.SelectedItems) 31 { 32 var value = item["idCol"].Text; // GridBoundColumn 33 var value = (item.FindControl("idText") as Literal).Text; // GridTemplateColumn 34 } 35 36 foreach (string item in rg.SelectedIndexes) 37 { 38 var value = rg.Items[int.Parse(item)]["idCol"].Text; // GridBoundColumn 39 var value = (rg.Items[int.Parse(item)].FindControl("idText") as Literal).Text; // GridTemplateColumn 40 41 }
初次涉及telerik RadGrid,請大家指教 O(∩_∩)O
文章列表
全站熱搜