C#笔记二
#关于cbo下拉框的相关操作
数据来源是数据库中的数据(包含实际值与显示值)
string sql = "select officeid,officename from office";//数据库语句
DataTable dt = DBHelper.GetDataTable(sql);// 连接数据库并返回临时表
DataRow dataRow = dt.NewRow();//将已有数据的dt,初始化一个行数据
//添加 请选择、全部 这类的初始值
dataRow["officeid"] = 0;//添加Iid=0
dataRow["officename"] = "请选择";//显示的值
dt.Rows.InsertAt(dataRow, 0);//添加到表中
this.cboOfficeList.ValueMember = "officeid";//隐藏值
this.cboOfficeList.DisplayMember = "officename";//显示值
this.cboOfficeList.DataSource = dt;//表中数据给cbo下拉框
实现后如下
关于C#密码转换为MD5加密
32位加密
数据库中语句为:
substring(sys.fn_sqlvarbasetostr(HashBytes("MD5",字段名)),3,32)
使用更新语句:
update 表名 set 字段名 = substring(sys.fn_sqlvarbasetostr(HashBytes("MD5",字段名)),3,32)
C#中语句为:
private string GetMD5(string pwd)
{
string password = "";
MD5 md5 = MD5.Create();
byte[] vs = md5.ComputeHash(Encoding.UTF8.GetBytes(pwd));
for(int i = 0;i<vs.Length;i++)
{
password += vs[i].Tostring("x2");//这里x2表示转化的等效的字符串形式x为小写,加密为小写,大写则为大写;这里需要注意后面更上2,目的是字符转化成字符串时数字的位数是2位宽度,不添加会丢失位数。
}
return password;
}
#关于C#dgv列表的重绘——根据不同条件,改变背景颜色
双击dgv的cellpainting事件
private void dgvOfficeList_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if(e.RowIndex < 0 )
{
returen;
}
DataGridViewRow row = dgvList.Rows[e.RowIndex];
if(row.Cells["列名"].Value.Tostring = "条件")
{
e.Cellstyle.Backcolor = Color.red;
}
}