none
DataTable internal index is corrupted: '5' from row = ((DataRowView)this.regexDgv.CurrentRow.DataBoundItem).Row; RRS feed

  • Question

  • how can I get rid of this index error?

    does (DatatRowView) causes write operation on the table?

    Wednesday, August 14, 2019 5:59 AM

All replies

  • Hi fs-ab,

    Thank you for posting here.

    I try to use the following code to reproduce your problem, but failed.

    private void Form1_Load(object sender, EventArgs e)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("Name", typeof(string));
                dt.Columns.Add("Age", typeof(int));
                dt.Rows.Add("Test1", 22);
                dt.Rows.Add("Test2", 23);
                this.dataGridView1.DataSource = dt;
            }
            private void Button1_Click(object sender, EventArgs e)
            {
                var data = ((DataRowView)this.dataGridView1.CurrentRow.DataBoundItem).Row;
            }

    I get the normal result instead of the exception.

    Therefore, I hope that you could provide a completed code.

    Best Regards,

    Jack


    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.

    Wednesday, August 14, 2019 6:54 AM
    Moderator
  • Thx for replying.

     the datagridview is bounded to a microsoft sql express database table not a stand alone table in memory

      private void regexBindingNavigatorSaveItem_Click(object sender, EventArgs e)
      {
       bNavigatorSaveItemInProgress = true;
       regexBindingNavigatorSaveItem.Enabled = false;  // avoid unnecessary repeat from validate event
       setStatus("Start saving Regex...name=" + regexTextBox.Text);

       if (!this.Validate())
       { // 100822 added:
        addMsg("Not saved due to failed validation - make sure there is value for group, regexname and regex\r\nTO cancel change  use the Cancel chane (Crossed ED button) or complete reload");
        return;
       }

       DataRow row; try // 100805 added exception handling from save after del of last row { row = ((DataRowView)this.regexDgv.CurrentRow.DataBoundItem).Row; //090903 } catch (IndexOutOfRangeException eIor) { MessageBox.Show(eIor.Message + "\r\nstack trace" + eIor.StackTrace , "regexBindingNavigatorSaveItem_Click error - should reload to avoid corruption"); return; //row = ((DataRowView)this.regexDgv.CurrentRow).Row; } catch (InvalidOperationException eioe) { MessageBox.Show(eioe.Message, "regexBindingNavigatorSaveItem_Click - please reload"); return; }




    • Edited by fs - ab Wednesday, August 14, 2019 5:47 PM
    Wednesday, August 14, 2019 5:35 PM
  • Here is a different approach using mocked data to keep things simple.

    using System;
    using System.Data;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp1
    {
        /// <summary>
        /// DataGridView columns are defined in the
        /// designer of the DataGridView
        /// </summary>
        public partial class Form1 : Form
        {
            private BindingSource _bindingSource = 
                new BindingSource();
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                var dt = new DataTable();
                dt.Columns.Add(new DataColumn()
                    { ColumnName = "FirstName", DataType = typeof(string)});
    
                dt.Columns.Add(new DataColumn()
                    { ColumnName = "LastName", DataType = typeof(string)});
    
                dt.Rows.Add("Karen", "Payne");
    
                dataGridView1.AllowUserToAddRows = false;
                dataGridView1.AutoGenerateColumns = false;
    
                _bindingSource.DataSource = dt;
                dataGridView1.DataSource = _bindingSource;
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                if (dataGridView1.CurrentRow == null) return;
    
                var row = ((DataRowView) _bindingSource.Current).Row;
                var firstName = row.Field<string>("FirstName");
                var lastName = row.Field<string>("LastName");
    
                MessageBox.Show($"{firstName} {lastName}");
            }
        }
    }
    


    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

    Wednesday, August 14, 2019 5:51 PM
    Moderator