Update BD Sqlserver when changing in datagridview instantly

José Carlos 886 Reputation points

Hi friends.

I put a datagridview in a form. I loaded this datagrid with data from SQL Server. I wanted that when I changed a data in the grid, this data would be changed in the database without programming. Is it possible or do I have to put an "Update" button and schedule this update?

Below is the code that populates the gridview.

			String^ connectionString = "Data Source=NOTEBOOK\\SQLEXPRESS;Initial Catalog=LOTOFACIL;Integrated Security=SSPI;Trusted_Connection=True;";

			SqlConnection^ connection = gcnew SqlConnection(connectionString);


			String^ query = "SELECT * from Numeros";

			SqlCommand^ command = gcnew SqlCommand(query, connection);

			SqlDataAdapter^ dataAdapter = gcnew SqlDataAdapter(command);

			DataTable^ dataTable = gcnew DataTable();


			bindingSource1 = gcnew System::Windows::Forms::BindingSource();
			bindingSource1->DataSource = dataTable;

			dataGridView1->DataSource = bindingSource1;


Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,763 questions
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,591 questions
0 comments No comments
{count} votes

Accepted answer
  1. Minxin Yu 10,461 Reputation points Microsoft Vendor

    Hi, José Carlos

    You have to add an update button:
    A primary key is needed in DB table .Change dataAdapter and dataTable to class member Click event sample:

    SqlCommandBuilder^ commandBuilder = gcnew SqlCommandBuilder(dataAdapter);
    	dataAdapter->UpdateCommand = commandBuilder->GetUpdateCommand();

    Best regards,

    Minxin Yu

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. José Carlos 886 Reputation points

    Hello Minxin Yu.

    Thank you very much for your attention.

    A question. Do I keep the same gridview completion code that I posted above and in the "Update" button click event do I put the code you sent me?

    Is that all? Or do I have to change the gridview filling code?

    José Carlos