| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4135 人关注过本帖
标题:程序修改之后,点击button按钮,为什么没有反应??
只看楼主 加入收藏
小丑鱼爱丘
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2010-5-30
结帖率:0
收藏
 问题点数:0 回复次数:6 
程序修改之后,点击button按钮,为什么没有反应??
将DataGrid1中的内容通过button2导入Excel。我的表一共有四列,编号、课程、成绩、学期。按照大虾们指点,修改成这样了,不过现在程序运行时没有错误,但是点击按钮确没有反应。请高手们看看 怎么修改?请说的具体点,指出要改的地方!本人非常感谢!!!!!赶快有人来帮帮我吧!!
   

程序主要代码如下:
using System;
using System.Drawing;
using System.Collections;
using
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data;
using Excel = Microsoft.Office.Interop.Excel;
  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;
  private components = null;

  public ScoreQuery()
  {
  InitializeComponent();

  }

  protected override void Dispose(bool disposing)
  {
  if (disposing)
  {
  if (components != null)
  {
  components.Dispose();
  }
  }
  base.Dispose(disposing);
  }

  private void InitializeComponent()
  {
  // button2
  //  
  this.button2.Location = new System.Drawing.Point(474, 11);
  this.button2.Name = "button2";
  this.button2.Size = new System.Drawing.Size(88, 23);
  this.button2.TabIndex = 10;
  this.button2.Text = "打印成绩列表";
  this.button2.UseVisualStyleBackColor = true;
  this.button2.Click += new System.EventHandler(this.button2_Click);
  // dataGrid1
  //  
  this.dataGrid1.AllowSorting = false;
  this.dataGrid1.BackgroundColor = System.Drawing.SystemColors.Control;
  this.dataGrid1.CaptionBackColor = System.Drawing.SystemColors.ActiveCaptionText;
  this.dataGrid1.CaptionFont = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
  this.dataGrid1.CaptionForeColor = System.Drawing.SystemColors.ControlText;
  this.dataGrid1.CaptionText = "成绩列表";
  this.dataGrid1.DataMember = "";
  this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
  this.dataGrid1.Location = new System.Drawing.Point(8, 64);
  this.dataGrid1.Name = "dataGrid1";
  this.dataGrid1.PreferredColumnWidth = 100;
  this.dataGrid1.ReadOnly = true;
  this.dataGrid1.Size = new System.Drawing.Size(464, 152);
  this.dataGrid1.TabIndex = 6;
  // ScoreQuery
  //  
  this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  this.ClientSize = new System.Drawing.Size(640, 354);
  this.Controls.Add(this.dataGrid2);
  this.Controls.Add(this.dataGrid1);
  this.Controls.Add(this.groupBox1);
  this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
  this.Name = "ScoreQuery";
  this.Text = "学生成绩查询";
  this.Load += new System.EventHandler(this.ScoreQuery_Load);
  this.groupBox1.ResumeLayout(false);
  this.groupBox1.PerformLayout();
  (()(this.numericUpDown2)).EndInit();
  (()(this.numericUpDown1)).EndInit();
  (()(this.dataGrid1)).EndInit();
  (()(this.dataGrid2)).EndInit();
  this.ResumeLayout(false);

  }
  #endregion
  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. = 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)
  {
  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");
  AddExcel(ds);

  }

  protected void AddExcel(DataSet ds)
  {
  DataTable dt = ds.Tables["score"];
  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)
  {

  }

  }
}
搜索更多相关主题的帖子: button 按钮 
2010-05-31 12:54
daiwenyi2004
Rank: 2
等 级:论坛游民
帖 子:3
专家分:10
注 册:2010-6-5
收藏
得分:0 
你跟踪button2的事件看看,是不是绑定了click事件,
如果绑定了,你逐个监视数据量,看数据读取是否正常,
一步一步调试
2010-06-05 10:39
ouxianzhi520
Rank: 8Rank: 8
来 自:重庆
等 级:蝙蝠侠
威 望:3
帖 子:245
专家分:932
注 册:2007-12-1
收藏
得分:0 
你的button2 注册的事件是
this.button2.Click += new System.EventHandler(this.button2_Click);
但是你程序中没有看到button2_Click的定义,只有button1_Click的定义。或许是这里 有问题

[url=http://blog./ouxianzhi520]csdn个人博客[/url]
2010-06-05 22:55
ggminjun
Rank: 2
等 级:论坛游民
威 望:1
帖 子:18
专家分:40
注 册:2010-6-4
收藏
得分:0 
到窗体设计界面,重新双击那个button按钮,让它自动重新生成事件
2010-06-07 13:20
zhenxiaoyan
Rank: 2
等 级:论坛游民
帖 子:22
专家分:40
注 册:2009-8-24
收藏
得分:0 
设置断点调试看看
2010-06-20 20:17
zxh66
Rank: 1
等 级:新手上路
帖 子:4
专家分:4
注 册:2010-6-23
收藏
得分:0 
以下是引用ggminjun在2010-6-7 13:20:30的发言:

到窗体设计界面,重新双击那个button按钮,让它自动重新生成事件
到窗体设计界面,重新双击那个button按钮,让它自动重新生成事件
2010-06-25 17:16
liu30211
Rank: 4
来 自:江苏苏州
等 级:业余侠客
威 望:1
帖 子:73
专家分:258
注 册:2008-6-10
收藏
得分:0 
重新双击按钮看不是事件弄错了

有做VOD的朋友
联系

VOD播发控件 OSD控件 双屏控件 手写控件
2010-06-26 21:52
快速回复:程序修改之后,点击button按钮,为什么没有反应??
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.029825 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved