📄 newbook.cs
字号:
this.label2.Location = new System.Drawing.Point(80, 144);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(72, 16);
this.label2.TabIndex = 1;
this.label2.Text = "书 名:";
//
// label1
//
this.label1.Location = new System.Drawing.Point(80, 80);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(80, 16);
this.label1.TabIndex = 0;
this.label1.Text = "图书编号:";
//
// button5
//
this.button5.Location = new System.Drawing.Point(200, 312);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(48, 23);
this.button5.TabIndex = 25;
this.button5.Text = "新 增";
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// label12
//
this.label12.Location = new System.Drawing.Point(408, 72);
this.label12.Name = "label12";
this.label12.Size = new System.Drawing.Size(40, 16);
this.label12.TabIndex = 26;
this.label12.Text = "正常";
//
// Newbook
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(568, 374);
this.Controls.Add(this.tabControl1);
this.Name = "Newbook";
this.Text = "新书管理";
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
this.Load += new System.EventHandler(this.Newbook_Load);
this.tabControl1.ResumeLayout(false);
this.tabPage1.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.list)).EndInit();
this.tabPage2.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private void Newbook_Load(object sender, System.EventArgs e)
{
}
private void LoadSelectItem()
{
//载入类别
DataSet ds = Public.GetDireItem( );
if(ds != null)
{
for(int i=0;i<ds.Tables[0].Rows.Count;i++)
{
DataRow row = ds.Tables[0].Rows[i];
this.comboBox1.Items.Add(row["name"].ToString());
}
comboBox1.SelectedIndex=0;
this.textBox8.Text ="0";
}
}
//书籍类别改变
private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
try
{
string sql = "select * from orders where name='"+ comboBox1.SelectedItem.ToString().Trim() +"'";
OleDbDataAdapter ad = new OleDbDataAdapter(sql,Public.conn);
DataSet ds = new DataSet();
ad.Fill(ds,"orders");
if(ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count == 1)
{
DataRow row = ds.Tables[0].Rows[0];
this.textBox8.Text = row["id"].ToString();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message.ToString());
return;
}
}
private void button2_Click(object sender, System.EventArgs e)
{
if (autoID == null)
{
try
{
string sql = "insert into book (code ,name,author,press,outdate,memotxt,cost,lost,ordercode) values('";
sql+= this.bookcode.Text +"','" + this.bookname.Text +"','" + this.bookauthor.Text +"','" +this.bookpress.Text +"','" +this.outdate.Text +"','";
sql+= this.bookmemo.Text +"'," + Int32.Parse(this.bookprice.Text ) +",'正常','" +this.textBox8.Text +"')";
//sql="insert into book (code ,name,author,press,outdate,memo,cost,lost,ordercode) values('0000000011','C#入门','周超','中国人民出版社','2006-12-19','第一章:入门',65,'1','2')";
//sql="insert into book (code ,name,author,press,outdate,cost,lost,ordercode,memotxt) values('0000000011','C#入门','周超','中国人民出版社','2006-12-19',45,'1','2','zc')";
Public.CloseConn();
Public.conn.Open();
OleDbCommand cmd = new OleDbCommand(sql,Public.conn);
cmd.ExecuteNonQuery();
Public.conn.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message.ToString());
return;
}
}
else
{
string sql = "update book set code ='";
sql+= this.bookcode.Text +"',name='" + this.bookname.Text +"',author='" + this.bookauthor.Text +"',press='" +this.bookpress.Text +"',outdate='" +this.outdate.Value.Date +"',memotxt='";
sql+= this.bookmemo.Text +"',cost=" + Int32.Parse(this.bookprice.Text ) +",ordercode='" +this.textBox8.Text +"' where id=" +Int32.Parse(autoID) ;
Public.CloseConn();
Public.conn.Open();
OleDbCommand cmd = new OleDbCommand(sql,Public.conn);
cmd.ExecuteNonQuery();
Public.conn.Close();
}
}
public void LoadListForList()
{
try
{string sqltem=" and ";
if (this.codeStr !=null || this.nameStr !=null || this.authorStr!=null || this.pressStr!=null || this.outdateStr !=null || this.costStr !=null || this.ordernameStr!=null || this.memotxtStr !=null )
{
if (this.codeStr !=null) sqltem=sqltem + this.codeStr;
if (this.nameStr !=null)
{
if (sqltem==" and ")
{ sqltem=sqltem + this.nameStr;}
else {sqltem=sqltem +" and "+ this.nameStr;}
}
if (this.authorStr !=null)
{
if (sqltem==" and ")
{ sqltem=sqltem + this.authorStr;}
else {sqltem=sqltem +" and "+ this.authorStr;}
}
if (this.pressStr !=null)
{
if (sqltem==" and ")
{ sqltem=sqltem + this.pressStr;}
else {sqltem=sqltem +" and "+ this.pressStr;}
}
if (this.outdateStr !=null)
{
if (sqltem==" and ")
{ sqltem=sqltem + this.outdateStr;}
else {sqltem=sqltem +" and "+ this.outdateStr;}
}
if (this.costStr !=null)
{
if (sqltem==" and ")
{ sqltem=sqltem + this.codeStr;}
else {sqltem=sqltem +" and "+ this.costStr;}
}
if (this.ordernameStr !=null)
{
if (sqltem==" and ")
{ sqltem=sqltem + this.ordernameStr;}
else {sqltem=sqltem +" and "+ this.ordernameStr;}
}
if (this.memotxtStr !=null)
{
if (sqltem==" and ")
{ sqltem=sqltem + this.memotxtStr;}
else {sqltem=sqltem +" and "+ this.memotxtStr;}
}
}
else { sqltem="";}
if(this.list.DataSource != null) ((DataTable)this.list.DataSource).Rows.Clear();
string sql = "select a.id as 自动编号,a.code as 编号,a.name as 名称,a.author as 作者,a.press as 出版社,a.outdate as 出版日期,a.memotxt as 内容简介,a.cost as 价格,a.lost as 状态,b.name as 图书类别 from book a,orders b where a.ordercode=b.id " +sqltem +" order by a.code ";
this.ds = new DataSet();
OleDbDataAdapter ad = new OleDbDataAdapter(sql,Public.conn);
ad.Fill(this.ds,"list");
this.list.SetDataBinding(ds.Tables[0],"");
}
catch(Exception ex)
{
MessageBox.Show(ex.Message.ToString());
return;
}
}
private void button3_Click(object sender, System.EventArgs e)
{
if(this.autoID != null && MessageBox.Show("删除后不可恢复,确定删除吗?","删除确认",MessageBoxButtons.OKCancel,MessageBoxIcon.Question) == DialogResult.OK)
{
try
{
string sql = "delete from book where id="+ autoID ;
Public.ExeSql(sql);
MessageBox.Show("操作成功,请刷新列表查看!");
this.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message.ToString());
return;
}
}
}
private void button1_Click(object sender, System.EventArgs e)
{
this.Close();
}
private void list_DoubleClick(object sender, System.EventArgs e)
{
if(this.autoID != null)
{
this.tabControl1.SelectedIndex =1;
LoadInfo();
}
}
/// <summary>
/// 事件一
/// </summary>
private void ClickInDG(object sender,MouseEventArgs e)
{
try
{
if(this.currentIndex != -1) this.list.UnSelect(this.currentIndex);
Point p = new Point(e.X,e.Y);
DataGrid.HitTestInfo hti = this.list.HitTest(p);
if(hti.Type == DataGrid.HitTestType.Cell)
{
this.list.CurrentCell = new DataGridCell(hti.Row,hti.Column);
this.list.Select(hti.Row);
this.currentIndex = hti.Row;
if(this.list.CurrentCell.RowNumber < this.ds.Tables[0].Rows.Count + 1)
{
string xuhao = this.list[this.list.CurrentCell.RowNumber,0].ToString();
if(xuhao != null && xuhao != "" && xuhao != "null")
{
try
{
this.autoID = xuhao;
}
catch
{
this.autoID = null;
}
}
}
else
{
if(this.currentIndex != -1)
{
this.list.UnSelect(this.currentIndex);
this.currentIndex = -1;
}
this.autoID = null;
}
}
else this.autoID = null;
//如果相等,则表示选中的是空行
if(this.currentIndex == ((DataTable)this.list.DataSource).Rows.Count)
{
this.autoID = null;
this.currentIndex = -1;
}
}
catch{}
}
/// <summary>
/// 事件二
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void UPInDG(object sender,MouseEventArgs e)
{
// if(this.currentIndex > -1)
// {
// this.menuItem1.Enabled = true;
// this.menuItem2.Enabled = true;
// }
// else
// {
// this.menuItem1.Enabled = false;
// this.menuItem2.Enabled = false;
// }
}
private void InitDataGrid()
{
DataGridTextBoxColumn dgtb = (DataGridTextBoxColumn)this.list.TableStyles[0].GridColumnStyles[0];
dgtb.Width = 0;
dgtb = (DataGridTextBoxColumn)this.list.TableStyles[0].GridColumnStyles[1];
dgtb.Width = 80;
dgtb = (DataGridTextBoxColumn)this.list.TableStyles[0].GridColumnStyles[2];
dgtb.Width = 120;
dgtb = (DataGridTextBoxColumn)this.list.TableStyles[0].GridColumnStyles[3];
dgtb.Width = 70;
dgtb = (DataGridTextBoxColumn)this.list.TableStyles[0].GridColumnStyles[4];
dgtb.Width = 120;
dgtb = (DataGridTextBoxColumn)this.list.TableStyles[0].GridColumnStyles[5];
dgtb.Width = 80;
dgtb = (DataGridTextBoxColumn)this.list.TableStyles[0].GridColumnStyles[6];
dgtb.Width = 130;
dgtb = (DataGridTextBoxColumn)this.list.TableStyles[0].GridColumnStyles[7];
dgtb.Width = 60;
dgtb = (DataGridTextBoxColumn)this.list.TableStyles[0].GridColumnStyles[8];
dgtb.Width = 40;
dgtb = (DataGridTextBoxColumn)this.list.TableStyles[0].GridColumnStyles[9];
dgtb.Width = 60;
}
//载入选中的信息
private void LoadInfo()
{
if(this.autoID != null)
{
try
{
string sql = "select * from book where id = " + this.autoID;
OleDbDataAdapter ad = new OleDbDataAdapter(sql,Public.conn);
DataSet ds = new DataSet();
ad.Fill(ds,"list");
if(ds != null && ds.Tables[0].Rows.Count > 0)
{
DataRow row = ds.Tables[0].Rows[0];
this.bookcode.Text = row["code"].ToString();
this.bookname.Text = row["name"].ToString();
this.bookauthor.Text = row["author"].ToString();
this.bookpress.Text = row["press"].ToString() ;
this.outdate.Text = Public.GetChineseDate(Convert.ToDateTime(row["outdate"].ToString().Replace(" 0:00:00","")));
this.bookprice.Text = row["cost"].ToString() ;
this.bookmemo.Text = row["memotxt"].ToString();
this.label12.Text = row["lost"].ToString();
//灰色化保存按钮
sql = "select * from orders where id="+ Convert.ToInt32(row["ordercode"].ToString()) ;
ad = new OleDbDataAdapter(sql,Public.conn);
DataSet _ds = new DataSet();
ad.Fill(_ds,"orders");
if( _ds.Tables[0].Rows.Count > 0)
{
this.comboBox1.Text = _ds.Tables[0].Rows[0]["name"].ToString() ;
this.textBox8.Text = _ds.Tables[0].Rows[0]["id"].ToString() ;
}
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message.ToString());
this.Close();
}
}
}
private void tabControl1_SelectedIndexChanged(object sender, System.EventArgs e)
{
if (this.tabControl1.SelectedIndex ==1) LoadInfo();
else{LoadListForList();}
}
private void button5_Click(object sender, System.EventArgs e)
{
this.autoID=null;
this.bookcode.Text="";
this.bookmemo.Text="";
this.bookname.Text="";
this.bookpress.Text="";
this.bookauthor.Text ="";
this.bookprice.Text="0";
this.textBox8.Text="1";
this.label12.Text ="正常";
this.comboBox1.SelectedIndex=0;
this.outdate.Value=System.DateTime.Now ;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -