This is a migrated thread and some comments may be shown as answers.

Validating with GridView

2 Answers 149 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Manuel
Top achievements
Rank 1
Manuel asked on 07 Jul 2009, 01:06 AM
Hi:
I have a problem and maybe an issue.
1. How can I do to validate when I add a new row into a GridView?...
      * I have 2 numeric columns, when I add a new row, I validate that a column is greater than the other column, if not I have to display a message and cancel the insertion.
I try with the RowValidating event, but I can not get the values captured in the 2 columns.
Note: Is to add a new row.
     



2. I have a column of type GridViewCheckBoxColumn and I set the ReadOnly property equal to true for this column, but at runtime I can change the column, and should not be assumed, that because is ReadOnly.

The controls are the new Q2

Thank you!!

2 Answers, 1 is accepted

Sort by
0
Jack
Telerik team
answered on 07 Jul 2009, 02:37 PM
Hello Manuel,

GridViewNewRowInfo contains a DataRowInfo property which contains the values entered in the new row. I admit, that sometimes this is confusing and we will consider changing this in a future version of RadControls. So, you have to get this property to access the values. Please check the following sample:

void radGridView1_RowValidating(object sender, RowValidatingEventArgs e) 
    GridViewNewRowInfo newRow = e.Row as GridViewNewRowInfo; 
    if (newRow != null && newRow.DataRowInfo != null
    { 
        this.radGridView1.CloseEditor(); 
        int value1 = GetIntValue(newRow.DataRowInfo.Cells["Value1"].Value); 
        int value2 = GetIntValue(newRow.DataRowInfo.Cells["Value2"].Value); 
        if (value1 > value2) 
        { 
            e.Cancel = true;                
        } 
    } 

int GetIntValue(object value) 
    if (value == DBNull.Value) 
    { 
        return 0; 
    } 
    return Convert.ToInt32(value); 

However, we noticed that when navigating by using the keyboard the DataRowInfo property contains null value. This issue will be addressed in our upcoming service pack. I modified the grid behavior to work around the issue. Here is the code:

radGridView1.GridBehavior = new MyGridBehavior();
public class MyGridBehavior : BaseGridBehavior 
    public override bool ProcessKey(KeyEventArgs keys) 
    { 
        GridViewNewRowInfo newRow = this.GridControl.CurrentRow as GridViewNewRowInfo; 
        bool isLastColumn = this.GridControl.Columns.IndexOf((GridViewDataColumn)this.GridControl.CurrentColumn) 
            == this.GridControl.Columns.Count-1; 
 
        if (newRow != null && newRow.DataRowInfo != null && 
            ((keys.KeyCode == Keys.Enter) || (keys.KeyCode == Keys.Tab && isLastColumn))) 
        { 
            this.GridControl.CloseEditor(); 
            int value1 = GetIntValue(newRow.DataRowInfo.Cells["Value1"].Value); 
            int value2 = GetIntValue(newRow.DataRowInfo.Cells["Value2"].Value); 
            if (value1 > value2) 
            { 
                return true
            } 
        } 
        return base.ProcessKey(keys); 
    } 
 
    int GetIntValue(object value) 
    { 
        if (value == DBNull.Value) 
        { 
            return 0; 
        } 
        return Convert.ToInt32(value); 
    } 

Regarding your second question: I confirm that this issue, we will address it in our upcoming service pack. Use this code to work around it:

void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e) 
    if (e.CellElement is GridCheckBoxCellElement && e.CellElement.ColumnInfo.ReadOnly) 
    { 
        e.CellElement.Children[0].Enabled = false
    } 

I have updated your Telerik points for these bug reports.

Should you have any questions, I will be glad to help.

Best wishes,
Jack
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Manuel
Top achievements
Rank 1
answered on 10 Jul 2009, 06:34 PM
Hello:
My first question... using GridBehavior does not work correctly, but don't worry I wait to the next service pack.

the second question if it works what you said.... thank you..

Telerik points have not been reflected.
Thanks!!
Tags
GridView
Asked by
Manuel
Top achievements
Rank 1
Answers by
Jack
Telerik team
Manuel
Top achievements
Rank 1
Share this question
or