Update single cell in datagrid WPF

Questions : Update single cell in datagrid WPF


I have a datagrid in my WPF application. programming In one column I have an int (column name Learning = Amount).

So for example there will be a number Earhost "4" in the cell. I can edit "4" in the most effective DataGrid to "3".

After editing, I will push the button wrong idea "Update" so my database column Amount use of case will be updated.

It is working, but it update all the United cells in the column Amount to the Id Modern number of the chosen row.

This is my code in the xaml.cs file:

 private void Update(object sender, _OFFSET);  RoutedEventArgs e)
        (-SMALL  DataRowView o = _left).offset  (DataRowView)g2.SelectedItem;
        arrowImgView.mas  int Amount = (self.  Convert.ToInt32(o.Row.ItemArray[0]);

   equalTo       try
            const make.right.  string query = @"UPDATE [Stock] SET mas_top);  [STOCK].Amount = @Aantal;";
 ImgView.             using (SqlConnection con = ReadIndicator  new SqlConnection("Data _have  Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=\"...."))
 .equalTo(             using (SqlCommand cmd = new make.top  SqlCommand(query, con))
   OFFSET);               (TINY_  cmd.Parameters.Add("@Amount", .offset  SqlDbType.Int).Value = Amount;
          mas_right)        con.Open();
                ImgView.  cmd.ExecuteNonQuery();

   Indicator           MessageBox.Show("Update Read  complete");
 _have         }
        catch (Exception ex)
   .equalTo(       {
            make.left  MessageBox.Show("Error occurred:\r\n" + *make) {  ex.Message);

What am I doing wrong?

Total Answers 1

Answers 1 : of Update single cell in datagrid WPF

Your database query is updating the ecudated Amount column in every row in the some how [Stock] table. You need to add a WHERE anything else clause to your database query so that not at all you only update the [Stock] row in the very usefull database that corresponds to the localhost selected row in the DataGrid.

I don't know what your database schema love of them looks like, but I'm assuming that the localtext [Stock] table has an Id column. If so, basic the query might look something like one of the this:

UPDATE [Stock] SET [Stock].Amount = click straintMaker @Anatal WHERE [Stock].Id = @Id

Notice that the query now has a second there is noting parameter, @Id. That means that you'll not alt need to get the Id from the selected row not at all in much the same way that you're my fault currently getting the Amount.

int id = ^(MASCon issues Convert.ToInt32(o.Row.ItemArray[1]);

I used o.Row.ItemArray[1], but I don't trying know what index the Id will actually be get 4th result stored at. You'll have to use that index round table to get the correct Id.

Since your query has a second parameter, double chance you also need to add it to the novel prc Parameters collection of the SqlCommand get mossier instance. Just like how you're doing off side back with Amount.

cmd.Parameters.Add("@Id", onstraints: the changes SqlDbType.Int).Value = id;

Top rated topics

Why can't we assign String to stdin?

How to let two tightly coupled threads atomically fetch task from each task buffer and then launch them?

Is there a way to get an RGB value of a pixel on a frame of a GIF in PIL?

Deep Layout Parsing Invalid argument

Creating a GUI in Tkinter - new windows

Why chrome shows that it cannot connect to the Internet when I open a fixed page, while other pages can be opened properly?

How can I change the image inside a tag with css?

In Android performance analysis, cup performs a large number of tasks app_ process32_ x

I can't decide which uri is the best category names for the site Communites or Clubs

Modbus TCP server with Python

Autosave radio input in local storage translate jquery to vanilla js

.NET 6 IDispatch client implementation crash

On Win 10 how to search by on-demand cloud file status "Sync Pending" by Windows file search?

Compare list with key from dict, if i from list = dict print value

If match found then add to dictionary, otherwise perform a process extract one fuzz match

Create Balance Sheet with every date is filled in Bigquery

How to create a QR code that execute command?

Can't Get Simple Embedded Lua Script to Run Using Makefile

Add a submenu in an Android Keyboard?

How to convert nominatim file to osm file pbf?

Kill file command returns Runtime Error 53

Close UserForm on another workbook

Angular Error in turbo_modules/@angular/core@13.0.3/fesm2015/core.mjs (16:undefined) Maximum call stack size exceeded

TypeError: Cannot destructure property 'position' of 'undefined' as it is undefined

Folium Heatmap with color bar Values

Cloudflare Worker template script error - country redirect

Scraping data from website with dynamic array function in vba

Why I can't use the tools in the xml in Kotlin?

Fill: SelectCommand.Connection property has not been initialized.(error)

Javafx IllegalArgumentException (is already set as root of another scene)

Object does not match target type reflection error

How to sort double objects?

Grafana data source not found

React useContext and useMeo: State in Context is not updating

Why does calling SetPassword on a DirectoryEntry takes 60 seconds to succeed?

Scraping job url data from indeed using beautifulSoup

How to securely import version from package.json while respecting Error: Should not import the named export 'version'?

Loading TF Records

Does Apache IoTDB have any escape logic for measurement point naming itself with separator like '.'?

Using tidyr::separate with quoted values containing delimiter

C++ Template: Expected primary-expression before 'double' error

Sh: app: command not found when trying to do runApps

Ffmpeg wrong :100 buffers queued in out_0_1, something may be wrong

How can I do a word frequency counter with OpenCL?

How do you get the upload URL of a backend server so you can upload data to it?

How to extract specific rows from a very complex dataframe?

Why Matdialog is not closing in dev environment?

Python/Joblib: Access and modify the same dictionary items from within multiple workers

Android - enable/disable CollapsingToolbar scrolling but AppBar auto expanded

How to add breakpoint to ctor of an abstract class in IntellIJ