有疑问

来源:互联网 发布:c语言buffer 编辑:程序博客网 时间:2024/05/16 18:15
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Data.SqlClient;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace _20160514_DataGridview{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void Form1_Load(object sender, EventArgs e)        {            // TODO: 这行代码将数据加载到表“maibo0415DataSet.xx_bumen”中。您可以根据需要移动或删除它。            this.xx_bumenTableAdapter.Fill(this.maibo0415DataSet.xx_bumen);            dataGridView2.DataSource = mydataSourse().Tables[0];   //table[0]?        }        DataSet mydataSourse()        {            string mycstr = "server=.;user=sa;pwd=sa;database=maibo0415";            SqlConnection myconn = new SqlConnection(mycstr);            string myselection = "select *from xx_yuangong";            DataSet mydataset = new DataSet();            try            {                myconn.Open();                SqlDataAdapter mydataapdter = new SqlDataAdapter(myselection, myconn);                mydataapdter.Fill(mydataset,"mytable");                this.dataGridView1.AutoGenerateColumns = true;                              //自动生成列                                                                                         //  this.dataGridView1.DataSource=my                this.dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;            }            catch (Exception ex){ MessageBox.Show(ex.Message); }            finally { myconn.Close(); }            return mydataset;                                                                 //return        }        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)        {            int row = dataGridView1.CurrentCell.RowIndex + 1;            int collm = e.ColumnIndex + 1;                                                     //e            string myvalue = dataGridView1.CurrentCell.Value.ToString();            MessageBox.Show(row.ToString()+collm.ToString()+myvalue.ToString());        }        private void button1_Click(object sender, EventArgs e)        {            dataGridView1.Columns[4].Visible = false;        }        private void button2_Click(object sender, EventArgs e)        {            dataGridView1.Columns[4].Visible =true;        }        private void button3_Click(object sender, EventArgs e)        {            dataGridView1.Rows[5].Visible = false;        }        public int rowIdex =0;        private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)        {            if (e.Button == MouseButtons.Right)            {                this.dataGridView1.Rows[e.RowIndex].Selected = true;                             //可被选择                rowIdex = e.RowIndex;                this.dataGridView1.CurrentCell = this.dataGridView1.Rows[e.RowIndex].Cells[1];   //清除选择?              //this.contextMenuStrip1.Show(this.dataGridView1,e.Location);                this.contextMenuStrip1.Show(Cursor.Position);                                    //鼠标            }        }        private void contextMenuStrip1_Click(object sender, EventArgs e)        {            if (!this.dataGridView1.Rows[this.rowIdex].IsNewRow)                this.dataGridView1.Rows.RemoveAt(rowIdex);        }        private void button4_Click(object sender, EventArgs e)        {            dataGridView1.Sort(dataGridView1.Columns[3], ListSortDirection.Ascending);        }        private void button5_Click(object sender, EventArgs e)        {            DataView mydv = new DataView(this.maibo0415DataSet.xx_bumen, "部门名称='人事部'","部门人数 asc",DataViewRowState.CurrentRows);            //   mydv = maibo0415DataSet.xx_bumen,"部门名称='人事部'"            dataGridView1.DataSource = mydv;        }        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)        {            string mystr0 = "server =.; user = sa; pwd = sa; database = maibo0415";            SqlConnection myconn = new SqlConnection(mystr0);            try            {                string mystr1 = dataGridView1.Columns[e.ColumnIndex].HeaderText + "=" + "'" + dataGridView1.CurrentCell.Value.ToString() + "'"; //列名                string mystr2 = "'" + dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString() + "'";                myconn.Open();                string myupdata = "update xx_bumen set " + mystr1 + "where 部门名称=" + mystr2;                SqlCommand mycom = new SqlCommand(myupdata,myconn);                mycom.ExecuteNonQuery();                mydataSourse();            }            catch (Exception ex) { MessageBox.Show(ex.Message);}            finally { myconn.Close(); }        }    }}
0 0
原创粉丝点击