博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
(转载)DevExpress ASPxGridView 使用文档三:编辑
阅读量:5154 次
发布时间:2019-06-13

本文共 7232 字,大约阅读时间需要 24 分钟。

转载请注明出处:

---------------------------------------------------------

-- ASPxGridView 编辑
-- 注意:想让GridView支持修改功能,必须指定KeyFieldName
---------------------------------------------------------
展示编辑按钮列
    <dxwgv:GridViewCommandColumn VisibleIndex="0">
        <EditButton   Visible="True" Text="修改" />
        <DeleteButton Visible="true" Text="删除" />
        <NewButton    Visible="True" Text="新建" />
        <CancelButton Visible="true" Text="取消" />
        <UpdateButton Visible="true" Text="保存" />
        <ClearFilterButton Visible="true" Text="取消过滤" />
    </dxwgv:GridViewCommandColumn>

编辑视图下控件的外观设置

    <dx:GridViewDataMemoColumn ...  PropertiesMemoEdit-Rows="4" EditFormSettings-ColumnSpan="2"  />
    或者
    <dxwgv:GridViewDataMemoColumn FieldName="Notes" Visible="False">
        <EditFormSettings RowSpan="4" ColumnSpan="2" />
    </dxwgv:GridViewDataMemoColumn>

其它

    多行编辑请参考: 
    编辑表单模板,请参考《ASPxGridView.Templates》

示例一: 绑定到IList并实现删除和修改
    <dx:ASPxGridView runat="server" ID="gvImages" KeyFieldName="ImageId" 
        OnRowDeleting="gvImages_RowDeleting" OnRowUpdating="gvImages_RowUpdating"
        >
        <SettingsEditing Mode="Inline" />
        <SettingsBehavior ConfirmDelete="true" />
        <Columns>
            <dx:GridViewDataColumn FieldName="ImageId" Caption="ID" Visible="false" />
            <dx:GridViewDataImageColumn FieldName="ThumbnailUrl" Caption="缩略图" PropertiesImage-DescriptionUrlField="ImageId" />
            <dx:GridViewDataTextColumn FieldName="Description" Caption="说明" />
            <dx:GridViewDataTextColumn FieldName="Keywords" Caption="关键字" />
            <dx:GridViewCommandColumn Caption="编辑" >
                <EditButton Visible="True" Text="修改" />
                <DeleteButton Visible="True" Text="删除" />
                <CancelButton Text="取消" />
                <UpdateButton Text="保存" />
            </dx:GridViewCommandColumn>
        </Columns>
    </dx:ASPxGridView>
    // 展示
    public void ShowArticleImages()
    {
        int articleId = Convert.ToInt32(Request.QueryString["articleId"]);
        IList<BlogArticleImage> images = BlogArticleImage.ListArticleImages(articleId);
        this.gvImages.KeyFieldName = "ImageId";
        this.gvImages.DataSource = images;
        this.gvImages.DataBind();
    }
    // 更新
    protected void gvImages_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
    {
        int id = Convert.ToInt32(e.Keys[0]);
        string descript = Convert.ToString(e.NewValues["Description"]);
        string keywords = Convert.ToString(e.NewValues["Keywords"]);
        BlogArticleImage image = BlogArticleImage.Retrieve(id);
        if (image != null)
        {
            image.Description = descript;
            image.Keywords = keywords;
            image.Persist();
        }

        e.Cancel = true;

        gvImages.CancelEdit();
        ShowArticleImages();
    }
    // 删除
    protected void gvImages_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e)
    {
        int id = Convert.ToInt32(e.Keys[0]);
        BlogArticleImage image = BlogArticleImage.Retrieve(id);
        if (image != null)
        {
            System.IO.File.Delete(Server.MapPath(image.ImageUrl));
            System.IO.File.Delete(Server.MapPath(image.ThumbnailUrl));
            image.Remove();
        }

        e.Cancel = true;

        gvImages.CancelEdit();
        ShowArticleImages();
    }

示例二: 控件访问权限控制及CRUD操作

    <dx:ASPxGridView runat="server" ID="gv" Caption="基础网" KeyFieldName="ID" Width="3000"
        OnRowDeleting="gv_RowDeleting" OnRowUpdating="gv_RowUpdating" OnRowInserting="gv_RowInserting"
        OnInitNewRow="grid_InitNewRow" onhtmlrowcreated="gv_HtmlRowCreated"  OnCellEditorInitialize="grid_CellEditorInitialize"
        >
        <Columns>
            <dx:GridViewCommandColumn Caption="编辑" Width="60" >
                <EditButton   Visible="True" Text="修改" />
                <DeleteButton Visible="true" Text="删除" />
                <NewButton    Visible="True" Text="新建" />
                <CancelButton Visible="true" Text="取消" />
                <UpdateButton Visible="true" Text="保存" />
            </dx:GridViewCommandColumn>
            <dx:GridViewDataColumn FieldName="ID" Caption="ID"/>
            <dx:GridViewDataColumn FieldName="CREATE_DT" Caption="创建时间"/>
            <dx:GridViewDataColumn FieldName="CREATOR" Caption="创建人"/>
            <dx:GridViewDataComboBoxColumn FieldName="UNIT" Caption="处理单位" >
                <PropertiesComboBox EnableSynchronization="False" EnableIncrementalFiltering="True"  DropDownStyle="DropDown" />
            </dx:GridViewDataComboBoxColumn>
            <dx:GridViewDataColumn FieldName="RESULT" Caption="处理结果"  />
            <dx:GridViewDataMemoColumn FieldName="ED_CMT" Caption="政企部核对结果"  PropertiesMemoEdit-Rows="4" EditFormSettings-ColumnSpan="2"  />
        </Columns>
    </dx:ASPxGridView>
    // 普通状态下的命令按钮显隐
    protected void gv_HtmlRowCreated(object sender, ASPxGridViewTableRowEventArgs e)
    {
        if (!gv.IsEditing && e.RowType == DevExpress.Web.ASPxGridView.GridViewRowType.Data)
        {
            bool isAdmin = Common.IsInRoles(new string[] { WZWF.DAL.Roles.Admin });
            string unit = gv.GetRowValues(e.VisibleIndex, "UNIT").ToString();
            bool isAuth = Common.IsInRoles(
                unit,
                WZWF.DAL.Roles.EnterpriseDepartment,
                WZWF.DAL.Roles.MaintainDepartment
                );

            // 修改按钮

            WebControl btnModify = e.Row.Cells[0].Controls[0] as WebControl;
            btnModify.Visible = isAuth;

            // 新建按钮

            WebControl btnAddNew = e.Row.Cells[0].Controls[1] as WebControl;
            btnAddNew.Visible = isAdmin;

            // 删除按钮

            WebControl btnDelete = e.Row.Cells[0].Controls[2] as WebControl;
            btnDelete.Visible = isAdmin;
        }
    }
    // 编辑状态下的控件访问权限控制
    // 管理员:unit, result, md_cmt, ed_cmt
    // 政企部:ed_cmt
    // 网络维护部:result, md_cmt
    protected void grid_CellEditorInitialize(object sender, ASPxGridViewEditorEventArgs e)
    {
        if (gv.IsEditing)
        {
            // 受理单位下拉框
            if (e.Column.FieldName == "UNIT")
            {
                ASPxComboBox combo = e.Editor as ASPxComboBox;
                using (DbClean db = new DbClean())
                {
                    combo.DataSource = db.ListUnits();
                    combo.TextField = "ROLE";
                    combo.ValueField = "ROLE";
                    combo.DataBind();
                }
            }

            // 新建状态除了id以外都可以编辑

            if (gv.IsNewRowEditing)
            {
                e.Editor.ReadOnly = (e.Column.FieldName == "ID");
            }
            // 修改状态下根据角色可编辑部分数据
            else
            {
                e.Editor.ReadOnly = true;
                if (e.Column.FieldName == "UNIT")
                    e.Editor.ReadOnly = !Common.IsInRoles(WZWF.DAL.Roles.Admin);
                else if (e.Column.FieldName == "RESULT" || e.Column.FieldName == "MD_CMT")
                    e.Editor.ReadOnly = !Common.IsInRoles(WZWF.DAL.Roles.Admin, WZWF.DAL.Roles.MaintainDepartment);
                else if (e.Column.FieldName == "ED_CMT")
                    e.Editor.ReadOnly = !Common.IsInRoles(WZWF.DAL.Roles.Admin, WZWF.DAL.Roles.EnterpriseDepartment);
            }
        }

        // 可编辑控件设置背景色

        e.Editor.BackColor = e.Editor.ReadOnly ? Color.White : Color.LightYellow;
    }
    // 删除
    protected void gv_RowDeleting(object sender, ASPxDataDeletingEventArgs e)
    {
        int id = Convert.ToInt32(e.Keys[0]);
        using (DbClean db = new DbClean())
            db.DelBasicNet(id);

        e.Cancel = true;

        gv.CancelEdit();
        ShowData(ViewState["Unit"].ToString());
    }
    // 更新
    protected void gv_RowUpdating(object sender, ASPxDataUpdatingEventArgs e)
    {
        string unit = Convert.ToString(e.NewValues["UNIT"]);
        string result = Convert.ToString(e.NewValues["RESULT"]);
        string edCmt = Convert.ToString(e.NewValues["ED_CMT"]);
        if (gv.IsEditing)
        {
            int id = Convert.ToInt32(e.Keys[0]);
            using (DbClean db = new DbClean())
                db.ModBasicNet(id, ...);
        }
        e.Cancel = true;
        gv.CancelEdit();
        ShowData(ViewState["Unit"].ToString());
    }

    // 新增

    protected void grid_InitNewRow(object sender, ASPxDataInitNewRowEventArgs e)
    {
        e.NewValues["CREATOR"] = Page.User.Identity.Name;
        e.NewValues["CREATE_DT"] = System.DateTime.Now;
    }
    protected void gv_RowInserting(object sender, ASPxDataInsertingEventArgs e)
    {
        string unit = Convert.ToString(e.NewValues["UNIT"]);
        string result = Convert.ToString(e.NewValues["RESULT"]);
        string edCmt = Convert.ToString(e.NewValues["ED_CMT"]);
        if (gv.IsNewRowEditing)
        {
            using (DbClean db = new DbClean())
                db.AddBasicNet(.....);
        }
        e.Cancel = true;
        gv.CancelEdit();
        ShowData(ViewState["Unit"].ToString());
    }

转载于:https://www.cnblogs.com/goole/archive/2011/04/19/2021155.html

你可能感兴趣的文章
php 小坑记录
查看>>
2018.7.28 二叉树的遍历规则(前序遍历、后序遍历、中序遍历)
查看>>
通过 poi 导入 Excel代码
查看>>
《CSS基础教程》 读书笔记三
查看>>
洛谷P4482 [BJWC2018]Border 的四种求法 字符串,SAM,线段树合并,线段树,树链剖分,DSU on Tree...
查看>>
PHP安全新闻早8点_1127
查看>>
57.Insert Interval
查看>>
PHP 五大运行模式
查看>>
CSS选项卡
查看>>
HDOJ1203 I NEED A OFFER!
查看>>
ZH奶酪:自然语言处理工具LTP语言云调用方法
查看>>
.NET中将图片文件流转成Base64字符串的实现
查看>>
js如何操作或是更改sass里的变量
查看>>
BZOJ1419: Red is good
查看>>
腾讯云-搭建 JAVA 开发环境
查看>>
POJ 3308 Paratroopers (对数转换+最小点权覆盖)
查看>>
rendering omni shadow in one pass.
查看>>
No repository found containing,eclipse 自动更新erro 解决
查看>>
iOS设计模式之单例模式
查看>>
MySQL面试题中:主从同步的原理
查看>>