?? readerlist.cs
字號:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
namespace Library
{
/// <summary>
/// UserList 的摘要說明。
/// </summary>
public class ReaderList : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Button btnAddReader;
private System.Windows.Forms.Button btnEditReader;
private System.Windows.Forms.Button btnDeleteReader;
private System.Windows.Forms.Button btnRefresh;
private System.Windows.Forms.Button btnOutput;
private System.Windows.Forms.Button btnClose;
private System.Windows.Forms.ListView lvReaders;
/// <summary>
/// 必需的設計器變量。
/// </summary>
private System.ComponentModel.Container components = null;
private bool reset = false;
public ReaderList()
{
//
// Windows 窗體設計器支持所必需的
//
InitializeComponent();
initializeScreen();
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
/// <summary>
/// 定義方法GetData(),返回一個數據表
/// </summary>
/// <returns>返回DataTable</returns>
public static System.Data.DataTable GetData(string view)
{
SqlDataAdapter adapter = new SqlDataAdapter("select * from " + view,Global.conn);
DataSet ds= new DataSet();
try
{
if(Global.conn.State!=ConnectionState.Open)
Global.conn.Open();
adapter.Fill(ds);
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
return ds.Tables[0];
}
/// <summary>
/// 初始化
/// </summary>
private void initializeScreen()
{
ListViewItem item = null;
int colIndex;
lvReaders.Items.Clear();
System.Data.DataTable tableTemp = GetData("讀者信息");
if(!reset)
{
//將所得到的表的列名,賦值給單元格
foreach(DataColumn col in tableTemp.Columns)
{
lvReaders.Columns.Add(col.ColumnName,-2,HorizontalAlignment.Left);
}
}
//同樣方法處理數據
foreach(DataRow row in tableTemp.Rows)
{
item = new ListViewItem(row[0].ToString());
for(colIndex= 1;colIndex<tableTemp.Columns.Count;colIndex++)
{
item.SubItems.Add(row[colIndex].ToString());
}
lvReaders.Items.Add(item);
}
tableTemp.Dispose();
}
#region Windows 窗體設計器生成的代碼
/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.btnClose = new System.Windows.Forms.Button();
this.btnOutput = new System.Windows.Forms.Button();
this.btnRefresh = new System.Windows.Forms.Button();
this.btnDeleteReader = new System.Windows.Forms.Button();
this.btnEditReader = new System.Windows.Forms.Button();
this.btnAddReader = new System.Windows.Forms.Button();
this.lvReaders = new System.Windows.Forms.ListView();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// label1
//
this.label1.BackColor = System.Drawing.Color.WhiteSmoke;
this.label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label1.Font = new System.Drawing.Font("宋體", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label1.Location = new System.Drawing.Point(283, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(216, 23);
this.label1.TabIndex = 8;
this.label1.Text = "讀 者 管 理";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// groupBox1
//
this.groupBox1.Controls.Add(this.btnClose);
this.groupBox1.Controls.Add(this.btnOutput);
this.groupBox1.Controls.Add(this.btnRefresh);
this.groupBox1.Controls.Add(this.btnDeleteReader);
this.groupBox1.Controls.Add(this.btnEditReader);
this.groupBox1.Controls.Add(this.btnAddReader);
this.groupBox1.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.groupBox1.Location = new System.Drawing.Point(24, 416);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(736, 72);
this.groupBox1.TabIndex = 10;
this.groupBox1.TabStop = false;
//
// btnClose
//
this.btnClose.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.btnClose.Location = new System.Drawing.Point(583, 24);
this.btnClose.Name = "btnClose";
this.btnClose.TabIndex = 5;
this.btnClose.Text = "關閉";
this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
//
// btnOutput
//
this.btnOutput.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.btnOutput.Location = new System.Drawing.Point(482, 24);
this.btnOutput.Name = "btnOutput";
this.btnOutput.TabIndex = 4;
this.btnOutput.Text = "導出";
this.btnOutput.Click += new System.EventHandler(this.btnOutput_Click);
//
// btnRefresh
//
this.btnRefresh.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.btnRefresh.Location = new System.Drawing.Point(381, 25);
this.btnRefresh.Name = "btnRefresh";
this.btnRefresh.TabIndex = 3;
this.btnRefresh.Text = "刷新";
this.btnRefresh.Click += new System.EventHandler(this.btnRefresh_Click);
//
// btnDeleteReader
//
this.btnDeleteReader.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.btnDeleteReader.Location = new System.Drawing.Point(280, 25);
this.btnDeleteReader.Name = "btnDeleteReader";
this.btnDeleteReader.TabIndex = 2;
this.btnDeleteReader.Text = "刪除";
this.btnDeleteReader.Click += new System.EventHandler(this.btnDeleteReader_Click);
//
// btnEditReader
//
this.btnEditReader.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.btnEditReader.Location = new System.Drawing.Point(179, 25);
this.btnEditReader.Name = "btnEditReader";
this.btnEditReader.TabIndex = 1;
this.btnEditReader.Text = "修改";
//
// btnAddReader
//
this.btnAddReader.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.btnAddReader.Location = new System.Drawing.Point(78, 24);
this.btnAddReader.Name = "btnAddReader";
this.btnAddReader.TabIndex = 0;
this.btnAddReader.Text = "添加";
this.btnAddReader.Click += new System.EventHandler(this.btnAddReader_Click);
//
// lvReaders
//
this.lvReaders.FullRowSelect = true;
this.lvReaders.GridLines = true;
this.lvReaders.Location = new System.Drawing.Point(24, 48);
this.lvReaders.Name = "lvReaders";
this.lvReaders.Size = new System.Drawing.Size(736, 360);
this.lvReaders.TabIndex = 11;
this.lvReaders.View = System.Windows.Forms.View.Details;
//
// ReaderList
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(790, 503);
this.ControlBox = false;
this.Controls.Add(this.lvReaders);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.label1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "ReaderList";
this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
this.Text = "讀者管理";
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private void btnDeleteReader_Click(object sender, System.EventArgs e)
{
if(lvReaders.SelectedItems.Count<=0)
{
MessageBox.Show("請選擇需要刪除的記錄!可以多選!");
return;
}
if(MessageBox.Show("您確定要刪除被選的"+lvReaders.SelectedItems.Count.ToString() + "條記錄?\n注意:此處并非真正刪除讀者記錄,而是相當于注銷借書證!","詢問",MessageBoxButtons.YesNo)==DialogResult.Yes)
{
string sqlstr = "";
for(int i=0;i<lvReaders.SelectedItems.Count;i++)
{
sqlstr = "update 借書證 set 借書證狀態=1 where 借書證號='" + lvReaders.SelectedItems[i].SubItems[0].Text + "'";
SqlCommand cmd = new SqlCommand(sqlstr,Global.conn);
try
{
cmd.ExecuteNonQuery();
}
catch(Exception ex)
{
MessageBox.Show("刪除記錄時發生錯誤!錯誤信息:" + ex.Message);
return;
}
}
initializeScreen();
}
}
private void btnOutput_Click(object sender, System.EventArgs e)
{
Microsoft.Office.Interop.Excel.Application excel= new Microsoft.Office.Interop.Excel.Application();
int rowIndex=1;
int colIndex=0;
excel.Application.Workbooks.Add(true);
System.Data.DataTable tableTemp = GetData("讀者信息");
//將所得到的表的列名,賦值給單元格
foreach(DataColumn col in tableTemp.Columns)
{
colIndex++;
excel.Cells[1,colIndex]=col.ColumnName;
}
//同樣方法處理數據
foreach(DataRow row in tableTemp.Rows)
{
rowIndex++;
colIndex=0;
foreach(DataColumn col in tableTemp.Columns)
{
colIndex++;
excel.Cells[rowIndex,colIndex]=row[col.ColumnName].ToString();
}
}
MessageBox.Show("成功導出記錄!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
//不可見,即后臺處理
excel.Visible=true;
}
private void btnClose_Click(object sender, System.EventArgs e)
{
Global.sbpGlobal.Text = "就緒";
this.Close();
}
private void btnRefresh_Click(object sender, System.EventArgs e)
{
reset = true;
initializeScreen();
}
private void btnAddReader_Click(object sender, System.EventArgs e)
{
AddNewCard addNewCard = new AddNewCard();
addNewCard.ShowDialog();
reset = false;
initializeScreen();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -