简体   繁体   English

如何将验证添加到GridView字段?

[英]How do I add validation to a GridView field?

I have gridview as follows: 我有如下的gridview:

<asp:GridView runat="server" ID="gvOverrideData" AutoGenerateColumns="false" AlternatingRowStyle-BackColor="LightGreen" Width="800" OnRowEditing="OverrideGrid_OnRowEditing" OnRowCancelingEdit="OverrideGrid_OnRowCancelingEdit" OnRowUpdating="OverrideGrid_RowUpdating">
    <HeaderStyle BackColor="LightGray" />
    <Columns>
        <asp:TemplateField HeaderText="Path">
            <ItemTemplate>
                <%# GetOverrideTemplatePath(DataBinder.Eval(Container.DataItem, "Path").ToString())
                %>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="FileName" HeaderText="File Name" />
        <asp:BoundField DataField="Extension" HeaderText="File Extension" />
        <asp:BoundField DataField="FileType" HeaderText="File Type" />
        <asp:BoundField DataField="Iteration" HeaderText="Iteration" />
        <asp:CommandField ButtonType="Link" ShowEditButton="true" ShowDeleteButton="true"
        ShowCancelButton="true" />
    </Columns>
</asp:GridView>

I would like to validate the FileType field, so that it only accepts an InDesign, XML, CorelDraw, StaticImage file type, where the extension should be one of 我想验证FileType字段,以便它仅接受InDesign,XML,CorelDraw,StaticImage文件类型,其中扩展名应为以下类型之一

.indd, .tif, .wmf, .idms, .eps, .pdf, .xml, .inds, .emf, .jpg, .cdr, .gif, .ai, .u01 .indd,.tif,.wmf,.idms,.eps,.pdf,.xml,.inds,.emf,.jpg,.cdr,.gif,.ai,.u01

How can I perform this validation check when a user adds/edit? 用户添加/编辑时如何执行此验证检查?

You can add validate before you bind with DataSource 您可以before you bind with DataSource添加验证

var expression = "FileType == '.indd' OR FileType == '.tif' OR ..... ";
DataRow[] foundRows;
// Use the Select method to find all rows matching the filter.
var foundRows = table.Select(expression);

You can also Bind with DataView and filter on your DataView 您还可以绑定DataViewfilter上的DataView

var dataView = new DataView(YourDataTable);
dataView.RowFilter = "FileType == '.indd' OR FileType == '.tif' OR .....";
YourGridView.DataSource = dataView ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM