All,

I have a customized DataGridView where I have implemented extra functionality and bounded datasource in the same custom DataGridView (Using

C# and .NET). Now, I could able to use it properly by placing it on a panel control. I have added label as button on panel control to display

data from datasource on to datagrid and create a binding source. Another Label which act as a button is used to update data from grid to

databse.

Issue: I pressed show label to display data in a dsatagridview. Modified the grid cell value and immediately pressed update label which is on

same panel control. I observed that, the cursor is still in the grid cell when I press Save button. While saving, the cell value is null even

though I have entered something in the presentation layer. My expected behaviour is to get the modified value while saving.

Special Case: After typing something in the grid cell, if I click on somewhere else like the row below where I entered something, before I

click on Save button, it is working fine. (Here, mainly I tried to remove the focus from the currently modified cell)

Is there any way to bind sources before I click on save button? Please suggest me.

Please feel free to ask me if you need any information.

I have also seen same kind of problem on this forum, but unfortunately the author got the answer and didnt post it back.

here is that URL:

http://social.msdn.microsoft.com/Forums/en/winformsdesigner/thread/54dcc87a-adc2-4965-b306-9aa9e79c2946

Please help me.

Recommended Answers

All 3 Replies

Hi,

I had a similar issue on a project. I overcame it by handling the datagridview's CellValidating and CellValidated events. This event fires whenever a cell loses input focus, enabling content validation.
You can use the validating event to check the data entered then update the data source in the validated event.
I also found that, doing it this way, i no longer needed a save button since the changes were bound as soon as the user clicked away from the cell they were editing :)

Just as a tip, i found it helpful to first check that the cell being validated is one that should be edited, then check that the value has actually changed. That way you can skip the validation and avoid binding the data when its not necesary

You mean, I will use something like that

In validating event I will use

private void dataGridView1_Validating(object sender, DataGridViewCellValidatingEventArgs  e)
{
bSource.EndEdit();
}

Can you please give a light on it. I dont understand that how to check the data entered in validating event. If you canm please provide a sample code then it will be really helpfull.

Thanks,
Jatin

try something like:

private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            string newValue = e.FormattedValue.ToString();
            string oldValue = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].FormattedValue.ToString();

            //i like to check that the value has changed before validating it
            if (newValue != oldValue)
            {
                if (false)//replace this with actual validation. 
                {
                    //if data not valid cancel validation
                    e.Cancel= true;
                }
            }
        }

        private void dataGridView1_CellValidated(object sender, DataGridViewCellEventArgs e)
        {
            dataGridView1.EndEdit();
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.