zoukankan      html  css  js  c++  java
  • [WinForms]



    //Author:Stephen.Ju
    //Date:2007-08-10

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Data;
    using System.Data.SqlClient;
    using System.Windows.Forms;
    using System.Drawing;

    namespace ZPGL
    {
        
    class LinkDB
        {
            
    private SqlConnection sqlCONN = new SqlConnection("server=.;integrated security=sspi;database=zupi");
            
    private SqlDataAdapter sqlDA = null;
            
    private DataSet ds = null;
            
    private string sqlSelect = "";
            
    /// <summary>
            
    /// 填充并返回数据集合
            
    /// </summary>
            
    /// <param name="sql"></param>
            
    /// <param name="tableName"></param>
            
    /// <returns></returns>
            public DataSet ExecuteSQLDataSet(string sql,string tableName)
            {
                sqlSelect 
    = sql;
                sqlDA 
    = new SqlDataAdapter(sqlSelect, sqlCONN);
                ds 
    = new DataSet();
                ds.Clear();
                sqlDA.Fill(ds, tableName);
                
    return ds;
            }

            
    /// <summary>
            
    /// 批量更新
            
    /// </summary>
            
    /// <param name="changedDS"></param>
            
    /// <param name="tableName"></param>
            
    /// <returns></returns>
            public int BatchUpdate(DataSet changedDS, string tableName)
            {
                
    int nums = 0;
                sqlDA 
    = new SqlDataAdapter(sqlSelect, sqlCONN);
                SqlCommandBuilder sqlCMDBLD 
    = new SqlCommandBuilder(sqlDA);
                sqlDA.InsertCommand 
    = sqlCMDBLD.GetInsertCommand();
                sqlDA.DeleteCommand 
    = sqlCMDBLD.GetDeleteCommand();
                sqlDA.UpdateCommand 
    = sqlCMDBLD.GetUpdateCommand();
                
    try
                {
                    
    lock (this)
                    {
                        nums 
    = sqlDA.Update(changedDS, tableName);
                    }
                }
                
    catch (Exception Ex)
                {
                    MessageBox.Show(Ex.Message);
                }
                
    return nums;
            }

            
    /// <summary>
            
    /// 自定义DataGridView
            
    /// </summary>
            
    /// <param name="dataGridView"></param>
            public void CustomersizeDataGridView(DataGridView dataGridView)
            {
                dataGridView.AllowUserToAddRows 
    = false;
                dataGridView.AutoGenerateColumns 
    = false;
                dataGridView.AlternatingRowsDefaultCellStyle.BackColor 
    = Color.Azure;
                
    for (int i = 1; i < dataGridView.ColumnCount; i++)
                {
                    dataGridView.Columns[i].ReadOnly 
    = true
                    dataGridView.Columns[i].SortMode 
    = DataGridViewColumnSortMode.NotSortable; //不自动排序
                    dataGridView.Columns[i].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; //列标题字体居中
                }
            }
        }
    }

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;

    namespace ZPGL
    {
        
    public partial class Frm_og_order_t : ZPGL.FrmParent
        {
            
    public Frm_og_order_t()
            {
                InitializeComponent();
            }

            LinkDB link 
    = new LinkDB();
            
    string sqlSelect = "select * from og_order_t", tbName = "og_order_t";
            DataSet ds_og_order_t;
            BindingSource bs 
    = null;
            
    private void Frm_og_order_t_Load(object sender, EventArgs e)
            {
                link.CustomersizeDataGridView(
    this.dataGridView1);
                ds_og_order_t 
    = new DataSet();
                ds_og_order_t 
    = link.ExecuteSQLDataSet(sqlSelect, tbName);
                bs 
    = new BindingSource(ds_og_order_t, tbName);
                dataGridView1.DataSource 
    = bs;
            }

            
    //查询
            private void btnQuery_Click(object sender, EventArgs e)
            {
                
    try
                {
                    
    string sqlFilter = "select * from og_order_t", sqlFilterAdd = "";
                    
    if (txtOrder.Text.Trim() != "")
                    {
                        sqlFilterAdd 
    = " where order_no='" + txtOrder.Text.Trim() + "'";
                    }
                    
    if (txtDealFlag.Text.Trim() != "")
                    {
                        
    if (sqlFilterAdd == "")
                            sqlFilterAdd 
    = " where deal_flag='" + txtDealFlag.Text.Trim() + "'";
                        
    if (txtDealFlag.Text.Trim() != "")
                            sqlFilterAdd 
    += " and deal_flag='" + txtDealFlag.Text.Trim() + "'";
                    }
                    sqlFilter 
    += sqlFilterAdd;
                    ds_og_order_t 
    = link.ExecuteSQLDataSet(sqlFilter, tbName);
                    bs 
    = new BindingSource(ds_og_order_t, tbName);
                    dataGridView1.DataSource 
    = bs;
                    IsModify(
    true);
                }
                
    catch (Exception ERR)
                {
                    MessageBox.Show(ERR.Message, 
    "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

            
    //组批
            private void btnZupi_Click(object sender, EventArgs e)
            {

            }

            
    //修改
            private void btnModify_Click(object sender, EventArgs e)
            {
                IsModify(
    false);
            }

            
    //删除
            private void btnDelete_Click(object sender, EventArgs e)
            {
                
    //if (dataGridView1.Rows.Count > 0)
                
    //{
                
    //    int i = dataGridView1.CurrentRow.Index;
                
    //    try
                
    //    {
                
    //        dataGridView1.Rows.RemoveAt(i);//或者:Remove(dataGridView1.Rows[i]);
                
    //        dataGridView1.BindingContext[ds_og_order_t,tbName].RemoveAt(i);
                
    //    }
                
    //    catch (Exception ERR)
                
    //    {
                
    //        MessageBox.Show(ERR.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                
    //    }
                
    //}

                
    /*******************加复选框的删除*******************/
                List
    <DataGridViewRow> tmplist = new List<DataGridViewRow>();
                
    for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    
    if (Convert.ToBoolean(dataGridView1[0, i].EditedFormattedValue.ToString()))
                    {
                        tmplist.Add(dataGridView1.Rows[i]);
                    }
                }
                
    for (int j = 0; j < tmplist.Count; j++)
                {
                    dataGridView1.Rows.Remove(tmplist[j]);
                }
                tmplist 
    = null;
            }

            
    //保存
            private void btnSave_Click(object sender, EventArgs e)
            {
                
    try
                {
                    bs.EndEdit();
                    
    if (ds_og_order_t.GetChanges() != null)
                    {
                        
    int changeNums = link.BatchUpdate(ds_og_order_t.GetChanges(), tbName);
                        
    if (changeNums > 0)
                        {
                            ds_og_order_t.AcceptChanges();
                            MessageBox.Show(
    "保存成功!""提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            IsModify(
    true);
                        }
                        
    else
                            ds_og_order_t.RejectChanges();
                    }
                    
    else
                        IsModify(
    true);
                }
                
    catch (Exception ERR)
                {
                    MessageBox.Show(ERR.Message, 
    "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

            
    //返回
            private void btnReturn_Click(object sender, EventArgs e)
            {
                
    this.Close();
            }

            
    private void IsModify(bool bl)
            {
                
    if (bl == true)
                {
                    
    for (int i = 2; i < dataGridView1.ColumnCount; i++)
                    {
                        dataGridView1.Columns[i].ReadOnly 
    = true;
                    }
                }
                
    if (bl == false)
                {
                    
    for (int i = 2; i < dataGridView1.ColumnCount; i++)
                    {
                        dataGridView1.Columns[i].ReadOnly 
    = false;
                    }
                }
            }

            
    //给dataGridView添加行号
            private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
            {
                SolidBrush s 
    = new SolidBrush(Color.Black);
                e.Graphics.DrawString((e.RowIndex).ToString(), e.InheritedRowStyle.Font, s, e.RowBounds.X 
    + 10, e.RowBounds.Y + 1);
            }
        }
    }
  • 相关阅读:
    Excel导出到浏览器(个人备份)
    关于js性能
    关于JQuery(最后一点动画效果*)
    Python 2.7 学习笔记 内置语句、函数、标准库
    Python 2.7 学习笔记 模块和包
    Python 2.7 学习笔记 异常处理
    Python 2.7 学习笔记 字典(map)的使用
    Python 2.7 学习笔记 元组的使用
    Python 2.7 学习笔记 列表的使用
    Python 2.7 学习笔记 条件与循环语句
  • 原文地址:https://www.cnblogs.com/perfect/p/866649.html
Copyright © 2011-2022 走看看