DataGridView 里数据的动态明细 DataGridView GridView

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

namespace 信息明细
{
    public partial class Form1 : Form
    {
        private SqlConnection objSqlConnection;
        private SqlDataAdapter objSqlAdapter;
        public DataSet dataset;
        //用于执行SQL语句的方法只而传值第一个对数的表名第二个是要执行的SQL语句
        public DataSet exeSql(string tableName, string strSql)
        {
            //this.sqlConncetion();
            try
            {
                objSqlConnection.Open();//打开连接    
                objSqlAdapter = new SqlDataAdapter(strSql, objSqlConnection);
                dataset = new DataSet();
                objSqlAdapter.Fill(dataset, tableName);//把得到的值给DataGrid
                return dataset;
            }
            catch (Exception ee)
            {
                MessageBox.Show("\n语句执行失败请检测您的输入信息后再重试!!!发生异常的原因如下\n\n" + ee.Message.ToString(), "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                return null;
            }
            finally
            {
                objSqlConnection.Close();
            }
        }
        public void sqlConncetion()
        {
            try
            {
                objSqlConnection = new SqlConnection("Data Source=.;Initial Catalog=student;Integrated Security=True");
            }
            catch (Exception)
            {
                MessageBox.Show("没有可用的数据库或连接不成功,请确保数据库已附加,请自行检测谢谢合作!!!");
            }
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            明细ToolStripMenuItem_Click(sender, e);
        }

        private void 明细ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            select objselect = new select();
            objselect.myDggrid = dataGridView1;
            objselect.objdatset = dataset;
            objselect.TopMost = true;
            objselect.Show();
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }

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

        private void button1_Click(object sender, EventArgs e)
        {
            dataGridView1 .DataSource=exeSql("score","select * from score").Tables [0];

        }

        private void button2_Click(object sender, EventArgs e)
        {
            dataGridView1.DataSource = exeSql("studentInfo", "select * from studentInfo").Tables[0];
        }
    }
}

 

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

namespace 信息明细
{
    public partial class select : Form
    {
        public select()
        {
            InitializeComponent();
        }
        public DataGridView myDggrid=new DataGridView ();
        public DataSet objdatset = new DataSet();
        private void select_Load(object sender, EventArgs e)
        {
            int f = 0;
            for (int i = 0; i < myDggrid.CurrentRow.Cells.Count; i++)
            {
                //add Lable
                Label lbltime = new Label();
                lbltime.AutoSize = true;
                //lbltime.Image = global::宿舍管理系统.Properties.Resources.title;
                lbltime.Location = new System.Drawing.Point(36,24+f);
                lbltime.Name = "lbltime"+i;
                lbltime.Size = new System.Drawing.Size(65, 12);
                lbltime.TabIndex = i;
                lbltime.Text = objdatset .Tables [0].Columns[i].ColumnName.ToString ();
                //add TextBox
                TextBox txttime = new TextBox();
                txttime.Location = new System.Drawing.Point(142, 24 + f);
                txttime.Name = "txttime"+i;
                txttime.ReadOnly = true;
                txttime.Size = new System.Drawing.Size(100, 21);
                txttime.TabIndex = i;
                txttime.Text = myDggrid.CurrentRow.Cells[i].Value.ToString().Trim();
                //add this.Controls
                this.Controls.Add(lbltime);
                this.Controls.Add(txttime);
                f = f + 38;
            }
        }
    }
}

你可能感兴趣的:(DataGridView 里数据的动态明细 DataGridView GridView)