Changing Values of DataGridView Cell on Button Click

LazyPirate

I just want to ask how I can update or change the cell value of column 'quantity' in my DataGridView when I select a certain row. I have 3 columns in my DataGridView which are Item Name, Quantity and Price. Every time I click the button 'add' or 'less' on my form, the quantity of the selected row item will either add to or subtract 1 from the quantity column and the price will also update every time the quantity increments or decrements. Here is my code when I add my item to my DataGridView.

I'm not really sure if I got it right to add items to my datagrid.

cmd = new MySqlCommand("SELECT * FROM tblmenu", dbConn);
MySqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
    Button btn = new Button();
    btn.Text = rdr["menuName"].ToString();
    btn.Width = 126;
    btn.Height = 80;
    btn.Click += delegate
    {
        MySqlConnection cnn2 = new MySqlConnection(sqlConn.connString);
        cnn2.Open();
        cmd = new MySqlCommand("SELECT menuName, menuPrice FROM tblmenu WHERE menuName = @name", cnn2);
        cmd.Parameters.AddWithValue("@name", btn.Text);
        MySqlDataReader rdr2 = cmd.ExecuteReader();
        while (rdr2.Read())
        {
            dataGridView1.Rows.Add(rdr2.GetString("menuName").ToUpper(), 1, rdr2.GetDouble("menuPrice"));
        }
        dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        lblQty.Text = dataGridView1.RowCount.ToString();
    };
}

I have tried this but when I click another set of item, the quantity from the previous selected item still increments.

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    Add.Click += delegate
    {
        dataGridView1.Rows[e.RowIndex].Cells["quantity"].Value = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells["quantity"].Value) + 1;
    };
    Minus.Click += delegate
    {
        dataGridView1.Rows[e.RowIndex].Cells["quantity"].Value = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells["quantity"].Value) - 1;
    };
}
Mukesh Adhvaryu

First of all, using dataGridView1_CellContentClick does not serve the purpose at all. You intend to increase or decrease quantity on Add or Minus button click. I would use dataGridView1.CurrentRow property to get hold of CurrentRow and manipulate quantity column.For updating price column, you should have a Rate column or a constant Rate value to multiply it with quantity. Do you have it? If not then I don't see how you arrive on price calculation. However assuming you have one :

void quantity_change(object sender, EventArgs e){
    var row=dataGridView1.CurrentRow;

    if(row==null || row.Index<0) 
    return;
    var unit = (sender==add)?1:-1;
    var quantity = Convert.ToInt32(row.Cells["quantity"].Value) + unit;

    row.Cells["quantity"].Value = quantity;
    //assuming you have rate column...
    var rate = Convert.ToDouble(row.Cells["Rate"].Value);
   row.Cells["Price"].Value = quantity * rate;
}

Now bind Click event of your Add & Minus Buttons to the method above in Form constructor.

public myForm(){
    InitializeComponents();
    Add.Click+=quantity_change;
    Minus.Click+=quantity_change;
}

You can also use form's Load event if you want.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Changing Values of DataGridView Cell on Button Click

From Dev

Change dataGridView Cell Formatting on button click (not on cell click)

From Dev

DataGridView not updating values on button click in WPF

From Dev

DataGridView Cell Value Changing

From Dev

DataGridView Cell Value Changing

From Dev

How to call Datagridview cell double click event from a button?

From Dev

How to call Datagridview cell double click event from a button?

From Dev

Button Click not firing When focus is set to next cell in DataGridView on CellEndEdit

From Dev

Changing cell Backcolor on DataGridView CellMouseClick

From Dev

Changing datagridview cell color dynamically

From Dev

changing the combobox cell background in datagridview

From Dev

DataGridView cell color not changing on error

From Dev

comparison of datagridview cell values

From Dev

how to assign random values from array to table cell on a button click

From Dev

Changing views on button click?

From Dev

Changing button value on button click

From Dev

How to check if a datagridview cell is empty through button click in vb.net?

From Dev

How to pass the content of a cell in DataGridView_CellClick function to button_Click function in C#?

From Dev

Adding a datagridview cell on button click brings up error "Collection already belongs.."

From Dev

Changing datagridview cell color based on condition

From Dev

Changing the cell background color of a 'DataGridView' in virtual mode

From Dev

Changing the cell background color of a 'DataGridView' in virtual mode

From Dev

How to resize the datagridview on button click?

From Dev

Enable datagridview by click on button - MVVM

From Dev

how to change cell values in datagridview according to a cell

From Dev

Changing button in cell when pressed

From Dev

Changing button color in cell if clicked

From Dev

Changing button text of different button on button click

From Dev

Microsoft Excel Is changing cell values

Related Related

  1. 1

    Changing Values of DataGridView Cell on Button Click

  2. 2

    Change dataGridView Cell Formatting on button click (not on cell click)

  3. 3

    DataGridView not updating values on button click in WPF

  4. 4

    DataGridView Cell Value Changing

  5. 5

    DataGridView Cell Value Changing

  6. 6

    How to call Datagridview cell double click event from a button?

  7. 7

    How to call Datagridview cell double click event from a button?

  8. 8

    Button Click not firing When focus is set to next cell in DataGridView on CellEndEdit

  9. 9

    Changing cell Backcolor on DataGridView CellMouseClick

  10. 10

    Changing datagridview cell color dynamically

  11. 11

    changing the combobox cell background in datagridview

  12. 12

    DataGridView cell color not changing on error

  13. 13

    comparison of datagridview cell values

  14. 14

    how to assign random values from array to table cell on a button click

  15. 15

    Changing views on button click?

  16. 16

    Changing button value on button click

  17. 17

    How to check if a datagridview cell is empty through button click in vb.net?

  18. 18

    How to pass the content of a cell in DataGridView_CellClick function to button_Click function in C#?

  19. 19

    Adding a datagridview cell on button click brings up error "Collection already belongs.."

  20. 20

    Changing datagridview cell color based on condition

  21. 21

    Changing the cell background color of a 'DataGridView' in virtual mode

  22. 22

    Changing the cell background color of a 'DataGridView' in virtual mode

  23. 23

    How to resize the datagridview on button click?

  24. 24

    Enable datagridview by click on button - MVVM

  25. 25

    how to change cell values in datagridview according to a cell

  26. 26

    Changing button in cell when pressed

  27. 27

    Changing button color in cell if clicked

  28. 28

    Changing button text of different button on button click

  29. 29

    Microsoft Excel Is changing cell values

HotTag

Archive