techPowerUp! Forums

Go Back   techPowerUp! Forums > Software > Programming & Webmastering

Reply
 
Thread Tools
Old Oct 16, 2007, 04:14 PM   #1
Kreij
Hardcore Monkey Moderator
 
Kreij's Avatar
 
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,117 (5.27/day)
Thanks: 591
Thanked 5,493 Times in 2,937 Posts

System Specs

DataGridView Question (C#)

Here is what I am trying to do;
When a user enters data into a DataGridView cell (TextBox) and then hits tab to advance to the next column, I want to verify the data and respond appropriately.

Overriding the CellEndEdit event I can easily validate the data.
The problem that I am seeing is that if the data is invalid, I want to return the users to the cell with the invalid data and set it back in edit mode.
I can't seem to get this to work the way I am trying, and can't seem to find anything.

The following code simply check to see if that a valid work order number was entered by
seeing if an ExecuteScalar call returns anything.

Code:
private void laborDataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            object temp;
            // Check for Valid WO #
            if (e.ColumnIndex == 3)
            {
                SqlCommand sqlCom = new SqlCommand(
                    "select WOListID from WorkOrders where WONum='" +
                    laborDataGridView[e.ColumnIndex, e.RowIndex].Value.ToString().Trim() + "' " +
                    "and Completed='false'", sqlCon);
                sqlCon.Open();
                temp = sqlCom.ExecuteScalar();
                if (temp == null)
                {
                    MessageBox.Show("WO # " +
                        laborDataGridView[e.ColumnIndex, e.RowIndex].Value.ToString().Trim() +
                        " is either completed or does not exist", "Data Error",
                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                    laborDataGridView[e.ColumnIndex, e.RowIndex].Value = "";
// The next line does not return the user to the cell with invalid data
                    laborDataGridView[e.ColumnIndex, e.RowIndex].Selected = true;
                    sqlCon.Close();
                    return;
                }
                sqlCon.Close();
            }
// rest of code removed. .........
The value of the offending cell is set to null (""), but the next column's cell gets the focus and selection.
Any ideas ?
__________________

Cloud (noun, singular): A dynamic arrangement of multiple potential single points of failure, with a user at one end and their data at the other.


Get more tech news on a wide variety of topics at NextPowerUp
Kreij is offline  
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 02:53 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
no new posts