DataGrid with validator

Discussion in 'ASP .Net Datagrid Control' started by BEN, Oct 28, 2004.

  1. BEN

    BEN Guest

    Hi,

    I want to add a custom validator to a dataGrid's boundcolumn. I use
    datagrid's "edit, update, cancel" button to do. But I cannot find a method
    to add the validator to boundcolumn.

    In other way, I try to use TemplateColumn and EditItemTemplate with custom
    validator. But I cannot bind the value to each corresponding column. All
    values in the column are the same. The code is shown as below.

    <asp:BoundColumn DataField="CustomerID" SortExpression="CustomerID"
    HeaderText="CustomerID"></asp:BoundColumn>
    <asp:BoundColumn Visible="False" DataField="EmployeeID"
    SortExpression="EmployeeID" HeaderText="EmployeeID"></asp:BoundColumn>
    <asp:BoundColumn DataField="Freight" SortExpression="Freight"
    HeaderText="Freight"></asp:BoundColumn>
    <asp:TemplateColumn SortExpression="OrderDate" HeaderText="OrderDate">
    <ItemTemplate>
    <%# DataBinder.Eval(DataSet11,
    "Tables[Orders].Defaultview.[0].OrderDate", "{0:d}") %>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox id=TextBox1 runat="server" Text='<%#
    DataBinder.Eval(DataSet11, "Tables[Orders].OrderDate", "{0:d}") %>'>
    </asp:TextBox>
    <asp:CustomValidator id=CustomValidator2 runat="server"
    ErrorMessage='<%# DataBinder.Eval(DataSet11,
    "Tables[Orders].DefaultView.[0].OrderDate", "{0:d}")
    %>'>*</asp:CustomValidator>
    </EditItemTemplate>
    </asp:TemplateColumn>


    What is the best way to validate a value in dataGrid? Or use other server
    control?

    Thanks in advance.
     
    BEN, Oct 28, 2004
    #1
    1. Advertising

  2. You have to use a TemplateColumn if you want to add validators for your
    editing interface.

    Try the following syntax for your EditItemTemplate:

    <asp:TextBox ... Text='<%# DataBinder.Eval(Container.DataItem,
    "ColumnName")' />

    Container.DataItem returns the "record" bound to the particular row, so
    you want to use that to get the column record, and not the DataSet class
    like you're doing now.

    hth



    BEN wrote:
    > Hi,
    >
    > I want to add a custom validator to a dataGrid's boundcolumn. I use
    > datagrid's "edit, update, cancel" button to do. But I cannot find a method
    > to add the validator to boundcolumn.
    >
    > In other way, I try to use TemplateColumn and EditItemTemplate with custom
    > validator. But I cannot bind the value to each corresponding column. All
    > values in the column are the same. The code is shown as below.
    >
    > <asp:BoundColumn DataField="CustomerID" SortExpression="CustomerID"
    > HeaderText="CustomerID"></asp:BoundColumn>
    > <asp:BoundColumn Visible="False" DataField="EmployeeID"
    > SortExpression="EmployeeID" HeaderText="EmployeeID"></asp:BoundColumn>
    > <asp:BoundColumn DataField="Freight" SortExpression="Freight"
    > HeaderText="Freight"></asp:BoundColumn>
    > <asp:TemplateColumn SortExpression="OrderDate" HeaderText="OrderDate">
    > <ItemTemplate>
    > <%# DataBinder.Eval(DataSet11,
    > "Tables[Orders].Defaultview.[0].OrderDate", "{0:d}") %>
    > </ItemTemplate>
    > <EditItemTemplate>
    > <asp:TextBox id=TextBox1 runat="server" Text='<%#
    > DataBinder.Eval(DataSet11, "Tables[Orders].OrderDate", "{0:d}") %>'>
    > </asp:TextBox>
    > <asp:CustomValidator id=CustomValidator2 runat="server"
    > ErrorMessage='<%# DataBinder.Eval(DataSet11,
    > "Tables[Orders].DefaultView.[0].OrderDate", "{0:d}")
    > %>'>*</asp:CustomValidator>
    > </EditItemTemplate>
    > </asp:TemplateColumn>
    >
    >
    > What is the best way to validate a value in dataGrid? Or use other server
    > control?
    >
    > Thanks in advance.
    >
    >



    --

    Scott Mitchell

    http://www.4GuysFromRolla.com

    * When you think ASP.NET, think 4GuysFromRolla.com!
     
    Scott Mitchell [MVP], Oct 28, 2004
    #2
    1. Advertising

  3. BEN

    BEN Guest

    Thx Scott. It works.

    "Scott Mitchell [MVP]" <> ¦b¶l¥ó
    news:mA9gd.1666$ ¤¤¼¶¼g...
    > You have to use a TemplateColumn if you want to add validators for your
    > editing interface.
    >
    > Try the following syntax for your EditItemTemplate:
    >
    > <asp:TextBox ... Text='<%# DataBinder.Eval(Container.DataItem,
    > "ColumnName")' />
    >
    > Container.DataItem returns the "record" bound to the particular row, so
    > you want to use that to get the column record, and not the DataSet class
    > like you're doing now.
    >
    > hth
    >
    >
    >
    > BEN wrote:
    > > Hi,
    > >
    > > I want to add a custom validator to a dataGrid's boundcolumn. I use
    > > datagrid's "edit, update, cancel" button to do. But I cannot find a

    method
    > > to add the validator to boundcolumn.
    > >
    > > In other way, I try to use TemplateColumn and EditItemTemplate with

    custom
    > > validator. But I cannot bind the value to each corresponding column. All
    > > values in the column are the same. The code is shown as below.
    > >
    > > <asp:BoundColumn DataField="CustomerID"

    SortExpression="CustomerID"
    > > HeaderText="CustomerID"></asp:BoundColumn>
    > > <asp:BoundColumn Visible="False" DataField="EmployeeID"
    > > SortExpression="EmployeeID" HeaderText="EmployeeID"></asp:BoundColumn>
    > > <asp:BoundColumn DataField="Freight" SortExpression="Freight"
    > > HeaderText="Freight"></asp:BoundColumn>
    > > <asp:TemplateColumn SortExpression="OrderDate"

    HeaderText="OrderDate">
    > > <ItemTemplate>
    > > <%# DataBinder.Eval(DataSet11,
    > > "Tables[Orders].Defaultview.[0].OrderDate", "{0:d}") %>
    > > </ItemTemplate>
    > > <EditItemTemplate>
    > > <asp:TextBox id=TextBox1 runat="server" Text='<%#
    > > DataBinder.Eval(DataSet11, "Tables[Orders].OrderDate", "{0:d}") %>'>
    > > </asp:TextBox>
    > > <asp:CustomValidator id=CustomValidator2 runat="server"
    > > ErrorMessage='<%# DataBinder.Eval(DataSet11,
    > > "Tables[Orders].DefaultView.[0].OrderDate", "{0:d}")
    > > %>'>*</asp:CustomValidator>
    > > </EditItemTemplate>
    > > </asp:TemplateColumn>
    > >
    > >
    > > What is the best way to validate a value in dataGrid? Or use other

    server
    > > control?
    > >
    > > Thanks in advance.
    > >
    > >

    >
    >
    > --
    >
    > Scott Mitchell
    >
    > http://www.4GuysFromRolla.com
    >
    > * When you think ASP.NET, think 4GuysFromRolla.com!
     
    BEN, Oct 28, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Kamoski
    Replies:
    0
    Views:
    348
    Mark Kamoski
    Jul 16, 2003
  2. Stephan Bour

    Custom validator inside DataGrid

    Stephan Bour, Jan 16, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    5,436
    Peter Blum
    Jan 16, 2004
  3. VB Programmer

    Datagrid/Range Validator Error - Help Pls

    VB Programmer, Sep 22, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    1,939
    VB Programmer
    Sep 22, 2004
  4. Replies:
    0
    Views:
    356
  5. Replies:
    4
    Views:
    904
    Richter~9.6
    Feb 13, 2007
Loading...

Share This Page