Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3406570
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:38:37+00:00 2026-05-18T05:38:37+00:00

I have a DataGridView attached to an XML DataSource. Everytime user edits a cell

  • 0

I have a DataGridView attached to an XML DataSource. Everytime user edits a cell the program automatically updates the relative XML file. To handle editing I am using both:

private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
       //do edit
}
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
       //do edit
}

Editing works like that, user clicks on a cell and changes the value. Then he should press the Enter key to make everything work fine, but for example if he click the mouse button outside or if he goes outside the cell with the left arrow key, the program messes up. It works anyway because I managed to handle this exception but I would like my program to handle this situation better. For example, when user enters a cell I want to deny him to go on other cells with the Arrow Keys. I tried to catch the KeyDown event but it didn’t work:

    private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
    {
        if ((e.KeyCode == Keys.Left) || (e.KeyCode == Keys.Right)) //etc...
        {
            e.Handled = true;
        }
    }

Basically when begin edit starts I want to edit the cell ONLY if user presses Enter. Any ideas? If the control (in this case the cell) looses focus during the edit (user presses Esc, clicks the mouse outside of the control, etc…) I need to prevent the EndEdit event from starting.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T05:38:38+00:00Added an answer on May 18, 2026 at 5:38 am

    You can use RowValidating Event to cancel event and RowValidated event for to save like this:

        private void dataGridView1_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
        {
            string data = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
            if(!ValidateData(data))
                e.Cancel = true;
        }
    
        private bool ValidateData(string data)
        {
            // do validation which u want to do.
        }
    
        private void dataGridView1_RowValidated(object sender, DataGridViewCellEventArgs e)
        {
            string data = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
            SaveData(data);
        }
    
        private void SaveData(string data)
        {
            // save data
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a datagridview whose datasource is attached to a datatable how do i
I have a DataGridView with its datasource set to a generic list of custom
I have datagridview in my C# program and when I move scroll-bar to left,
i have DatagridView in my C# program and i want to clean the grid
Hi I have DataGridView control that I manyally fill with items (no DataSource) like
I have a datagridview and attached list of Employees to it, somthing like this:
I have a datagridview on a form, and I want a specific cell in
I have a DataGridView with one DataGridViewComboBoxColumn in my WinForms application. I need to
I have a datagridview where the users can select which subset of columns to
I have a DataGridView that I want to query using Linq (C# WinForm). I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.