C# 面试  (datagrid || datagridView显示数据)

这篇博客介绍了如何使用C#进行数据访问,通过OperatorConnection类连接数据库并执行SQL查询。在业务层中,展示了如何从Sell_Details表中筛选商品数据,并通过DataGridView展示结果。在表示层,详细阐述了如何绑定数据到DataGridView以及处理用户交互事件,如双击行打开详细列表窗体。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

数据访问类

using System;
using System.Collections.Generic;
using System.Text;

using System.Data;
using System.Data.SqlClient;

namespace Interview
{
   
    class OperatorConnection
    {
        SqlConnection objSqlConnection = null;
        SqlCommand objSqlCommand  = null;
        DataSet objDataSet = null;
        SqlDataAdapter objSqlDataAdapter = null;


        public OperatorConnection()
        {
            objSqlConnection = new SqlConnection("database=Wear;server=.;uid=sa;pwd=sa");
           
            objSqlConnection.Open();
            if (objSqlCommand == null)
            {
                objSqlCommand = new SqlCommand();
                objSqlCommand.Connection = this.objSqlConnection;
            }
        }

        public DataSet search(String sql, String strTableName)
        {
            try
            {              
                objDataSet = new DataSet();
                objSqlCommand.CommandType = CommandType.Text;
                objSqlCommand.CommandText = sql;
                              
                objSqlDataAdapter = new SqlDataAdapter();
                objSqlDataAdapter.SelectCommand = objSqlCommand;
                objSqlDataAdapter.Fill(objDataSet,strTableName);                              
            }
            catch (Exception ce)
            {
                throw ce;
            }
            return objDataSet;
        }       
    }
}

业务层:

using System;
using System.Collections.Generic;
using System.Text;

using System.Data;
using System.Data.SqlClient;

namespace Interview
{
    class common
    {
        DataSet objDataSet = null;

        public DataSet findGoods(String ID,DateTime startTime,DateTime endTime)
        {
            OperatorConnection objConnection = new OperatorConnection();
            objDataSet = objConnection.search("select * from Sell_Details as se left join stock_Details as st on se.ClothesID = st.ClothesID left join ClothesMessage as cl on st.ProduceID = cl.ProducerID  where st.date>'" + startTime + "' and st.date <'" + endTime + "'","Sell_Details");
            // and SellID= '" + ID + "'"
            return objDataSet;
        }

        public DataSet findListGoods(String ID)
        {
            OperatorConnection objConnection = new OperatorConnection();
            objDataSet = objConnection.search("select * from Sell_Details where SellID = '" + ID + "'", "Sell_Details");
            return objDataSet;
        }
    }
}

表示层:

public partial class frmGoods : Form
    {
        DataSet objDataSet = null;
        public static String Number = null;
        public frmGoods()
        {
            InitializeComponent();
            ;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           
        }

        //查询的按钮
        private void BtnSelect_Click(object sender, EventArgs e)
        {
            common objcommon = new common();           
            String strID = this.txtID.Text.ToString();
            DateTime startTime = System.DateTime.Parse(this.dtpStartTime.Text.ToString());
            DateTime endTime = System.DateTime.Parse(this.dtpEndTime.Text.ToString());

            //隐藏主键 yourDS.Tables[yourTableName].Columns[yourKeyCol].ColumnMapping   =   MappingType.Hidden;
             //this.dgv.DataSource = objDataSet;            
            objDataSet = objcommon.findGoods(strID, startTime, endTime);
           
           
            //dataGrideView绑定数据  与dataGrid的区别是:它是一个视图,所以速度快
            //DataGridView。与 DataGrid 不同的是,DataGridView 适用于各种不同的现实情况
            //——无论是要处理数据绑定和用户编辑,还是仅涉及静态文本显示,都可以采用 DataGridView。
            this.dgv.DataSource = this.objDataSet.Tables[0].DefaultView;
           
            this.dgr.SetDataBinding(objDataSet, "Sell_Details");
        }

       
        //当前行变化 的事件
        private void dgr_CurrentCellChanged(object sender, EventArgs e)
        {
            dgr.Select(dgr.CurrentCell.RowNumber);

            Number = objDataSet.Tables[0].Rows[dgr.CurrentCell.RowNumber]["SellID"].ToString();  
        }

        //
        private void dgr_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            System.Drawing.Point pt = new Point(e.X, e.Y);
            DataGrid.HitTestInfo hti = dgr.HitTest(pt);
            if (hti.Type == DataGrid.HitTestType.Cell)
            {
                dgr.CurrentCell = new DataGridCell(hti.Row, hti.Column);               
                dgr.Select(hti.Row);
            }
            //调用详细列表的窗体
            frmListGoods objfrmListGoods = new frmListGoods();
            objfrmListGoods.Show();
        }

        //
        private void dgv_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            if (this.dgv.Rows.Count != 0)
            {
                for (int i = 0; i < this.dgv.Rows.Count; )
                {
                    this.dgv.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.Pink;
                    i += 2;
                }
            }
        }

             
        //dataGridView离开时的事件
        private void dgv_MouseUp(object sender, MouseEventArgs e)
        {
            string value = this.dgv.Rows[dgv.CurrentRow.Index].Cells[0].Value.ToString();
            MessageBox.Show(value);
        }

    }

public partial class frmListGoods : Form
    {
        DataSet objDataSet = null;
        public frmListGoods()
        {
            InitializeComponent();
        }

        private void frmListGoods_Load(object sender, EventArgs e)
        {
            common  objcommon = new common();

            objDataSet = objcommon.findListGoods(frmGoods.Number);

            this.dgrList.SetDataBinding(objDataSet, "Sell_Details");


            //绑定文本框
            DataView dv = this.objDataSet.Tables[0].DefaultView;
            this.dgrList.DataSource = dv;           
            this.textBox1.DataBindings.Add(new Binding
                  ("Text", dv, "DpotName"));
            this.textBox2.DataBindings.Add(new Binding
                  ("Text", dv, "CustomerID"));  
        }
    }

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值