?? booksinfo.cs
字號:
this.Controls.Add(this.label1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "BooksInfo";
this.ShowInTaskbar = false;
this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
this.Text = "圖書資料";
this.Load += new System.EventHandler(this.MakeCatalogue_Load);
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.groupBox3.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 定義方法GetData(),返回一個數(shù)據(jù)表
/// </summary>
/// <returns>返回DataTable</returns>
public System.Data.DataTable GetData()
{
SqlDataAdapter adapter = new SqlDataAdapter("select * from V_圖書資料",Global.conn);
DataSet ds= new DataSet();
try
{
adapter.Fill(ds);
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
return ds.Tables[0];
}
/// <summary>
/// 定義方法GetData(),返回一個數(shù)據(jù)表
/// </summary>
/// <returns>返回DataTable</returns>
public System.Data.DataTable GetData2(string sel)
{
SqlDataAdapter adapter = new SqlDataAdapter(sel,Global.conn);
DataSet ds= new DataSet();
try
{
adapter.Fill(ds);
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
return ds.Tables[0];
}
/// <summary>
/// 關(guān)閉
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnClose_Click(object sender, System.EventArgs e)
{
Global.sbpGlobal.Text = "就緒";
Global.BooksInfo = false;
this.Close();
}
/// <summary>
/// 窗口加載代碼
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MakeCatalogue_Load(object sender, System.EventArgs e)
{
cmd = Global.conn.CreateCommand();
}
/// <summary>
/// 初始化
/// </summary>
private void initializeScreen()
{
textBox1.Text = "";
radioButton1.Checked = true;
listView1.Items.Clear();
comboBox1.Items.Clear();
ListViewItem item = null;
int colIndex=0;
tableTemp = GetData();
//將所得到的表的列名,賦值給單元格
foreach(DataColumn col in tableTemp.Columns)
{
colIndex++;
comboBox1.Items.Add(col.ColumnName);
}
//同樣方法處理數(shù)據(jù)
foreach(DataRow row in tableTemp.Rows)
{
colIndex=0;
item = new ListViewItem(row[0].ToString());
for(colIndex= 1;colIndex<tableTemp.Columns.Count;colIndex++)
{
item.SubItems.Add(row[colIndex].ToString());
}
listView1.Items.Add(item);
}
}
/// <summary>
/// ListView雙擊事件——修改圖書記錄
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void listView1_DoubleClick(object sender, System.EventArgs e)
{
if(listView1.SelectedItems[0].SubItems[0].Text!="")
{
Library.BuyBooks dialog = new BuyBooks(true,listView1.SelectedItems[0].SubItems[0].Text);
dialog.ShowDialog();
initializeScreen();
}
}
/// <summary>
/// 篩選
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSearch_Click(object sender, System.EventArgs e)
{
string str = "";
if(comboBox1.Text != "")
{
if(radioButton1.Checked)
{
str = "select * from V_圖書資料"+ " where " + comboBox1.Text + "='" + textBox1.Text + "'";
ListViewItem item = null;
listView1.Items.Clear();
int colIndex=0;
tableTemp = GetData2(str);
//同樣方法處理數(shù)據(jù)
foreach(DataRow row in tableTemp.Rows)
{
colIndex=0;
item = new ListViewItem(row[0].ToString());
for(colIndex= 1;colIndex<tableTemp.Columns.Count;colIndex++)
{
item.SubItems.Add(row[colIndex].ToString());
}
listView1.Items.Add(item);
}
}
else
{
str = "select * from V_圖書資料" + " where " + comboBox1.Text + " like '%" + textBox1.Text + "%'";
ListViewItem item = null;
listView1.Items.Clear();
int colIndex=0;
tableTemp = GetData2(str);
//同樣方法處理數(shù)據(jù)
foreach(DataRow row in tableTemp.Rows)
{
colIndex=0;
item = new ListViewItem(row[0].ToString());
for(colIndex= 1;colIndex<tableTemp.Columns.Count;colIndex++)
{
item.SubItems.Add(row[colIndex].ToString());
}
listView1.Items.Add(item);
}
}
}
else
{
MessageBox.Show("查詢依據(jù)不清楚!請重新設(shè)定!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
comboBox1.Focus();
}
}
/// <summary>
/// 重置
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnRefresh_Click(object sender, System.EventArgs e)
{
initializeScreen();
}
/// <summary>
/// 新添圖書
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnBuyBooks_Click(object sender, System.EventArgs e)
{
Library.BuyBooks dialog = new BuyBooks(false,"");
dialog.ShowDialog();
initializeScreen();
}
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if(e.KeyValue == 13)
{
string str = "";
if(comboBox1.Text != "")
{
if(radioButton1.Checked)
{
str = "select * from V_圖書資料"+ " where " + comboBox1.Text + "='" + textBox1.Text + "'";
ListViewItem item = null;
listView1.Items.Clear();
int colIndex=0;
tableTemp = GetData2(str);
//同樣方法處理數(shù)據(jù)
foreach(DataRow row in tableTemp.Rows)
{
colIndex=0;
item = new ListViewItem(row[0].ToString());
for(colIndex= 1;colIndex<tableTemp.Columns.Count;colIndex++)
{
item.SubItems.Add(row[colIndex].ToString());
}
listView1.Items.Add(item);
}
}
else
{
str = "select * from V_圖書資料" + " where " + comboBox1.Text + " like '%" + textBox1.Text + "%'";
ListViewItem item = null;
listView1.Items.Clear();
int colIndex=0;
tableTemp = GetData2(str);
//同樣方法處理數(shù)據(jù)
foreach(DataRow row in tableTemp.Rows)
{
colIndex=0;
item = new ListViewItem(row[0].ToString());
for(colIndex= 1;colIndex<tableTemp.Columns.Count;colIndex++)
{
item.SubItems.Add(row[colIndex].ToString());
}
listView1.Items.Add(item);
}
}
}
else
{
MessageBox.Show("查詢依據(jù)不清楚!請重新設(shè)定!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
comboBox1.Focus();
}
}
}
/// <summary>
/// 導(dǎo)出圖書記錄到Excel
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnExcel_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);
//將所得到的表的列名,賦值給單元格
foreach(DataColumn col in tableTemp.Columns)
{
colIndex++;
excel.Cells[1,colIndex]=col.ColumnName;
}
//同樣方法處理數(shù)據(jù)
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("成功導(dǎo)出記錄!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
//不可見,即后臺處理
excel.Visible=true;
}
/// <summary>
/// 修改圖書資料
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnEdit_Click(object sender, System.EventArgs e)
{
if(listView1.SelectedItems[0].SubItems[0].Text!="")
{
Library.BuyBooks dialog = new BuyBooks(true,listView1.SelectedItems[0].SubItems[0].Text);
dialog.ShowDialog();
initializeScreen();
}
}
/// <summary>
/// 刪除圖書記錄
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnDelete_Click(object sender, System.EventArgs e)
{
if(listView1.SelectedItems[0].SubItems[0].Text != "")
{
if(MessageBox.Show("是否確定刪除此記錄!?","詢問",MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)
{
string str = "delete * from 圖書資料 where 條形碼='" + listView1.SelectedItems[0].SubItems[0].Text + "'";
cmd.CommandText = str;
try
{
cmd.ExecuteNonQuery();
MessageBox.Show("成功刪除記錄!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
initializeScreen();
}
catch(Exception ex)
{
MessageBox.Show("發(fā)生如下錯誤:\n\t" + ex.ToString(),"錯誤",MessageBoxButtons.OK,MessageBoxIcon.Error);
return;
}
}
}
}
/// <summary>
/// 單擊列表頭進(jìn)行手工排序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void listView1_ColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e)
{
listView1.ListViewItemSorter = new Library.Class.ListViewItemComparer(e.Column);
listView1.Sort();
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -