程序调试没有错误出现,为什么点击button2 没有反应呢???红色部分。请各位大虾帮忙改改啊 !!!!!
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data;
using Excel = Microsoft.Office.Interop.Excel;
namespace WindowsApplication1
{
    /// <summary>
    /// ScoreQuery 的摘要说明。
    /// </summary>
    public class ScoreQuery : System.Windows.Forms.Form
    {
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.NumericUpDown numericUpDown1;
        private System.Windows.Forms.NumericUpDown numericUpDown2;
        private System.Windows.Forms.TextBox textBox2;
        private System.Windows.Forms.TextBox textBox3;
        private System.Windows.Forms.DataGrid dataGrid1;
        private System.Data.OleDb.OleDbConnection oleDbConnection1;
        private System.Windows.Forms.DataGrid dataGrid2;
        private System.Windows.Forms.Button button2;
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.Container components = null;        public ScoreQuery()
        {
            //
            // Windows 窗体设计器支持所必需的
            //
            InitializeComponent();            //
            // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
            //
        }        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }
 public bool strisyear(string s)
        {
            if (s.Trim().Length != 4)
                return (false);
            else
            {
                int n = 1;
                for (int i = 0; i <= s.Length - 1; i++)
                {
                    if (!System.Char.IsNumber(s[i]))
                    {
                        n = -1;
                        break;
                    }
                }
                if (n == 1)
                    return (true);
                else
                    return (false);
            }
        }        private void button1_Click(object sender, System.EventArgs e)
        {
            if (!strisyear(textBox2.Text.Trim()) || !strisyear(textBox3.Text.Trim()))
                MessageBox.Show("年份输入非法");
            else if (textBox1.Text.Trim() == "")
            {
                MessageBox.Show("学号不能为空");
            }
            else if (int.Parse(textBox2.Text.Trim()) <= int.Parse(textBox3.Text.Trim()) && numericUpDown1.Value <= numericUpDown2.Value)
            {
                string sql, xq1, xq2;
                xq1 = textBox2.Text.Trim() + numericUpDown1.Value.ToString();
                xq2 = textBox3.Text.Trim() + numericUpDown2.Value.ToString();
                sql = "select ScoreId as 编号,Course as 课程,Score as 成绩,cstr(cint(Semester/10))+'-'+cstr(cint(Semester/10)+1)+'年度第'+cstr(Semester mod 10)+'学期' as 学期 from ScoreInfo where StudentNumber='" + textBox1.Text.Trim() + "' and Semester between '" + xq1 + "'and '" + xq2 + "' order by Semester desc";
                OleDbDataAdapter adp = new OleDbDataAdapter(sql, oleDbConnection1);
                DataSet ds = new DataSet();
                adp.Fill(ds, "score");
                if (ds.Tables["score"].Rows.Count != 0)
                {
                    dataGrid1.DataSource = ds.Tables["score"].DefaultView;
                    dataGrid1.CaptionText = "共检索出" + ds.Tables["score"].Rows.Count + "门课程";
                    for (int i = 0; i <= ds.Tables["score"].Rows.Count - 1; i++)
                        dataGrid1[i, 0] = i + 1;                    sql = "select cstr(cint(Semester/10))+'-'+cstr(cint(Semester/10)+1)+'年度第'+cstr(Semester mod 10)+'学期' as 学期,avg(Score) as 平均成绩,sum(Score) as 总成绩,count(ScoreId) as 课程数量  from ScoreInfo where StudentNumber='" + textBox1.Text.Trim() + "'and Semester between '" + xq1 + "'and '" + xq2 + "' group by Semester";
                    adp.SelectCommand.CommandText = sql;
                    DataSet ds2 = new DataSet();
                    adp.Fill(ds, "tj");
                    dataGrid2.DataSource = ds.Tables["tj"].DefaultView;
                }
                else
                {
                    dataGrid1.DataSource = null;
                    dataGrid1.CaptionText = "暂时无纪录";
                }
            }
            else
            {
                MessageBox.Show("请在左边输入较小的学期数值");
            }
        }        private void button2_Click(object sender, System.EventArgs e)
        {            DataSet ds = new DataSet();
            AddExcel(ds);        }        protected void AddExcel(DataSet ds)
        {
            DataTable dt = ds.Tables[0];
            string fileName = Guid.NewGuid() + ".xls";
            Excel.Application excel = new Excel.ApplicationClass();
            int rowIndex = 1;
            int colIndex = 0;
            excel.Application.Workbooks.Add(true);            foreach (DataColumn col in dt.Columns)
            {
                colIndex++;
                excel.Cells[1, colIndex] = col.ColumnName;
            }
            foreach (DataRow row in dt.Rows)
            {
                rowIndex++;
                colIndex = 0;
                for (colIndex = 0; colIndex < dt.Columns.Count; colIndex++)
                {
                    excel.Cells[rowIndex, colIndex + 1] = row[colIndex].ToString();
                }
            }
            excel.Visible = false;
            excel.ActiveWorkbook.SaveAs(fileName, Excel.XlFileFormat.xlExcel9795, null, null, false, false, Excel.XlSaveAsAccessMode.xlNoChange, null, null, null, null, null);
            excel.Quit();
            excel = null;
            GC.Collect();
        }
        private void ScoreQuery_Load(object sender, EventArgs e)
        {        }    }
}

解决方案 »

  1.   

    button2 事件中是否绑定了button2_Click
    单步跟踪
      

  2.   

    我按照1楼说的,绑定了。但是又出现错误了 
    未处理 System.IndexOutOfRangeException
      Message="无法找到表 0。"
      Source="System.Data"
      StackTrace:
           在 System.Data.DataTableCollection.get_Item(Int32 index)
           在 WindowsApplication1.ScoreQuery.AddExcel(DataSet ds) 位置 C:\Documents and Settings\Administrator\桌面\学生管理系统\ScoreQuery.cs:行号 360
           在 WindowsApplication1.ScoreQuery.button2_Click(Object sender, EventArgs e) 位置 C:\Documents and Settings\Administrator\桌面\学生管理系统\ScoreQuery.cs:行号 354
           在 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(Message& m, MouseButtons button, Int32 clicks)
           在 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(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
           在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
           在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
           在 System.Windows.Forms.Application.RunDialog(Form form)
           在 System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
           在 System.Windows.Forms.Form.ShowDialog()
           在 WindowsApplication1.Login.button1_Click(Object sender, EventArgs e) 位置 C:\Documents and Settings\Administrator\桌面\学生管理系统\Login.cs:行号 234
           在 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(Message& m, MouseButtons button, Int32 clicks)
           在 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(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
           在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
           在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
           在 System.Windows.Forms.Application.Run(Form mainForm)
           在 WindowsApplication1.MainForm.Main() 位置 C:\Documents and Settings\Administrator\桌面\学生管理系统\MainForm.cs:行号 370
           在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
           在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           在 System.Threading.ThreadHelper.ThreadStart()
    这个表 0  是要自己建的吗??怎么建??我这个表共有4列,编号、课程、成绩、学期!怎么改啊 ???帮忙看看 !!
      

  3.   

    F5跟踪,设置断点,如果进不来事件,就说明button事件没声明嘛,
    到这种文件里看一下web应用程序Default.aspx.designer.cs,声明了没有
    如果建的不是web应用程序,是网站的话,aspx页面上就有
    最简单的法子就是把button删了,拖一个新button进来