如何向GridView字段添加验证?

时间:2012-10-09 07:31:34

标签: c# asp.net

我的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>

我想验证FileType字段,以便它只接受InDesign,XML,CorelDraw,StaticImage文件类型,其中扩展名应为

之一
  

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

如何在用户添加/编辑时执行此验证检查?

1 个答案:

答案 0 :(得分:0)

您可以添加验证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);

您还可以在DataView

上与filterDataView绑定
var dataView = new DataView(YourDataTable);
dataView.RowFilter = "FileType == '.indd' OR FileType == '.tif' OR .....";
YourGridView.DataSource = dataView ;