The Perfect Day

分享技术,编写未来

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

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

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

        
public DataSet ExecuteSQLDataSet(string sqlTmp, string tableTableName)
        {
            sqlSelect 
= sqlTmp;
            sqlDA 
= new SqlDataAdapter(sqlSelect, sqlCONN);
            ds.Clear();
            sqlDA.Fill(ds, tableTableName);
            
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);
            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 CustomizeDataGridView(DataGridView dataGridView)
        {
            dataGridView.AllowUserToAddRows 
= false;
            dataGridView.AutoGenerateColumns 
= false;
            dataGridView.AlternatingRowsDefaultCellStyle.BackColor 
= Color.Azure;
            dataGridView.RowTemplate.Resizable 
= DataGridViewTriState.False;
            dataGridView.SelectionMode 
= DataGridViewSelectionMode.FullRowSelect;
            dataGridView.EditMode 
= DataGridViewEditMode.EditOnEnter;
            dataGridView.ColumnHeadersDefaultCellStyle.Alignment 
= DataGridViewContentAlignment.MiddleCenter;

            
for (int i = 0; i < dataGridView.ColumnCount; i++)
            {
                dataGridView.Columns[i].ReadOnly 
= true;
                dataGridView.Columns[i].SortMode 
= DataGridViewColumnSortMode.NotSortable; //不自动排序
            }
        }

        
/// <summary>
        
/// 给DataGridView添加行号
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>
        public void AddRowIndex(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            SolidBrush s 
= new SolidBrush(Color.Black);
            e.Graphics.DrawString(Convert.ToString(e.RowIndex 
+ 1), e.InheritedRowStyle.Font, s, e.RowBounds.X + 10, e.RowBounds.Y + 1);
        }

        
/// <summary>
        
/// 获取日期(格式:YYYY-MM-DD)
        
/// </summary>
        
/// <param name="DTP"></param>
        
/// <param name="date"></param>
        
/// <returns></returns>
        public string GetDate(DateTimePicker DTP, string date, string format)
        {
            
if (format == "YYYY-MM-DD")
            {
                
if (DTP.Value.Month <= 9)
                {
                    
if (DTP.Value.Day <= 9)
                        date 
= DTP.Value.Year.ToString() + "-0" + DTP.Value.Month.ToString() + "-0" + DTP.Value.Day.ToString();
                    
else
                        date 
= DTP.Value.Year.ToString() + "-0" + DTP.Value.Month.ToString() + "-" + DTP.Value.Day.ToString();
                }
                
else
                {
                    
if (DTP.Value.Day <= 9)
                        date 
= DTP.Value.Year.ToString() + "-" + DTP.Value.Month.ToString() + "-0" + DTP.Value.Day.ToString();
                    
else
                        date 
= DTP.Value.Year.ToString() + "-" + DTP.Value.Month.ToString() + "-" + DTP.Value.Day.ToString();
                }
            }
            
if (format == "YYYYMMDD")
            {
                
if (DTP.Value.Month <= 9)
                {
                    
if (DTP.Value.Day <= 9)
                        date 
= DTP.Value.Year.ToString() + "0" + DTP.Value.Month.ToString() + "0" + DTP.Value.Day.ToString();
                    
else
                        date 
= DTP.Value.Year.ToString() + "0" + DTP.Value.Month.ToString() + DTP.Value.Day.ToString();
                }
                
else
                {
                    
if (DTP.Value.Day <= 9)
                        date 
= DTP.Value.Year.ToString() + DTP.Value.Month.ToString() + "0" + DTP.Value.Day.ToString();
                    
else
                        date 
= DTP.Value.Year.ToString() + DTP.Value.Month.ToString() + DTP.Value.Day.ToString();
                }
            }
            
return date;
        }
    }
}
以上函数修改为:
 public string GetDate(DateTimePicker DTP, string date, string format)
        
{
            
if (format = "YYYY-MM-DD")
            
{
                date 
= DTP.Value.ToString("yyyy-MM-dd");
            }

            
if (format = "YYYYMMDD")
            
{
                date 
= DTP.Value.ToString("yyyyMMdd");
            }

            
return date;
        }

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 RZGY
{
    
public partial class FrmW_TD_GROOVE : Form
    {
        
public FrmW_TD_GROOVE()
        {
            InitializeComponent();
        }

        LinkDB link 
= new LinkDB();
        DataSet dsGroove 
= null;
        
string sqlGroove = "select * from TD_GROOVE";
        BindingSource bs 
= null;
        
private void FrmW_TD_GROOVE_Load(object sender, EventArgs e)
        {
            link.CustomizeDataGridView(dgvW_TD_GROOVE);
            dsGroove 
= new DataSet();
            dsGroove 
= link.ExecuteSQLDataSet(sqlGroove, "tbTD_GROOVE");
/*************************************************************************************/
            bs 
= new BindingSource(dsGroove, "tbTD_GROOVE");
            dgvW_TD_GROOVE.DataSource 
= bs;
/*************************************************************************************/

            FillcmbGrooveCode();
        }

        
private void FillcmbGrooveCode()
        {
            cmbGrooveCode.Items.Clear();
            DataTable dt 
= link.ExecuteSQLDataTable("select GROOVE_CODE from TD_GROOVE");
            cmbGrooveCode.Items.Add(
"全部");
            
for (int i = 0; i < dt.Rows.Count; i++)
            {
                
this.cmbGrooveCode.Items.Add(dt.Rows[i][0].ToString());
            }
        }
        
private void btnQuery_Click(object sender, EventArgs e)
        {
            
try
            {
                
string sqlFilter = "select * from TD_GROOVE";
                
string sqlFilterAdd = "";
                
if (cmbGrooveCode.Text.ToString() == "全部")
                {
                    sqlFilterAdd 
= "";
                }
                
if (cmbGrooveCode.Text.ToString() != "全部")
                {
                    sqlFilterAdd 
= " where GROOVE_CODE='" + cmbGrooveCode.SelectedItem.ToString() + "'";
                }
                sqlFilter 
+= sqlFilterAdd;
                dsGroove 
= link.ExecuteSQLDataSet(sqlFilter, "tbTD_GROOVE");
                dgvW_TD_GROOVE.DataSource 
= bs;
            }
            
catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        
private void btnAddNew_Click(object sender, EventArgs e)
        {
            
try
            {
                
if (dgvW_TD_GROOVE.DataSource != null)
                {
                    bs.AddNew();
                    dgvW_TD_GROOVE.CurrentCell 
= dgvW_TD_GROOVE[0, dgvW_TD_GROOVE.CurrentRow.Index]; //设定当前活动单元格
                    
//设置当前新增行为可写
                    dgvW_TD_GROOVE[0, dgvW_TD_GROOVE.CurrentRow.Index].ReadOnly = false;
                    dgvW_TD_GROOVE[
1, dgvW_TD_GROOVE.CurrentRow.Index].ReadOnly = false;
                    dgvW_TD_GROOVE[
2, dgvW_TD_GROOVE.CurrentRow.Index].ReadOnly = false;
                }
            }
            
catch(Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }

        
private void btnModify_Click(object sender, EventArgs e)
        {
            dgvW_TD_GROOVE[
0, currentSelectedRowIndex].ReadOnly = false;
            dgvW_TD_GROOVE[
1, currentSelectedRowIndex].ReadOnly = false;
            dgvW_TD_GROOVE[
2, currentSelectedRowIndex].ReadOnly = false;
        }

        
private void btnDelelte_Click(object sender, EventArgs e)
        {
            
try
            {
                
if (dsGroove.Tables["tbTD_GROOVE"].Rows.Count > 0)
                {
                    
int currentRowIndex = dgvW_TD_GROOVE.CurrentRow.Index;
                    bs.RemoveAt(currentRowIndex);
                }
            }
            
catch (Exception ERROR)
            {
                MessageBox.Show(ERROR.Message);
            }
        }

        
private void btnSave_Click(object sender, EventArgs e)
        {
            
try
            {
                bs.EndEdit();
//注意
                if (dsGroove.GetChanges() != null)
                {
                    
int changeNums = link.BatchUpdate(dsGroove.GetChanges(), "tbTD_GROOVE");
                    
if (changeNums > 0)
                    {
                        dsGroove.AcceptChanges();
//注意
                        FillcmbGrooveCode();
                        
for (int i = 0; i < dgvW_TD_GROOVE.Columns.Count; i++)
                        {
                            dgvW_TD_GROOVE.Columns[i].ReadOnly 
= true;
                        }
                        MessageBox.Show(
"保存成功!""提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    
else
                        dsGroove.RejectChanges();
                }
                
else
                    
return;
            }
            
catch (Exception ERROR)
            {
                MessageBox.Show(ERROR.Message);
            }
        }

        
private void btnPrint_Click(object sender, EventArgs e)
        {

        }

        
private void btnExit_Click(object sender, EventArgs e)
        {
            
this.Close();
        }

        
int currentSelectedRowIndex;
        
private void dgvW_TD_GROOVE_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            currentSelectedRowIndex 
= dgvW_TD_GROOVE.CurrentRow.Index;
        }

        
private void btnRefresh_Click(object sender, EventArgs e)
        {
            
//bs.ResetBindings(false);//使绑定到 System.Windows.Forms.BindingSource 的控件重新读取列表中的所有项,并刷新这些项的显示值
            
//如果数据架构已更改,则为 true;如果只有值发生了更改,则为 false
            bs = new BindingSource(dsGroove, "tbTD_GROOVE");
            dgvW_TD_GROOVE.DataSource 
= bs;
        }
    }
}

打印暂没实现...
注:以前写的几个窗体代码有问题,各位请原谅!最近修改!!
posted on 2007-11-27 01:49  StephenJu  阅读(1218)  评论(3编辑  收藏  举报