2018年6月C#winform项目开发总结

  1. dataGridView中如果多出一行 则将AllowUserToAddRows 属性设为false;
  2. 用图片做为背景图会出现抖动现象,将窗体的doubleBuffered  双缓存处理设为true。
  3. 控件的 Dock属性:获取或设置哪些边框停靠父容器并随父容器变大变小。
  4. SendToBack:将控件发送到Z顺序的后面。
  5. BringToFront:将控件发送到Z顺序的前面。
  6. Dispose:放在最后用于释放所有资源。
  7. dataGridView控件中获取或设置某一行,某一列的数据dataGridView1.Rows[i].Cells[8].Value 的值。
  8. 设置dataGridView控件中网格的列为自动宽度:

  For(int i=0;i

  dataGridView1.Columns[i].AutoSizeMode=DataGridViewAutoSizeColumnMode.AllCells;//自适应列宽

  dataGridView1.Columns[0].HeaderText="列名", 设置表的列名

   }

连接数据库

1,连接数据库字符串;

2连接对象 SqlConnection con=new SqlConnection(连接数据库的字符串)

3,打开连接

4,数据库语句

5,创建数据库适配器对象,用于填充dataset 对象

SqlDataAdapter da=new SqlDataAdapter(sql,con); 传入的参试sql语句,和一个数据库连接对象。

6,创建Dataset 对象

DataSet ds=new DataSet()

7填充 da.Fill(ds,"fsdf随便填");

8关闭连接

9为数据组件绑定数据源 dataGridView1.DataSource=ds.Ta

Select 查询的另外一种方式

Con.Open()

String sql="select * from 表名";

SqlCommand comm=new SqlCommand(sql, con);

SqlDataAdapter dr=new SqlDataAdapter();

Dr.SelectCommand=comm;

DataSet ds=new DataSet();

Dr.Fill(ds);

Con.Close();

dataGridView1.DataSource=ds.Tables[0].DefaultView;

更新数据库语句

  string sql=“update 表名  set 字段名=das where 。。。”

更新,插入,删除用SqlCommand 对象,表示:执行一个语句或一个存储过程

SqlCommand cmd=new SqlCommand(sql,con)

Cmd.ExecuteNonQuery();

如果select 查询的只是单个影响的行数,

String sql=“select count(*) from 表名 where id=。。”

SqlCommand com=new SqlCommand(sql,connection);

String line=com.ExecuteScalar().ToString(); 返回;影响的行数。

  1. 获取dataGridView1组件中选中单元格,或一行的数据dataGridView1.SelectedCells[0].Value.ToString();
  2. Convert.ToInt16("123") 强转类型。
  3. 日期控件转化为字符串: dateTimePicker2.Value.ToString();
  4. 确定,取消对话框。

 DialogResult r = MessageBox.Show("确认要删除该执法人员的信息吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);

If(r==DialogResult.Cancel)

Sql 中时间类型的字段可以时间格式相同的字符串的相比较

dataGridView 组件新添加一个checkBox的列

先新建这个列对象

System.Windows.Forms.DataGridViewCheckboxColumn chk;

Chk=new System.Windows.Forms.DataGridViewCheckBoxColumn();

Chk.HeaderText="选择";

chkName=“chk”;

This.dataGridView1Columns.Add(chk);

//判断勾选的代码,并获取勾选之后的选中的单元的数据。

  DataGridViewCheckBoxCell checkCell;

        private void button2_Click(object sender, EventArgs e)

        {

            try

            {

                DataTable dt = new DataTable();

                dt.Columns.AddRange(new DataColumn[] {

 

                new DataColumn("f_vehicleid",typeof(string)),

                new DataColumn("f_axlesum",typeof(string)),

                new DataColumn("f_weightlimited",typeof(string)),

                new DataColumn("f_fctime",typeof(string)),

                new DataColumn("f_checkno",typeof(string)),

                new DataColumn("f_fctotalweight",typeof(string)),

                new DataColumn("f_overlimited",typeof(string)),

                new DataColumn("f_Rctime",typeof(string)),

                new DataColumn("f_rctotalweight",typeof(string)),

                new DataColumn("f_offload",typeof(string)),

                new DataColumn("f_model1",typeof(string)),

                new DataColumn("f_carTel",typeof(string)),

            });

                for (int i = 0; i < dataGridView1.Rows.Count; i++)

                {

                    checkCell = (DataGridViewCheckBoxCell)dataGridView1.Rows[i].Cells["Chk"];

                    if (Convert.ToBoolean(checkCell.Value) == true)

                    {

                        DataRow dr = null;

                        dr = dt.NewRow();

                        //  dr["id"] = i;

                        //将选中的一行数据转换到datatable中

                        //dr["id"] = Convert.ToString(dataGridView1.Rows[i].Cells["id"].Value);

                        dr["f_vehicleid"] = Convert.ToString(dataGridView1.Rows[i].Cells["f_vehicleid"].Value);

                        dr["f_axlesum"] = Convert.ToString(dataGridView1.Rows[i].Cells["f_axlesum"].Value);

                        dr["f_weightlimited"] = Convert.ToString(dataGridView1.Rows[i].Cells["f_weightlimited"].Value);

                        dr["f_fctime"] = Convert.ToString(dataGridView1.Rows[i].Cells["f_fctime"].Value);

                        dr["f_checkno"] = Convert.ToString(dataGridView1.Rows[i].Cells["f_checkno"].Value);

                        dr["f_fctotalweight"] = Convert.ToString(dataGridView1.Rows[i].Cells["f_fctotalweight"].Value);

                        dr["f_overlimited"] = Convert.ToString(dataGridView1.Rows[i].Cells["f_overlimited"].Value);

                        dr["f_Rctime"] = Convert.ToString(dataGridView1.Rows[i].Cells["f_Rctime"].Value);

                        dr["f_rctotalweight"] = Convert.ToString(dataGridView1.Rows[i].Cells["f_rctotalweight"].Value);

                        dr["f_offload"] = Convert.ToString(dataGridView1.Rows[i].Cells["f_offload"].Value);

                        dr["f_model1"] = Convert.ToString(dataGridView1.Rows[i].Cells["f_model1"].Value);

                        dr["f_carTel"] = Convert.ToString(dataGridView1.Rows[i].Cells["f_carTel"].Value);

                        dt.Rows.Add(dr);

                        // LoginUser bll = new LoginUser();

                        string id = dr["id"].ToString();

                        // bll.UptIsExcel(id)

                    }

                }//for

//导出excel的代码

    private void button1_Click(object sender, EventArgs e)

        {

            SaveFileDialog saveFileDialog = new SaveFileDialog();

            saveFileDialog.Filter = "Excel files (*.xls)|*.xls";

            saveFileDialog.FilterIndex = 0;

            saveFileDialog.RestoreDirectory = true;

            saveFileDialog.CreatePrompt = true;

            saveFileDialog.Title = "导出Excel文件到";

            saveFileDialog.ShowDialog();

            try

            {

                Stream myStream;

                myStream = saveFileDialog.OpenFile();

                StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("gb2312"));

                string str = "";

                //写标题 

                for (int i = 0; i < dataGridView1.ColumnCount; i++)

                {

                    if (i > 1)

                    {

                        str += "\t";

                    }

                    str += dataGridView1.Columns[i].HeaderText;

                }

                sw.WriteLine(str);

                //写内容

                for (int j = 0; j < dataGridView1.Rows.Count; j++)

                {

                    string tempStr = "";

                    for (int k = 0; k < dataGridView1.Columns.Count; k++)

                    {

                        if (k > 0)

                        {

                            tempStr += "\t";

                        }

                        tempStr += dataGridView1.Rows[j].Cells[k].Value.ToString();

                    }

                    sw.WriteLine(tempStr);

                }

                MessageBox.Show("导出成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                sw.Close();

                myStream.Close();

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.ToString());

            }

            finally

            {

                //sw.Close();

                //myStream.Close();

            }

        }

获取当前时间并在过去一周时间的查询

String s=“Select * from 表名  where dateTime>='”+DateTime.Now.AddDays(-7).Tostring()+"' and dateTime<='"+DateTime.Now.ToLocalTime().ToString()+"' order by dateTime desc";

获取时间控件的时间并在其过去一周的查询

 DateTime dt=this.dateTimePicker1.Value;

String sql="select * from 表名 where dateTiem>='"+dt.AddDays(-7).ToString()+"'  and dateTiem<="+this.dateTimePicker1.Value.Tostring()+"' order by [dateTime] desc";

数据库case when 操作

select f_site_code,f_site_name,f_road_Coding,mileage,meter,

count(f_hspid) totalNum,

sum(CASE WHEN f_overlimited > 1000 THEN 1 ELSE 0 END  ) AS overNum,

0 as cxl

from b_site a left join b_hspinfo h on a.f_site_code = h.f_stationid where a.f_type='1'

group by f_site_code,f_site_name,f_road_Coding,mileage,meter;

select  (case when f_speed is null then 12 else f_speed end ) as f_speed from b_hspinfo;

  1. 查询语句拼接,条件不确定时,可以先写 where 1=1;
  2. sqlDataAdapter 连接 DataSet 和数据库 ;sqlDataAdapter.fill()方法可以填充到dataset数据集中去,第二参数为绑定dataset中的数据;绑定个表名,不写的话,再次执行fill方法,将数据追加到默认表的后面。
  3. Dataset.Tables["表名"]去获取指定表数据
  4. DataTable 对象可以通过dataset赋值表数据,也可以自己设置表数据;其中行操作,DataTable.columns.Add()添加列名dataTable.Rows["行索引"]["列索引"] 去操作表中每一行,每一列的数据
  5. sql语句中case 的用法:Case 表达式(可省略) when 判读 then 处理  else 默认处理 end 可以用于判断输出, 过滤输出
  6. As 用于 重命名 或者省略
  7. Select 列名="默认值"  填加一列默认值的数据
  8. 报表文件与数据集文件同用
  9. 报表文件需要绑定数据集文件
  10. 报表文件设置当前页的表达式:"第" & Globals!PageNumber & "页/共" & Globals!TotalPages & "页"
  11. 获取当前时间 CDate(Now()).ToString("yyyy-MM-dd:HH:mm")

窗体设置打印报表代码

Microsoft.Reporting.WinForms.ReportDataSource reportDataSource1 = new Microsoft.Reporting.WinForms.ReportDataSource();

 reportViewer1.Reset();

  reportViewer1.LocalReport.DataSources.Clear();

   this.reportViewer1.LocalReport.ReportPath = @"..\..\RepNoStopCar.rdlc";

   reportViewer1.LocalReport.ReportEmbeddedResource = "winFmain.Fm_RptNoStopCar.RepNoStopCar.rdlc";

   reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DataSet1", dt));

  this.reportViewer1.RefreshReport();

行转列操作。。

你可能感兴趣的:(2018年6月C#winform项目开发总结)