有疑问

本文介绍了一个使用 C# 编写的 DataGridView 控件的应用实例,展示了如何连接数据库、填充数据、响应单元格点击事件等操作,并提供了对数据进行排序、筛选和更新的方法。

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

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(); }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值