点击datagridview任何单元格赋值别一个窗体Textbox中

来源:互联网 发布:乖离账号数据丢失 编辑:程序博客网 时间:2024/06/05 17:57

Form1窗体

datagridview属性中selectionMode选择FullRowSelect



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 点击datagridview任何单元格赋值别一个窗体Textbox中
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
         
        }
        SqlConnection conn;
        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                conn = new SqlConnection("server=WINDOWS-CBBU2E6;database=OrderManagementSystem;Uid = liweilu; Pwd = liweilu123");
                SqlCommand cmd = new SqlCommand("select*from dbo.Table_2", conn);
                SqlDataAdapter sda = new SqlDataAdapter();
                sda.SelectCommand = cmd;
                DataSet ds = new DataSet();
                sda.Fill(ds, "dbo.Table_2");
                dataGridView1.DataSource = ds.Tables["dbo.Table_2"];


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                throw ex;
            }
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            string data1 = dataGridView1.SelectedCells[0].Value.ToString();
            string data2 = dataGridView1.SelectedCells[1].Value.ToString();
           string data3 = dataGridView1.SelectedCells[2].Value.ToString();
           string data4 = dataGridView1.SelectedCells[3].Value.ToString();
            Form2 frm = new Form2();
            frm.textBox1.Text = data1;
           frm.textBox2.Text = data2;
            frm.textBox3.Text = data3;
           frm.textBox4.Text = data4;
          // frm.tabControl1.Text = "tabPage2";
            frm.Show();
        }
    }
}



Form2窗体

Textbox1,Textbox2,Textbox3,Textbox4属性全部选择Public



阅读全文
0 0