打印的原理是:生成mdi文件,系统碰到mdi的时候会自动以打印的方式处理。所以,不管用什么模板,什么方式;能在PrintPage事件处理中,生成一张要打印内容的图片就OK了!
C#实现打印源码如下:
#region 打印
private void btnPrint_Click(object sender, EventArgs e)
{
//打印预览
//PrintPreviewDialog ppd = new PrintPreviewDialog();
PrintDocument pd = new PrintDocument();
//设置边距
Margins margin = new Margins(20, 20, 20, 20);
pd.DefaultPageSettings.Margins = margin;
////纸张设置默认
//PaperSize pageSize = new PaperSize("First custom size", 800, 600);
//pd.DefaultPageSettings.PaperSize = pageSize;
//打印事件设置
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
//ppd.Document = pd;
//ppd.ShowDialog();
try
{
pd.Print();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
pd.PrintController.OnEndPrint(pd, new PrintEventArgs());
}
}
//打印事件处理
private void pd_PrintPage(object sender, PrintPageEventArgs e)
{
string date = lblDate.Text; //当前日期
string flowId = lblFlowId.Text; //流水号
string payDate = PayDate.Year.ToString() + "年" + PayDate.Month.ToString() + "月"; //应收年月
string adminId = lblAdminId.Text; //操作员编号
string baseExpense = lblBaseExpense.Text; //应交基本费用
string fine = lblFine.Text; //罚款数目
string upExpense = lblUpExpense.Text; //上月上余
string actualExpense = txtActualExpense.Text; //实际应交费用
string chineseExpense = DecimalToChinese.ConvertSum(actualExpense); //实际应交费用的中文大写
//读取图片模板
Image temp = Image.FromFile(@"Receipts.jpg");
GetResultIntoImage(ref temp, UserId, flowId, date, baseExpense, fine, upExpense, actualExpense, chineseExpense, payDate, adminId);
int x = e.MarginBounds.X;
int y = e.MarginBounds.Y;
int width = temp.Width;
int height = temp.Height;
Rectangle destRect = new Rectangle(x, y, width, height);
e.Graphics.DrawImage(temp, destRect, 0, 0, temp.Width, temp.Height, System.Drawing.GraphicsUnit.Pixel);
}
/// <summary>
/// 将收费结果填充到图片模板
/// </summary>
private void GetResultIntoImage(
ref Image temp,
string userId,
string flowId,
string currentDate,
string baseExpense,
string fine,
string upExpense,
string actualExpense,
string chineseExpense,
string payDate,
string adminName)
{
//读取图片模板
Graphics g = Graphics.FromImage(temp);
Font f = new Font("宋体", 12);
Brush b = new SolidBrush(Color.Black);
//填充数据到图片模板(位置要在制作图片模板的时候度量好)
g.DrawImage(temp, 0, 0, temp.Width, temp.Height);
g.DrawString(userId, f, b, 168, 105);
g.DrawString(UserName, f, b, 166, 134);
g.DrawString(flowId, f, b, 535, 105);
g.DrawString(currentDate, f, b, 535, 134);
g.DrawString(baseExpense, f, b, 219, 202);
g.DrawString(fine, f, b, 372, 202);
g.DrawString(upExpense, f, b, 486, 202);
g.DrawString(actualExpense, f, b, 596, 202);
g.DrawString(chineseExpense, f, b, 196, 238);
g.DrawString(payDate, f, b, 176, 269);
g.DrawString(adminName, f, b, 497, 298);
g.Dispose();
}
#endregion
用string.Replace方法将 ' 单引号替换为 '' 双引号,可以避免用户在输入 ' 单引号的时候提交到数据库里失败的问题。
例如: temp_str = dgvOS.CurrentCell.Value.ToString();
DataRow[] eid = dt_ed.Select("版本='" + temp_str .Replace ("'","''")+ "'");
DataAdapter的名字为adapter,DataSet命名为set
SqlCommandBuilder builder = new SqlCommandBuilder (adapter)
adapter.Update(set,"表名")
DataGridView里更新数据实现与数据库同步。
第一个错误 Main.cs 854行 第一次打开,双击了根节点,展开后,再双击根节点和子节点的连接线(不点在子节点上),导致以下两个条件都满足,但是此时 tvHardwareList.SelectedNode的值还是根节点上的值;
if (level > 0 && tvHardwareList.SelectedNode != null)
{
...
...
}
在 datagridview上点击右键添加系统,如果只有COMBOBOXOS,则右键菜单显示添加系统版本,否则右键菜单显示添加版本号
-------------------------用正则表达式校验IP地址的格式-------------------引用using System.Text.RegularExpressions;
-------------------------用到获取系统当期时间。-------------------------
System.DateTime currentTime=new System.DateTime();
currentTime=System.DateTime.Now;
string s_reg = @"^([1-9]|[1-9]/d|(1/d{2}|2[0-5][0-5]))/.([0-9]|[1-9]/d|(1/d{2}|2[0-5][0-5]))/.([0-9]|[1-9]/d|(1/d{2}|2[0-5][0-5]))/.([0-9]|[1-9]/d|(1/d{2}|2[0-5][0-5]))$";
string s = cbLanIP.Text;
Regex reg = new Regex(s_reg);
if (reg.IsMatch(s))
{ }
else
{
MessageBox.Show("IP地址格式错误!","提示!");
}
SqlConnection conn=new SqlConnection ("Server=.;UID=sa;PWD=123456;Database=Server");
conn.Open ();
string cmdstr="insert into ipinfo values('"+DbHelperSQL.ID +"','"+s +"','"+Convert .ToDateTime( currentTime.ToString ("f"))+"','1','true','1')";
SqlCommand cmd = new SqlCommand(cmdstr, conn );
cmd.ExecuteNonQuery();
conn.Close();
MessageBox.Show("添加IP成功!");
----------------------------------------------------------------------------
-------------------------执行SQL语句,无返回值。-------------------------
SqlConnection conn=new SqlConnection ("Server=.;UID=sa;PWD=123456;Database=Server");
conn.Open ();
string cmdstr="insert into ipinfo values('"+DbHelperSQL.ID +"','"+s +"','"+Convert .ToDateTime( currentTime.ToString ("f"))+"','1','true','1')";
SqlCommand cmd = new SqlCommand(cmdstr, conn );
cmd.ExecuteNonQuery();
conn.Close();
-------------------------------------------------------------------------
-------------------------执行SQL语句,有返回值,存到一个DataSet的表里。--------------------
string connStr = "Server=localhost;UID=sa;PWD=123456;Database=Server";
DataSet maxds = new DataSet();
SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter("select ID,g_id from serverinfo where s_no='" + DbHelperSQL.S_No + "'", connStr);
sqlDataAdapter1.Fill(maxds);
int ID = Convert.ToInt32(maxds.Tables[0].Rows[0][0].ToString());
int G_ID = Convert.ToInt32(maxds.Tables[0].Rows[0][1].ToString());
-------------------------------------------------------------------------
---------------------------创建存储过程的例子----------------------------------------------
create Procedure [dbo].[Proc_IPInfo_Insert]
@ID int,
@I_IP nvarchar(50),
@I_ActiveTime datetime,
@S_ID int,
@I_Active bit,
@I_Type int
AS
INSERT INTO IPInfo (
ID,
I_IP,
I_ActiveTime,
S_ID,
I_Active,
I_Type
) VALUES(
@ID ,
@I_IP ,
@I_ActiveTime,
@S_ID ,
@I_Active ,
@I_Type
)
--------------------------------------------------------------------------------------------
create proc [dbo].[select_si_sg_by_GroupName]
@GroupName nvarchar(50)
as
select a.id,a.s_no,b.id from serverinfo a,servergroup b where a.g_id=b.id and b.groupname=@GroupName
null 空(连个对象都没有)
empty 空(空对象)
'' 空(空字符)
DataGridView 中右键单击一个cell使它为选中状态
private void dgvOS_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
//DataGridView currentcell =dgvOS.CurrentCell.get(ClickPoint);
if (e.Button == MouseButtons.Right)
{
dgvOS .CurrentCell =dgvOS[e.ColumnIndex, e.RowIndex];
dgvOS.ContextMenuStrip = cmsOS;
}
}