[C#] RemoveAt index out of bounds problem

Series of articles

【C#】Single number generator (numbering rules, fixed characters, serial numbers, generating business single numbers)
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/129129787

[C#] Date range generator (start date, end date)
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/129040663

[C#] Component development, call dll component method
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/129492112

[C#] Use of data entity classes
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/128816638

[C#] Document approval flow scheme
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/128972545

[C#] QR code label making and printing
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/126884228

[C#] The most complete document printing source code (design printing template, barcode & amp; two-dimensional code, label, font)
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/129415723

[C#] Barcode Management Operation Manual
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/126589496

[C#] WebAPI publishing and exception handling under the IIS platform
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/126539836

[C#] [Improve Programming Efficiency] Code Template Generation Tool
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/126890673

[C#] [Improve programming efficiency] Import Excel data into the database in batches
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/126427323

[C#] Windows service (Service) installation and start and stop scheme
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/124053794

[C#] Penetrate Session isolation, service call external program (no form interface solution)
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/124053033

[C#] Task plan implementation, using the Quartz class
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/123667723

[C#] “Weekly plan management about prenatal preparation module” solution 20200203
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/122919543

[C#] Source code parsing regular expression
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/118337074

[C#] Software version and file MD5 records (XML operation)
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/112513871

[C#] Test whether the network is connected
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/110137288

[C#] Get the code according to the name (Dictionary get key method)
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/129816701

[C#] Data modeling, do you use DataTable or List?
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/129792726

[C#] Two-way binding between GridControl control and List data set
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/129423755

[C#] GridControl dynamically replaces DataSource, data query exception handling
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/130305424

Article directory

  • series of articles
  • foreword
  • 1. Problem description
  • Two, the solution
  • 3. Software development (source code)
  • 4. Project display
  • 5. Resource Links

Foreword

I can abstract the whole world, but I cannot abstract you. Wants to make you a private constant so outside functions can’t access you. I also want you to be a global constant so that I can call you throughout my lifetime. It’s a pity that there is no such constant in the world, and I can’t define you, because you are so specific in my heart.

Hello everyone, this column is the [Project Actual Combat] column, which is different from the [Underlying Library] column. We can find that the chapters “Problem Description” and “Project Display” have been added, which is very in line with the project development process and allows readers to understand the project more clearly. The problem to be solved, and the effect that the product can achieve. This column contains solutions to the project development process, which is the refinement of relatively mature and reliable methods for my project development. I will sort out the solutions to these problems and write this article to share with you. If you encounter similar problems, you can deal with them according to the solution in this article.

This column will continue to be updated and improved, and the column articles are weakly related (the dependencies between articles are weak, and there is no reading order). If you have any questions, you can private message me. If you are interested in this column, please pay attention. I will show you how to use the most concise code to realize complex functions.

·Reminder: This column is a project actual combat chapter, and students who have not been in touch with project development may have difficulty understanding it, so it is not recommended to read it.
3A0N000001

1. Problem description

//Function content: menu button, delete row
        //Function version: 0.0.0.1
        //Modification time: 2023.05.12
        //================================================== ===============================================
        //Precautions
        //  1.  
        //  2.  
        //================================================== ===============================================
        private void Mi_DeleteRow_Click(object sender, EventArgs e)
        {<!-- -->
            if (MessageBox.Show("Are you sure to delete the selected data?", "Ask",
                 MessageBoxButtons.YesNo, MessageBoxIcon.Question,
                 MessageBoxDefaultButton.Button1) == DialogResult.No)
            {<!-- -->
                return;
            }

            GridView_Moudle. CloseEditor();
            GridView_Moudle. PostEditor();

            //Clone the bound data source. The original data source is deleted one by one, and the index is out of bounds.
            DataTable dr_Select = Dt_Excel. Clone();

            //Get the selected line number
            int[] int_Rows = GridView_Moudle. GetSelectedRows();
            for (int i = 0; i < int_Rows. Length; i ++ )
            {<!-- -->
                Dt_Excel.Rows.RemoveAt(int_Rows[i]);
                Dt_Excel. AcceptChanges();

                GridControl_Moudle.DataSource = Dt_Excel;
                GridControl_Moudle. RefreshDataSource();
            }
        }


?

2. Solutions

method one:
DataTable does not use RemoveAt to delete multiple rows of data.

Method Two:
DataTable first clones the data source, deletes multiple rows of data, and then rebinds the data source.

3. Software development (source code)

4. Project display

5. Resource link