c# datadridview

Doğukan Çakır 1 Reputation point
2022-04-04T18:03:18.07+00:00

C# I want to increase the value of the selected cell with (+) and (-) buttons.

Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,596 Reputation points Volunteer Moderator
    2022-04-05T00:33:44.83+00:00

    Best way to do this is have a DataSource e.g. in this case a DataTable. In the code below I increase and decrease the Qty value of the current row in the DataGridView.

    The BindingSource component provides access to the current row, we cast to a DataRow than use SetField and Field extensions to change values.

    Alternate to a DataTable would be a List<T>

    namespace DataGridViewDemo  
    {  
        public partial class DataGridViewForm : Form  
        {  
            private BindingSource bindingSource = new BindingSource();  
            public DataGridViewForm()  
            {  
                InitializeComponent();  
      
                Shown += OnShown;  
      
            }  
      
            private void OnShown(object sender, EventArgs e)  
            {  
                  
                dataGridView1.AutoGenerateColumns = false;  
      
                DataTable table = new DataTable();  
                table.Columns.Add("Item", typeof(string));  
                table.Columns.Add("Qty", typeof(int));  
                table.Columns.Add("Price", typeof(decimal));  
      
                table.Rows.Add("First item", 2, 60);  
                table.Rows.Add("Second item", 2, 50);  
                table.Rows.Add("Third item", 1, 20);  
      
                bindingSource.DataSource = table;  
                dataGridView1.DataSource = bindingSource;  
      
            }  
      
            private void IncreaseButton_Click(object sender, EventArgs e)  
            {  
                var row = ((DataRowView)bindingSource.Current).Row;  
                row.SetField("Qty", row.Field<int>("Qty") +1);  
            }  
      
            private void DecreaseButton_Click(object sender, EventArgs e)  
            {  
                var row = ((DataRowView)bindingSource.Current).Row;  
                row.SetField("Qty", row.Field<int>("Qty") -1);  
      
            }  
        }  
    }  
    

    Source code

    Can be found here.

    190075-datagridviewplusminus.png

    0 comments No comments

  2. Doğukan Çakır 1 Reputation point
    2022-04-05T09:09:14.95+00:00

    190037-ekran-al%C4%B1nt%C4%B1s%C4%B1.png

    I'm sorry I don't know much, but I can't increase or decrease it again.

    using System;  
    using System.Collections.Generic;  
    using System.ComponentModel;  
    using System.Data;  
    using System.Drawing;  
    using System.Linq;  
    using System.Text;  
    using System.Threading.Tasks;  
    using System.Windows.Forms;  
    using System.Data.SqlClient;  
      
    namespace kafe_ders  
    {  
        public partial class Form2 : Form  
        {  
      
            SqlConnection connection = new SqlConnection("Data Source=DESKTOP-46FHV6F;Initial Catalog=ders-kafe;Integrated Security=True");  
            SqlCommandBuilder commandBuilder ;  
            SqlDataAdapter adtr;  
            DataTable tbl= new DataTable();  
              
            public Form2()  
            {  
                InitializeComponent();  
                GetList();  
                  
      
                Shown += OnShown;  
            }    
            DataTable GetList()  
            {  
                tbl.Clear();  
                adtr = new SqlDataAdapter("select * from musteri",connection);  
                adtr.Fill(tbl);  
                dataGridView1.DataSource = tbl;  
                return tbl;  
            }  
            private void Form2_Load(object sender, EventArgs e)  
            {  
                // TODO: Bu kod satırı '_ders_kafeDataSet12.musteri' tablosuna veri yükler. Bunu gerektiği şekilde taşıyabilir, veya kaldırabilirsiniz.  
                this.musteriTableAdapter2.Fill(this._ders_kafeDataSet12.musteri);  
            }  
            private void button2_Click(object sender, EventArgs e)  
            {  
                commandBuilder = new SqlCommandBuilder(adtr);  
                adtr.Update(tbl);  
                GetList();  
            }  
      
            private void button4_Click(object sender, EventArgs e)  
            {  
                  
                this.Close();  
            }  
            private void selectedCellsButton_Click(object sender, System.EventArgs e)  
            {  
                Int32 selectedCellCount =  
                    dataGridView1.GetCellCount(DataGridViewElementStates.Selected);  
                if (selectedCellCount > 0)  
                {  
                    if (dataGridView1.AreAllCellsSelected(true))  
                    {  
                        MessageBox.Show("All cells are selected", "Selected Cells");  
                    }  
                    else  
                    {  
                        System.Text.StringBuilder sb =  
                            new System.Text.StringBuilder();  
      
                        for (int i = 0;  
                            i < selectedCellCount; i++)  
                        {  
                            sb.Append("Row: ");  
                            sb.Append(dataGridView1.SelectedCells[i].RowIndex  
                                .ToString());  
                            sb.Append(", Column: ");  
                            sb.Append(dataGridView1.SelectedCells[i].ColumnIndex  
                                .ToString());  
                            sb.Append(Environment.NewLine);  
                        }  
      
                        sb.Append("Total: " + selectedCellCount.ToString());  
                        MessageBox.Show(sb.ToString(), "Selected Cells");  
                    }  
                }  
            }  
            int deger ;  
            private void dataGridView1_CellMouseClick_1(object sender, DataGridViewCellEventArgs e)  
            {  
      
            }  
            private BindingSource bindingSource = new BindingSource();  
            private void OnShown(object sender, EventArgs e)  
            {  
      
                dataGridView1.AutoGenerateColumns = false;  
      
                DataTable table = new DataTable();  
                table.Columns.Add("Item", typeof(string));  
                table.Columns.Add("Qty", typeof(int));  
                table.Columns.Add("Price", typeof(decimal));  
      
                table.Rows.Add("First item", 2, 60);  
                table.Rows.Add("Second item", 2, 50);  
                table.Rows.Add("Third item", 1, 20);  
      
                bindingSource.DataSource = table;  
                dataGridView1.DataSource = bindingSource;  
            }  
            private void button1_Click_1(object sender, EventArgs e)  
            {  
                var row = ((DataRowView)bindingSource.Current).Row;  
                row.SetField("Qty", row.Field<int>("Qty") + 1);  
            }  
            private void button3_Click(object sender, EventArgs e)  
            {  
                var row = ((DataRowView)bindingSource.Current).Row;  
                row.SetField("Qty", row.Field<int>("Qty") - 1);  
            }  
        }  
    }  
      
    

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.