Load columns and rows of datagridview

Noni aggarwal 40 Reputation points
2023-12-03T07:22:18.7733333+00:00

I want to query and perform update operations on a datagridview data in the fastest and efficient manner but in the memory . The term efficient is used because there will be aprrox 1000 rows data .

why and what i am trying to do

Please consider a scenario . There is a datagridview with 2 columns and 2 rows

mobile case 30 pieces

Tv 20 pieces

now there is another datagridview which is used a form for selling products

in column1 of this datagridview if the user types Mobile case and in second column he types 10 , then in the memory only , it should update the quantity column to 20 . (30-10) . in the actual database i will update later .

So please suggest how to load the datagridview and perform select and update queries onto it .

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,806 questions
C#
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.
9,986 questions
{count} votes

2 answers

Sort by: Most helpful
  1. KOZ6.0 4,335 Reputation points
    2023-12-03T11:29:29.9133333+00:00

    When you update a row in the DataTable, the RowState property becomes RowState.Modified, and the value before the update can be retrieved with DataRowVersion.Original.

    Ex.

    var dt = new DataTable();
    dt.Columns.Add("num", typeof(int));
    dt.Rows.Add(30);
    dt.AcceptChanges();
    dt.Rows[0]["num"] = 10;
    var state = dt.Rows[0].RowState; // RowState.Modified
    var original = dt.Rows[0]["num", DataRowVersion.Original]; // 30
    var current = dt.Rows[0]["num", DataRowVersion.Current];   // 10
    var difference = (int)original - (int)current;  // 20
    

    You can update only changed rows using DataAdapter.Update method.

    Or you can write code yourself.


  2. Karen Payne MVP 35,021 Reputation points
    2023-12-04T11:19:27.9533333+00:00

    If all you are doing are simple CRUD operations than consider a TableAdapter which write all the code for you but be forewarned going farther than simple CRUD operations you need to fully understand how to use the TableAdapter. They have no problem with a 1,000 records.

    Sample code which I modified from what Microsoft wrote.

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Closing += OnClosing;
            personDataGridView.DataError += DataGridViewDataError;
        }
    
        private void DataGridViewDataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            e.Cancel = true;
            MessageBox.Show("Invalid input");
        }
    
        private void OnClosing(object sender, CancelEventArgs e)
        {
            if (dataSet1.Tables[0].HasChanges())
            {
                if (Question("Save before exiting"))
                {
                    SaveChanges();
                }
                else
                {
                    e.Cancel = false;
                }
            }
           
        }
    
        private void personBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            SaveChanges();
        }
    
        private void SaveChanges()
        {
            Validate();
            personBindingSource.EndEdit();
            tableAdapterManager.UpdateAll(dataSet1);
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
            personTableAdapter.Fill(dataSet1.Person);
        }
    }
    

    If using .NET Framework a BindingNavigator is placed on the form to make standard operations easy while with .NET Core the BindingNavigator is missing. Add the follow class to get the BindingNavigator in a .NET Core project.

    public class CoreBindingNavigator : BindingNavigator
    {
    	public CoreBindingNavigator()
    	{
    		AddStandardItems();
    	}
    }
    

    And the Question dialog

    public static class Dialogs
    {
        public static bool Question(string text) =>
            (MessageBox.Show(
                text,
                Application.ProductName,
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Question,
                MessageBoxDefaultButton.Button2) == DialogResult.Yes);
    }
    
    0 comments No comments