📄 goodslistform.cs
字号:
this.groupBox2.Enter += new System.EventHandler(this.groupBox2_Enter);
//
// m_GoodListdataGrid
//
this.m_GoodListdataGrid.CaptionVisible = false;
this.m_GoodListdataGrid.DataMember = "";
this.m_GoodListdataGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.m_GoodListdataGrid.Location = new System.Drawing.Point(16, 24);
this.m_GoodListdataGrid.Name = "m_GoodListdataGrid";
this.m_GoodListdataGrid.Size = new System.Drawing.Size(704, 320);
this.m_GoodListdataGrid.TabIndex = 0;
this.m_GoodListdataGrid.Navigate += new System.Windows.Forms.NavigateEventHandler(this.dataGrid1_Navigate);
this.m_GoodListdataGrid.CurrentCellChanged += new System.EventHandler(this.m_GoodListdataGrid_CurrentCellChanged);
//
// button2
//
this.button2.Location = new System.Drawing.Point(584, 88);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(64, 24);
this.button2.TabIndex = 17;
this.button2.Text = "删除";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Goodlist
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.AutoScroll = true;
this.ClientSize = new System.Drawing.Size(752, 518);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Name = "Goodlist";
this.Text = "Goodlist";
this.Load += new System.EventHandler(this.Goodlist_Load);
this.TextChanged += new System.EventHandler(this.goodview_Enter);
this.Enter += new System.EventHandler(this.goodview_Enter);
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.m_GoodListdataGrid)).EndInit();
this.ResumeLayout(false);
}
#endregion
private void goodview_Enter(object sender, System.EventArgs e)
{
}
private void 浏览_Enter(object sender, System.EventArgs e)
{
}
private void groupBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
}
private void label1_Click(object sender, System.EventArgs e)
{
}
private void dataGrid1_Navigate(object sender, System.Windows.Forms.NavigateEventArgs ne)
{
}
private void textBox1_TextChanged(object sender, System.EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
}
private void button1_Click(object sender, System.EventArgs e)
{
SqlCommand m_sqjUpdateCommand=new SqlCommand();
m_sqlDataAdapter.UpdateCommand=m_sqjUpdateCommand;
m_sqjUpdateCommand.Connection=m_connection;
m_sqjUpdateCommand.CommandText="update Goods set GoodsName=@name,SalePrice=@saleprice,PurchasePrice=@purchaseprice,StockNumber=@stocknumber,Unit=@unit,Remark=@remark where GoodsCode=@code";
m_sqjUpdateCommand.Parameters.Add(new SqlParameter("@name", SqlDbType.VarChar, 50));
m_sqjUpdateCommand.Parameters["@name"].Value=m_GoodsName.Text.Trim();
m_sqjUpdateCommand.Parameters.Add(new SqlParameter("@saleprice", SqlDbType.SmallMoney, 4));
m_sqjUpdateCommand.Parameters["@saleprice"].Value=m_SalePrice.Text.Trim();
m_sqjUpdateCommand.Parameters.Add(new SqlParameter("@purchaseprice", SqlDbType.SmallMoney, 200));
m_sqjUpdateCommand.Parameters["@purchaseprice"].Value=m_PurchasePrice.Text.Trim();
m_sqjUpdateCommand.Parameters.Add(new SqlParameter("@stocknumber", SqlDbType.Int, 200));
m_sqjUpdateCommand.Parameters["@stocknumber"].Value=m_StockNumber.Text.Trim();
m_sqjUpdateCommand.Parameters.Add(new SqlParameter("@unit", SqlDbType.VarChar, 200));
m_sqjUpdateCommand.Parameters["@unit"].Value=m_Unit.Text.Trim();
m_sqjUpdateCommand.Parameters.Add(new SqlParameter("@remark", SqlDbType.VarChar, 200));
m_sqjUpdateCommand.Parameters["@remark"].Value=m_Remark.Text.Trim();
m_sqjUpdateCommand.Parameters.Add(new SqlParameter("@code", SqlDbType.VarChar, 200));
m_sqjUpdateCommand.Parameters["@code"].Value=m_GoodsCode.Text.Trim();
try
{
m_sqjUpdateCommand.Connection.Open();
m_sqjUpdateCommand.ExecuteNonQuery();
m_sqjUpdateCommand.Connection.Close();
reflesh();
MessageBox.Show(this,"已经成功修改该商品的信息!","正确");
}
catch (SqlException e1)
{
MessageBox.Show(this,e1.Message,"错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
m_connection.Close();
}
}
private void button2_Click(object sender, System.EventArgs e)
{
if(m_GoodListdataGrid.CurrentRowIndex >= 0)
{
string goodsid = datatable.Rows[m_GoodListdataGrid.CurrentRowIndex][1].ToString();
if(DialogResult.Yes==MessageBox.Show(this, "确定删除该商品", "警告", MessageBoxButtons.YesNo))
{
try
{
String sql ="delete from Goods where GoodsCode='"+ goodsid +"'";
m_connection = DBUtil.GetConnection();
m_connection.Open();
SqlCommand cmd = new SqlCommand(sql, m_connection);
int res = cmd.ExecuteNonQuery();
m_connection.Close();
reflesh();
}
catch(SqlException e1)
{
MessageBox.Show(this,"被删除商品已被关联,请先删除关联记录","错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
m_connection.Close();
}
}
}
}
private void button7_Click(object sender, System.EventArgs e)
{
SqlCommand m_sqlInsertCommand=new SqlCommand();
m_sqlDataAdapter.InsertCommand=m_sqlInsertCommand;
m_sqlInsertCommand.Connection=m_connection;
m_sqlInsertCommand.CommandText="insert into GoodsList (Goodscode,GoodsName,PurchasePrice,SalePrice,StockNumber,Unit,Remark) values (@code,@goodsname,@purchaseprice,@saleprice,@stocknumber,@unit,@remark)";
m_sqlInsertCommand.Parameters.Add(new SqlParameter("@code", SqlDbType.VarChar, 50));
m_sqlInsertCommand.Parameters["@code"].Value=m_GoodsCode.Text.Trim();
m_sqlInsertCommand.Parameters.Add(new SqlParameter("@goodsname", SqlDbType.VarChar, 50));
m_sqlInsertCommand.Parameters["@goodsname"].Value=m_GoodsName.Text.Trim();
m_sqlInsertCommand.Parameters.Add(new SqlParameter("@purchaseprice", SqlDbType.VarChar, 200));
m_sqlInsertCommand.Parameters["@purchaseprice"].Value=m_PurchasePrice.Text.Trim();
m_sqlInsertCommand.Parameters.Add(new SqlParameter("@saleprice", SqlDbType.VarChar, 200));
m_sqlInsertCommand.Parameters["@saleprice"].Value=m_SalePrice.Text.Trim();
m_sqlInsertCommand.Parameters.Add(new SqlParameter("@stocknumber", SqlDbType.VarChar, 200));
m_sqlInsertCommand.Parameters["@remark"].Value=m_StockNumber.Text.Trim();
m_sqlInsertCommand.Parameters.Add(new SqlParameter("@unit", SqlDbType.VarChar, 200));
m_sqlInsertCommand.Parameters["@remark"].Value=m_Unit.Text.Trim();
m_sqlInsertCommand.Parameters.Add(new SqlParameter("@remark", SqlDbType.VarChar, 200));
m_sqlInsertCommand.Parameters["@remark"].Value=m_Remark.Text.Trim();
try
{
m_sqlInsertCommand.Connection.Open();
m_sqlInsertCommand.ExecuteNonQuery();
m_sqlInsertCommand.Connection.Close();
MessageBox.Show(this,"已经成功插入一条商品信息!","正确");
}
catch (SqlException e1)
{
MessageBox.Show(this,"被删除商品已被关联,请先删除关联记录","错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void Goodlist_Load(object sender, System.EventArgs e)
{
reflesh();
}
private void groupBox2_Enter(object sender, System.EventArgs e)
{
}
private void m_GoodListdataGrid_CurrentCellChanged(object sender, System.EventArgs e)
{
int idx = m_GoodListdataGrid.CurrentRowIndex;
if(idx != pre_idx)
{
this.m_GoodsCode.Text = m_dataview.Table.Rows[idx][1].ToString();
this.m_GoodsName.Text = m_dataview.Table.Rows[idx][2].ToString();
this.m_PurchasePrice.Text = m_dataview.Table.Rows[idx][3].ToString();
this.m_SalePrice.Text = m_dataview.Table.Rows[idx][4].ToString();
this.m_StockNumber.Text = m_dataview.Table.Rows[idx][5].ToString();
this.m_Unit.Text = m_dataview.Table.Rows[idx][6].ToString();
this.m_Remark.Text = m_dataview.Table.Rows[idx][7].ToString();
pre_idx = idx;
}
}
private void reflesh()
{
try
{
String sql ="select * from Goods order by GoodsID desc";
System.Data.DataRow datarow;
m_connection = DBUtil.GetConnection();
m_connection.Open();
SqlCommand cmd = new SqlCommand(sql, m_connection);
SqlDataReader reader = cmd.ExecuteReader();
if (datatable.Rows.Count>0)
{
datatable.Rows.Clear();
}
while (reader.Read())
{
datarow = datatable.NewRow();
datarow[0] = reader["GoodsID"].ToString();
datarow[1] = reader["GoodsCode"].ToString();
datarow[2] = reader["GoodsName"].ToString();
datarow[3] = reader["purchaseprice"].ToString();
datarow[4] = reader["saleprice"].ToString();
datarow[5] = reader["stocknumber"].ToString();
datarow[6] = reader["unit"].ToString();
datarow[7] = reader["remark"].ToString();
datatable.Rows.Add(datarow);
}
// System.Data.DataView m_dataview = new System.Data.DataView(datatable);
m_dataview.AllowNew = false;
m_dataview.AllowEdit = false;
m_dataview.AllowDelete = false;
m_GoodListdataGrid.DataSource = m_dataview;
m_connection.Close();
if(datatable.Rows.Count> 0)
{
this.m_GoodsCode.Text = datatable.Rows[0][1].ToString();
this.m_GoodsName.Text = datatable.Rows[0][2].ToString();
this.m_PurchasePrice.Text = datatable.Rows[0][3].ToString();
this.m_SalePrice.Text = datatable.Rows[0][4].ToString();
this.m_StockNumber.Text = datatable.Rows[0][5].ToString();
this.m_Unit.Text = datatable.Rows[0][6].ToString();
this.m_Remark.Text = datatable.Rows[0][7].ToString();
pre_idx = 0;
}
}
catch(SqlException ex)
{
string s = ex.ToString();
m_connection.Close();
MessageBox.Show(s);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -