如何使用C#Form应用程序以另一种形式显示网格视图的选定行?

时间:2016-01-16 08:30:09

标签: c# datagridview

当我选择具有双击事件的行时,我希望以第二种形式将DataGridView的显示数据显示为第一种形式的文本框。

private void button1_Click(object sender, EventArgs e)
        {
            FRM_DRIVERS_LIST frm = new FRM_DRIVERS_LIST();
            frm.ShowDialog();
            ID = Convert.ToInt32(frm.dGV_Driver.SelectedRows[0].Cells[0].Value);
            this.txt_code.Text = frm.dGV_Driver.SelectedRows[0].Cells[1].Value.ToString();
            this.txt_Driver_Name.Text = frm.dGV_Driver.SelectedRows[0].Cells[2].Value.ToString();
            this.txt_Tel1.Text = frm.dGV_Driver.SelectedRows[0].Cells[3].Value.ToString();
            this.txt_Tel2.Text = frm.dGV_Driver.SelectedRows[0].Cells[4].Value.ToString();
            this.txt_Address.Text = frm.dGV_Driver.SelectedRows[0].Cells[5].Value.ToString();
            this.dtp_S_work.Text = frm.dGV_Driver.SelectedRows[0].Cells[6].Value.ToString();
            this.CB_WorkPlace.Text = frm.dGV_Driver.SelectedRows[0].Cells[7].Value.ToString();
            this.CB_License_type.Text = frm.dGV_Driver.SelectedRows[0].Cells[8].Value.ToString();
            this.dTP_Date_e_License.Text = frm.dGV_Driver.SelectedRows[0].Cells[9].Value.ToString();
            txt_code.ReadOnly = true;
            btn_Edite.Enabled = true;
            btn_Delete.Enabled = true;
            btn_Add.Enabled = false;
        }

这是第二个包含DataGridView的表单代码: -

public partial class FRM_DRIVERS_LIST : Form
    {
        BL.CLS_DRIVERS clsDriver = new BL.CLS_DRIVERS();

        int ID;

        public FRM_DRIVERS_LIST()
        {
            InitializeComponent();
            this.dGV_Driver.DataSource = clsDriver.GET_ALL_DRIVERS();
            dGV_Driver.Columns[0].Visible = false;
        }


        private void txt_Search_TextChanged(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt = clsDriver.Search_Driver(txt_Search.Text);
            this.dGV_Driver.DataSource = dt;
        }

        private void dGV_Driver_DoubleClick(object sender, EventArgs e)
        {
            //FRM_DRIVERS frm = new FRM_DRIVERS();
            //ID = Convert.ToInt32(this.dGV_Driver.SelectedRows[0].Cells[0].Value);
            //frm.txt_code.Text = this.dGV_Driver.SelectedRows[0].Cells[1].Value.ToString();
            //frm.txt_Driver_Name.Text = this.dGV_Driver.SelectedRows[0].Cells[2].Value.ToString();
            //frm.txt_Tel1.Text = this.dGV_Driver.SelectedRows[0].Cells[3].Value.ToString();
            //frm.txt_Tel2.Text = this.dGV_Driver.SelectedRows[0].Cells[4].Value.ToString();
            //frm.txt_Address.Text = this.dGV_Driver.SelectedRows[0].Cells[5].Value.ToString();
            //frm.dtp_S_work.Text = this.dGV_Driver.SelectedRows[0].Cells[6].Value.ToString();
            //frm.CB_WorkPlace.Text = this.dGV_Driver.SelectedRows[0].Cells[7].Value.ToString();
            //frm.CB_License_type.Text = this.dGV_Driver.SelectedRows[0].Cells[8].Value.ToString();
            //frm.dTP_Date_e_License.Text = this.dGV_Driver.SelectedRows[0].Cells[9].Value.ToString();
            this.Close();
        }

此代码是有效但我有问题,当我关闭第二个表格时,第一行显示在第一个表格的文本框中我希望仅在我双击行时显示

2 个答案:

答案 0 :(得分:0)

Form3 frm3 = new Form3();
            frm3.textBox7.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            frm3.textBox6.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            frm3.textBox2.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
            frm3.textBox9.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
            frm3.dateTimePicker1.Text = dataGridView1.CurrentRow.Cells[4].Value.ToString();
            frm3.textBox5.Text = dataGridView1.CurrentRow.Cells[5].Value.ToString();
            frm3.textBox3.Text = dataGridView1.CurrentRow.Cells[7].Value.ToString();

在不同的表单上只显示特定文本框中的特定单元格...这会有所帮助

答案 1 :(得分:-1)

http://www.c-sharpcorner.com/UploadFile/thiagu304/passdata05172006234318PM/passdata.aspx

 DataRow row = gridView1.GetDataRow(gridView1.GetSelectedRows()[0]);
      Form2 frm = new Form2(row);
      frm.Show();

希望这会有所帮助。