DataGridView直接修改和自动完成.

版权声明: https://blog.csdn.net/eds124/article/details/83961399
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{

    public partial class Form1 : Form
    {
        string connstr = "Data Source=.;Initial Catalog=mydata;User ID=sa;Password=sa";
        DataGridView dgv = new DataGridView();

        public Form1()
        {
            InitializeComponent();
            dgv.CellValueChanged += new DataGridViewCellEventHandler(dgv_CellValueChanged);
            dgv.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dgv_EditingControlShowing);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
            dgv.Location = new Point(100, 100);
            dgv.Size = new Size(this.Size.Width - 200, this.Size.Height - 200);
            this.Controls.Add(dgv);

            using (SqlConnection conn = new SqlConnection())
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    using (SqlDataAdapter da = new SqlDataAdapter())
                    {
                        using (DataTable dt = new DataTable())
                        {
                            conn.ConnectionString = connstr;
                            cmd.Connection = conn;
                            cmd.CommandText = "select * from t_user";
                            da.SelectCommand = cmd;
                            da.Fill(dt);
                            dgv.DataSource = dt;
                            dgv.EditMode = DataGridViewEditMode.EditOnEnter;
                            dgv.Columns[0].ReadOnly = true;//第一列不可修改
                        }
                    }
                }
            }
        }

        private void dgv_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            using (SqlConnection conn = new SqlConnection())
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    string username = dgv.Rows[e.RowIndex].Cells[1].Value.ToString();
                    string gender = dgv.Rows[e.RowIndex].Cells[2].Value.ToString();
                    if (!(gender.Equals("男") || gender.Equals("女") || gender.Equals("无")))
                    {
                        MessageBox.Show("性别必须是男,女或者无");
                        return;
                    }
                    string birthday = dgv.Rows[e.RowIndex].Cells[3].Value.ToString();
                    
                    string address = dgv.Rows[e.RowIndex].Cells[4].Value.ToString();
                    string idnumber = dgv.Rows[e.RowIndex].Cells[5].Value.ToString();
                    string id = dgv.Rows[e.RowIndex].Cells[0].Value.ToString();
                    conn.ConnectionString = connstr;
                    cmd.Connection = conn;
                    cmd.CommandText = "update t_user set username = @username, gender = @gender, birthday = @birthday, address = @address, idnumber = @idnumber where id = @id";
                    conn.Open();
                    cmd.Parameters.AddWithValue("username", username);
                    cmd.Parameters.AddWithValue("gender", gender);
                    cmd.Parameters.AddWithValue("birthday", birthday);
                    cmd.Parameters.AddWithValue("address", address);
                    cmd.Parameters.AddWithValue("idnumber", idnumber);
                    cmd.Parameters.AddWithValue("id", id);
                    cmd.ExecuteNonQuery();
                }
            }
        }

        private void dgv_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            string address = dgv.Columns[dgv.CurrentCell.ColumnIndex].HeaderText;
            TextBox tb = e.Control as TextBox;
            if (!address.Equals("address") || tb == null)
            {
                return;
            }
            tb.AutoCompleteMode = AutoCompleteMode.Suggest;
            tb.AutoCompleteSource = AutoCompleteSource.CustomSource;
            AutoCompleteStringCollection acsc = new AutoCompleteStringCollection();
            acsc.Add("asdfklasdjkf");
            acsc.Add("asdfasdf");
            acsc.Add("asdfasdfsad");
            tb.AutoCompleteCustomSource = acsc;

        }
    }
}
阅读更多

没有更多推荐了,返回首页