当我调用DataRowCollection.Add

时间:2018-11-20 05:20:20

标签: c# datatable system.data.sqlite indexoutofrangeexception

在以下代码中:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SQLite;
using System.Diagnostics;

namespace WindowsFormsApplication1
{
    public partial class List : Form
    {
        public List()
        {
            InitializeComponent();
        }

        private void List_Load(object sender, EventArgs e)
        {
            comboBox1.SelectedIndex = 1;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            var cmd = new SQLiteCommand("select * from students", DB.Connection);
            var ad = new SQLiteDataAdapter(cmd);
            var dt = new DataTable();
            ad.Fill(dt);
            listBox1.DataSource = dt;
            listBox1.DisplayMember = "name";
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Debug.Assert(listBox1.DataSource is DataTable);
            var dt = listBox1.DataSource as DataTable;
            dt.Rows.Add();
            btnEdit.Enabled = btnRemove.Enabled = dt.Rows.Count != 0;
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            Debug.Assert(listBox1.SelectedItem is DataRowView);
            new StudentsForm2(listBox1.SelectedItem as DataRowView).ShowDialog(this);
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            Debug.Assert(listBox1.DataSource is DataTable);
            var dt = listBox1.DataSource as DataTable;
            dt.Rows.RemoveAt(listBox1.SelectedIndex);
            btnEdit.Enabled = btnRemove.Enabled = dt.Rows.Count != 0;
        }

        private void List_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                Close();
            }
        }

    }
}

表为空时,出现以下异常:

  

System.Windows.Forms.dll中发生了类型'System.ArgumentOutOfRangeException'的第一次机会异常

     

其他信息:InvalidArgument =值“ 0”对于“ SelectedIndex”无效。

在btnAdd_Click方法中的

调用dt.Rows.Add()时。 comboBox1保存表名,但是由于我目前只有一个表,除非要调用comboBox1_SelectedIndexChanged,否则它将不使用。

这是抛出异常时的堆栈跟踪:

  

在System.Environment.GetStackTrace(异常e,布尔的NeedFileInfo)   在System.Environment.get_StackTrace()   在System.Windows.Forms.ListBox.set_SelectedIndex(Int32值)   在System.Windows.Forms.ListControl.DataManager_PositionChanged(对象发送者,EventArgs e)   在System.Windows.Forms.CurrencyManager.OnPositionChanged(EventArgs e)   在System.Windows.Forms.CurrencyManager.ChangeRecordState(Int32 newPosition,布尔验证,布尔endCurrentEdit,布尔firePositionChange,布尔pullData)   在System.Windows.Forms.CurrencyManager.List_ListChanged(对象发送者,ListChangedEventArgs e)   在System.Data.DataView.OnListChanged(ListChangedEventArgs e)   在System.Data.DataView.IndexListChanged(对象发送者,ListChangedEventArgs e)   在System.Data.DataView.IndexListChangedInternal(ListChangedEventArgs e)   在System.Data.DataViewListener.IndexListChanged(ListChangedEventArgs e)   在System.Data.Index。<> c.b__88_0(DataViewListener侦听器,ListChangedEventArgs args,布尔arg2,布尔arg3)   在System.Data.Listeners 1.Notify[T1,T2,T3](T1 arg1, T2 arg2, T3 arg3, Action 4操作)   在System.Data.Index.OnListChanged(ListChangedEventArgs e)   在System.Data.Index.OnListChanged(ListChangedType更改类型,Int32索引)   在System.Data.Index.InsertRecord(Int32记录,布尔型FireEvent)   在System.Data.Index.ApplyChangeAction(Int32记录,Int32动作,Int32 changeRecord)   在System.Data.Index.RecordStateChanged(Int32记录,DataViewRowState oldState,DataViewRowState newState)   在System.Data.DataTable.RecordStateChanged(Int32 record1,DataViewRowState oldState1,DataViewRowState newState1,Int32 record2,DataViewRowState oldState2,DataViewRowState newState2)   在System.Data.DataTable.SetNewRecordWorker处(DataRow行,Int32建议的记录,DataRowAction操作,布尔值isInMerge,布尔值detectEnsurePropertyChanged,Int32位置,布尔值fireEvent,Exception&deferredException)   在System.Data.DataTable.InsertRow(DataRow行,Int64提议的ID,Int32 pos,布尔型fireEvent)   在System.Data.DataRowCollection.Add(Object []值)   在WindowsFormsApplication1.List.btnAdd_Click(Object sender,EventArgs e)中的C:\ Users \ hamidi \ Documents \ Visual Studio 2010 \ Projects \ m_rezaee \ WindowsFormsApplication1 \ WindowsFormsApplication1 \ List.cs:line 44   在System.Windows.Forms.Control.OnClick(EventArgs e)   在System.Windows.Forms.Button.OnClick(EventArgs e)   在System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)   在System.Windows.Forms.Control.WmMouseUp上(消息和m,MouseButtons按钮,Int32单击)   在System.Windows.Forms.Control.WndProc(Message&m)   在System.Windows.Forms.ButtonBase.WndProc(Message&m)   在System.Windows.Forms.Button.WndProc(Message&m)   在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&m)   在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&m)   在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)   在System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&msg)   在System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID,Int32原因,Int32 pvLoopData)   在System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32原因,ApplicationContext上下文)   在System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32原因,ApplicationContext上下文)   在System.Windows.Forms.Application.Run(Form mainForm)   在WindowsFormsApplication1.Program.Main()中的C:\ Users \ hamidi \ Documents \ Visual Studio 2010 \ Projects \ m_rezaee \ WindowsFormsApplication1 \ WindowsFormsApplication1 \ Program.cs:line 18   在System.AppDomain._nExecuteAssembly(RuntimeAssembly程序集,String []参数)   在System.AppDomain.ExecuteAssembly(String assemblyFile,Evidence assemblySecurity,String [] args)中   在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()   在System.Threading.ThreadHelper.ThreadStart_Context(对象状态)   在System.Threading.ExecutionContext.RunInternal(ExecutionContext executeContext,ContextCallback回调,对象状态,布尔类型saveSyncCtx)   在System.Threading.ExecutionContext.Run(ExecutionContext执行上下文,ContextCallback回调,对象状态,布尔类型saveSyncCtx)   在System.Threading.ExecutionContext.Run(ExecutionContext执行上下文,ContextCallback回调,对象状态)   在System.Threading.ThreadHelper.ThreadStart()

我找不到有关其发生原因的任何线索。

0 个答案:

没有答案