后台幫定代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Datagrid();
}
private void Datagrid()
{
DataTable data = DB.FillDataTable_Pro("select top 40 * from bi_t_item_info");
this.ASPxGridView1.DataSource = data;
this.ASPxGridView1.DataBind();
}
//刪除事件
protected void ASPxGridView1_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e)
{
string str = e.Keys[0].ToString();
DB.ExecuteScalar(string.Format("delete bi_t_item_info where item_no='{0}'", str));
e.Cancel = true;
Datagrid();
}
}
前台代碼:
<dx:ASPxGridView ID="ASPxGridView1" runat="server" OnRowDeleting="ASPxGridView1_RowDeleting"
OnDataBound="ASPxGridView1_DataBound" KeyFieldName="item_no">
<Columns>
<dx:GridViewCommandColumn VisibleIndex="0" ShowSelectCheckbox="True">
<EditButton Visible="True" Text="編輯">
</EditButton>
<DeleteButton Visible="True" Text="刪除">
</DeleteButton>
</dx:GridViewCommandColumn>
<dx:GridViewDataTextColumn Caption="編號" FieldName="item_no" Name="item_no" VisibleIndex="0"
ReadOnly="true">
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn Caption="條碼" FieldName="barcode" Name="barcode" VisibleIndex="1"
ReadOnly="true">
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn Caption="單位" FieldName="unit_no" Name="unit_no" VisibleIndex="2"
ReadOnly="true">
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn Caption="名稱" FieldName="item_name" Name="item_name" VisibleIndex="3"
ReadOnly="false">
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn Caption="零售價" FieldName="sale_price" Name="sale_price"
VisibleIndex="4" ReadOnly="false">
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn Caption="備注" FieldName="item_other3" Name="sale_price"
VisibleIndex="4" ReadOnly="false">
</dx:GridViewDataTextColumn>
</Columns>
<SettingsBehavior AllowSelectByRowClick="True" ConfirmDelete="True" />
</dx:ASPxGridView>
要注意的是,有紅色標示地方,此處必須是主鍵,我就是因為這個地方沒有邦定主鍵,刪除事件不執行,查了好久才查出來!
對於ASPxGridView刪除按鈕的確認彈出框,只需要簡單設置幾個屬性即可。
1.首先自然要先啟用行的刪除功能。
2.然后設置ASPxGridView的SettingsBehavior的ConfirmDelete屬性為Ture,即啟用刪除功能的確認框。
3.設置彈出框內容文本。在ASPxGridView的SettingsText中設置ConfirmDelete屬性為”確定要刪除嗎?”即可。
幾個常用屬性
IsEditing : 是否處於編輯狀態
IsNewRowEditing : 是否是新建行的編輯狀態
GridLines="Vertical" : 網格樣式 Vertical, Both, None
ShowGroupPanel="True" : 分組面板
ShowFooter="True" : 腳注面板
ShowFilterRow="True" : 過濾器行
ShowHeaderFilterButton="true" : 表頭過濾按鈕
ShowGroupFooter="VisibleAlways" : 分組腳注面板 Hidden | VisibleIfExpand | VisibleAlways
ShowPreview="true" : 預覽面板
ShowVerticalScrollBar="True" : 垂直滾動條
VerticalScrollableHeight="250" : 垂直滾動條
SettingsBehavior
AllowDragDrop="False" : 允許托拽
ColumnResizeMode="Control" : 列寬度調整模式
AllowFocusedRow="True" : 鼠標點擊選擇行
PageSize="30" : 分頁大小
Mode="ShowAllRecords" : 展示模式
SEOFriendly="Enabled" : Search engine friendly
Position="TopAndBottom" : 分頁控件位置
SettingsText
Title="標題"
EmptyDataRow="無數據"
PopupEditFormCaption="編輯"
ConfirmDelete="確定刪除?"
編輯模式 SettingsEditing.Mode
EditForm : 當前行轉化為表單,嵌入在行中
EditFormAndDisplayRow : 同EditForm,但保留當前行
Inline : 在當前行現場編輯
PopupEditForm : 彈出窗口編輯
幾個常用方法
獲取單元格的值
decimal change = (decimal)grid.GetRowValues(e.VisibleIndex, "Change");
獲取模板中的控件
Label label = grid.FindRowCellTemplateControl(e.VisibleIndex, null, "changePercent") as Label;