Monday, March 26, 2012

Validation Control in DataGrid

I have a datagrid in which I allow the users to edit the records. Is it possible for me to apply validation control in the datagrid? If yes, can anybody hint me how to do that or if there is any article describing about it. If no, then would anyone have good suggestion to ensure data integrity (I want to make only money i.e. $1.50 is entered). Thanks.There are a couple of ways to do this. You first need to use a TemplateColumn to put multiple controls in the column when in "edit mode." In your ItemTemplate, you can just throw in the html and databinding syntax to display the value. In your EditItemTemplate, add in a textbox and the addition Validation Controls you would like to use. For help with that,this link may help.

You could use a Range Validator with the Type set to Currency. You'll have to set the MinimumValue and MaximumValue properties. Your minimum would likely be zero, with the max being whatever you decide. You could also use a RegularExpressionValidator and just use the regular expression for your currency of choice (or for any positive decimal). If this value is required, you also need to include a RequiredFieldValidator in addition to either of the above.

Hope this helps.
Thank you, but if you can give some codes will be great 'cause this is my first project using .net.
it would be something like this:


<asp:datagrid id="dg" ...>
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<%#Container.DataItem("bla")%>
</ItemTemplate>
<EditItemTemplate>
<asp:textbox id="txb1" runat="server" />
<asp:RegularExpressionValidator .../>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>

No comments:

Post a Comment