📄 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(),返回一个数据表
/// </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(),返回一个数据表
/// </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>
/// 关闭
/// </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);
}
//同样方法处理数据
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);
//同样方法处理数据
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);
//同样方法处理数据
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("查询依据不清楚!请重新设定!","提示",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);
//同样方法处理数据
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);
//同样方法处理数据
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("查询依据不清楚!请重新设定!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
comboBox1.Focus();
}
}
}
/// <summary>
/// 导出图书记录到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;
}
//同样方法处理数据
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;
}
/// <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("发生如下错误:\n\t" + ex.ToString(),"错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
return;
}
}
}
}
/// <summary>
/// 单击列表头进行手工排序
/// </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();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -