C#项目遇到的事儿

来源:互联网 发布:网络视听内容审核通则 编辑:程序博客网 时间:2024/05/21 14:00

1.dataGridView
设定RowHeadersVisible False,可去掉系统添加的第一列。
编辑列->AutoSizaMode->Fill,可使列填充正行。

2.Form1右键菜单链接Form2
在Form1添加contextMenuStrip,且属性设置contextMenuStrip选中添加的contextMenuStrip1。
Form1代码中加入:
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
//MessageBox.Show(“Right”);
contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);
}
}
private void Form2ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 dAdd = new Form2(this);
dAdd.Show();
this.Hide();
}
Form2代码中加入:
public Form2(Form1 f1)
{
InitializeComponent();
}
就可以了。
要想从Form2返回Form1怎么办呢?在Form2添加一个返回按钮,为其添加代码如下:
private void btnClose_Click(object sender, EventArgs e)
{
Application.Restart();
}
重启系统。也许有更好的办法?

3.实现主窗口隐藏子窗口开启,子窗口关闭重新打开主窗口:
//主窗体
private void 部门编辑ToolStripMenuItem_Click(object sender, EventArgs e)//右键菜单部门编辑
{
部门编辑 dAdd = new 部门编辑(this);
dAdd.Owner = this;
dAdd.Show();
this.Hide();
}
//子窗体
private void button4_Click(object sender, EventArgs e)//返回键
{
this.Close();
//this.Owner.Show();
}
private void 部门编辑_FormClosing(object sender, FormClosingEventArgs e)//右上关闭键
{
this.Owner.Show();
}

4.更改dataFridView的列名,使其与数据库中不同:
在dgv.DataSource = dt; // 设置到DataGridView中
后加入
dgv.Columns[0].HeaderCell.Value = “部门编号”;//该显示列名
dgv.Columns[1].HeaderCell.Value = “部门名称”;
或者在查询语句中重命名列名
String sql = “select 部门编号=deptNum,部门名称=deptName from dept”; // 查询语句

5.从dateTimePicker1输入时间到SQLServer应该采取的姿势:
string time1 = dateTimePicker1.Value.ToString(“yyyy/MM/dd HH:mm:ss”);
string time2 = dateTimePicker2.Value.ToString(“yyyy/MM/dd HH:mm:ss”);
String sql = “select * from (select A.pNum,A.pName,A.pDept from person A where @dept=A.pDept and @name=A.pName) C inner join (select * from doorLog B where B.inTime between @time1 and @time2) D on C.pNum=D.pNum”; // 查询语句

6.从数据库加载一列到comboBox:
private void 进山查询_Load(object sender, EventArgs e)//打开窗口后加载部门列表
{
using (SqlConnection myConn = new SqlConnection(connsql))//加载部门列表
{
String sql = “select deptName from dept”;
SqlDataAdapter myDa = new SqlDataAdapter(sql, myConn);
DataTable dt = new DataTable(); // 实例化数据表
myDa.Fill(dt);
comboBox1.DataSource = dt;
comboBox1.DisplayMember = “deptName”;
}
comboBox1.SelectedIndex = -1;//将初始显示设置为空
}

7.comboBox1选择完后,comboBox2根据comboBox1的Text,如果1中没2则将2清空:
用SelectedIndexChanged,
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)//选择部门后填充姓名下拉框
{
using (SqlConnection myConn = new SqlConnection(connsql))//加载部门列表
{
string pd = comboBox1.Text;
String sql = “select pName from person where pDept=@pd”;
SqlDataAdapter myDa = new SqlDataAdapter(sql, myConn);
SqlParameter parn1 = new SqlParameter(“@pd”, pd);
myDa.SelectCommand.Parameters.Add(parn1);
DataSet myDs = new DataSet();
myDa.Fill(myDs, “table”);
if (myDa.Fill(myDs, “table”) > 0)//如果box1没box2的内容,需要将之前box2的内容清空
{
comboBox2.DataSource = myDs.Tables[0];
comboBox2.DisplayMember = “pName”;
}
else { comboBox2.DataSource = null; }
}
}

8.backcolor设置成Transparent可以融合在背景色当中。web下第一个。

9.dataGridView选中的行显示在textBox当中:
private void dgv_CellClick(object sender, DataGridViewCellEventArgs e)
{
textBox1.Text =dgv.Rows[dgv.CurrentRow.Index ].Cells[0].Value.ToString() ;
textBox2.Text = dgv.Rows[dgv.CurrentRow.Index].Cells[1].Value.ToString();
}

10.关于生成安装文件的,以后不知道会不会用到:
http://www.mamicode.com/info-detail-2453.html

11.窗体传值
www.cnblogs.com/xcong/p/3386085.html

12.自定义窗口最小化最大化关闭按钮
http://www.cnblogs.com/bison1989/archive/2012/04/18/2455068.html
http://bbs.csdn.net/topics/390751391?page=1

0 0