none
Updating data grid column values on a button click RRS feed

  • Question

  • In the data grid all the values are manually entered by the user also the record number. ("No" column)

    But if the user deletes a record from the middle, how can i update the "No" column in the correct order.

    • Moved by CoolDadTx Thursday, December 5, 2019 3:02 PM Winforms related
    Thursday, December 5, 2019 4:23 AM

All replies

  • Hi Thushan Withanage,
    I noticed that you mentioned "datagrid" in your description, and this control is used in many project types. Could you explain what your project type is?  Winforms? WPF? ASP.NET? or any other type?
    Best Regards,
    Daniel Zhang

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, December 5, 2019 6:26 AM
  • Not knowing how the DataGridView is setup the following example uses a DataTable with defined columns where the DataTable becomes the DataSource of a BindingSource then the BindingSource becomes the data source for a DataGridView. Best practices is to load a DataGridView with a DataTable or a List<T> which then go to the BindingSource to the DataGridView which allows access to the underlying data anytime rather then deal with cells of a DataGridView. Although I have not included doing the math for Quantity + Price for row sub-total that is easy using a DataColumn Expression

    The key aspect here is to (since there is no mention of a button to perform the row removal) subscribge to the UserDeletedRow event of the DataGridView, determine if there are row, if so reorder the rows as the row is now gone.

    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;
    
    namespace WindowsFormsApp1
    {
        public partial class Form1 : Form
        {
            private readonly BindingSource _bindingSource = 
                new BindingSource();
    
            public Form1()
            {
                InitializeComponent();
                Shown += Form1_Shown;
            }
    
            private void Form1_Shown(object sender, EventArgs e)
            {
                var table = new DataTable();
                table.Columns.Add(new DataColumn()
                {
                    ColumnName = "No",
                    DataType = typeof(int),
                    AllowDBNull = false
                });
    
                table.Columns.Add(new DataColumn()
                    { ColumnName = "ItemNumber",
                        DataType = typeof(string)
                    });
    
                _bindingSource.DataSource = table;
                dataGridView1.DataSource = _bindingSource;
    
                dataGridView1.Columns["ItemNumber"]
                    .HeaderText = "Item Number";
            }
    
            private void dataGridView1_UserDeletedRow(object sender, DataGridViewRowEventArgs e)
            {
                if (_bindingSource.Count <= 0) return;
    
                var table = (DataTable) _bindingSource.DataSource;
    
                for (var rowIndex = 0; rowIndex < table.Rows.Count; rowIndex++)
                {
                    table.Rows[rowIndex].SetField("No", rowIndex +1);
                }
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, December 5, 2019 12:42 PM
    Moderator
  • It's a Windows form application.
    Thursday, December 5, 2019 5:59 PM
  • Hi Thushan Withanage,
    As Kareninstructor said, you can use DataTable. And her code is correct you can refer to.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, December 9, 2019 8:54 AM