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

Radgridview cell validating

2 Answers 333 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Ryan
Top achievements
Rank 1
Ryan asked on 15 Oct 2014, 05:09 PM
Hi,
I  have 2 columns among other columns in a radgridview: one is a textbox and the another is a button (Scan button). The user either can enter data in the textbox or can click on the scan button to get back data into the textbox. The textbox has some validation that I don't need to be triggered if the user is clicking on the Scan button but should be triggered if the user click on any other cell. How can do that as right now any validation is triggered as soon as I click on the Scan button.

Thanks, 

2 Answers, 1 is accepted

Sort by
0
Accepted
Dess | Tech Support Engineer, Principal
Telerik team
answered on 20 Oct 2014, 10:11 AM
Hello Jamie,

Thank you for writing.

Please have a look at the following code snippet, demonstrating how to control whether the CellValidating event will be canceled or not, depending on which other cell you are clicking:
public Form1()
{
    InitializeComponent();
 
    DataTable dt = new DataTable();
    dt.Columns.Add("Id", typeof(int));
    dt.Columns.Add("Name", typeof(string));
    dt.Columns.Add("IsActive", typeof(bool));
 
    for (int i = 0; i < 10; i++)
    {
        dt.Rows.Add(i, "Item" + i, i % 2 == 0);
    }
 
    this.radGridView1.DataSource = dt;
    GridViewCommandColumn commandColumn = new GridViewCommandColumn("Scan");
    commandColumn.DefaultText = "Scan";
    commandColumn.UseDefaultText = true;
    radGridView1.MasterTemplate.Columns.Add(commandColumn);
 
    radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
}
 
private void radGridView1_CellValidating(object sender, CellValidatingEventArgs e)
{
    e.Row.ErrorText = string.Empty;
    RadTextBoxEditor tbEditor = e.ActiveEditor as RadTextBoxEditor;
    if (tbEditor != null &&
        e.Column.Name == "Name" &&
        tbEditor.Value + "" == string.Empty &&
        e.Row.Tag == null)
    {
        e.Row.ErrorText = "Empty value is not allowed!";
        e.Cancel = true;
    }
}
 
private void radGridView1_CommandCellClick(object sender, EventArgs e)
{
    GridCommandCellElement commandCell = sender as GridCommandCellElement;
    commandCell.RowInfo.Cells["Name"].Value = "SomeValue";
}
 
private void radGridView1_MouseDown(object sender, MouseEventArgs e)
{
    RadButtonElement cellButton = this.radGridView1.ElementTree.GetElementAtPoint(e.Location) as RadButtonElement;
    if (cellButton != null)
    {
        GridCommandCellElement commandCell = cellButton.Parent as GridCommandCellElement;
        if (commandCell != null)
        {
            commandCell.RowInfo.Tag = "SkipValidation";
        }
    }
}
 
private void radGridView1_MouseUp(object sender, MouseEventArgs e)
{
    RadButtonElement cellButton = this.radGridView1.ElementTree.GetElementAtPoint(e.Location) as RadButtonElement;
    if (cellButton != null)
    {
        GridCommandCellElement commandCell = cellButton.Parent as GridCommandCellElement;
        if (commandCell != null)
        {
            commandCell.RowInfo.Tag = null;
        }
    }
}

Note that this is just a sample implementation and it may not cover all possible cases. Feel free to modify it on a way which suits your requirement best.

I hope this information helps. Should you have further questions, I would be glad to help.

Regards,
Desislava
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Ryan
Top achievements
Rank 1
answered on 20 Oct 2014, 07:31 PM
Thanks Desislava, it was helpful.
Tags
GridView
Asked by
Ryan
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Ryan
Top achievements
Rank 1
Share this question
or