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

  • SEARCH
  • Home
  • 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 7626745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:22:01+00:00 2026-05-31T05:22:01+00:00

I’m using a winform DataGridView for data entry. The problem is that when my

  • 0

I’m using a winform DataGridView for data entry. The problem is that when my user has entered a row but not clicked off it, that row isn’t getting saved to the grid’s datasource (a datatable in memory). So, when my user submits the form, I want to detect if the DataGridview has focus, and simulate a keypress (tab?) or set the focus to a different control or do some other operation to save the current row without changing the data.

DataGridView.ContainsFocus is always returning false, as is DataGridView.Focused. Is there another property i should be using? How should I save this row of data?

EDIT: See this question Trouble using DataGridViewComboboxColumn for input for how I’m creating the DataTable and binding it to the datagridview. And datagridview not allowing user to delete row to see another issue I had with this same datagrid.

  • 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-31T05:22:02+00:00Added an answer on May 31, 2026 at 5:22 am

    Windows forms controls can be connected directly to data sources, but are designed to be used with a BindingSource control. This is used to marshall input from form controls to and from a data source.

    It is a component which you can drag onto your form in the designer, and then you can set it as the data source for your datagridview control.

    Once it is on the form, you can use the designer to set it as the datasource for your datagridview. Then you can use an event handler, for example, the form load event handler to provide a datatable as the data source for the binding source.

    The binding source provides you with much finer control over how and when data is transferred from the form controls to the underlying data sources. Simply using the Binding Source may fix your problem. If not, then calling the EndEdit method on the binding source before attempting to save the data should cause any outstanding edits to be written to the datatable.

    For a simple example, create a new Windows forms project. To the form, add a datagridview called “datagridview1”, a BindingSource with the name peopleBinding source, and a command button called saveButton. Set the datasource for gridview1 to be peopleBindingSource.

    Add event handlers for form.Load and saveButton.Click as follows:

       public DataTable GetData()
        {
            DataTable t = new DataTable();
            t.Columns.Add("FirstName", typeof(string));
            t.Columns.Add("LastName", typeof(string));
            t.Rows.Add("Joe","Bloggs");
            t.Rows.Add("Fred","Bloggs");
            return t;
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = true;
            DataTable people = GetData();
            peopleBindingSource.DataSource = people;
    
        }
    
        private void SaveButton_Click(object sender, EventArgs e)
        {
            DataTable t = peopleBindingSource.DataSource as DataTable;
        }
    

    Run the application, and you should find that if you inspect the data table in SaveButton_Click that any changes you have made to the data are persisted.

    If you prefer Visual Studio to write all the code for you:

    1. Add a new Data source using the “Add data source…” wizard.
    2. From the Data sources window, drag a table onto your form.

    Visual studio will add the necessary DataGridView, BindingSource, etc and wire them all up for you. You can then inspect the code to see how it all fits together.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I want to count how many characters a certain string has in PHP, but
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I need to clean up various Word 'smart' characters in user input, including but
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) 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.