2015-02-12 17 views
0

我一直在处理我的datagridview属性,并想知道如何将我选择的数据行的值传递给另一个表单。当datagridviewbutton单元被点击时如何将datagridview值传递给另一个表单

private void dgvRptView_CellContentClick(object sender, DataGridViewCellEventArgs e) 
    { 
     var senderGrid = (DataGridView)sender; 
     if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn && 
      e.RowIndex >= 0) 
     { 

       Form Update = new frmUpdateSvcRep(); 
       Update.Show(); 
     } 
    } 

显然,我只能在datagridview中添加按钮,并在点击按钮时添加了一个事件,它会显示一个表单。然而。我一直在试图将我选择的值传递给另一个文本框,但无济于事。有人可以帮我弄清楚如何通过点击我的按钮来传递价值吗?这是我的图像标题。

How to pass the values of my current selected row to another form

这里是我的另一种形式的样子,当我点击的DataGridView内编辑按钮。

how can I pass the value in the rows I selected

我真的从我的深度现在...我在创建构造选择,但我不知道如何实现它在这种情况下。提前致谢

+0

你分配给DataGridView .. DataTable是什么?列表? – 2015-02-12 02:55:35

+0

SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = cmd; DataTable dt = new DataTable(); da.Fill(dt); (dt.Rows.Count> 0) if(dt.Rows.Count> 0) dgvRptView.DataSource = dt; }“我分配了数据表来填充我的数据到datagridview” – 2015-02-12 03:00:01

回答

1

有几种方法可以在Forms之间完成传递数据。一个方法,如你所说,是通过构造当实例的形式传递:

private void dgvRptView_CellContentClick(object sender, DataGridViewCellEventArgs e) 
{ 
    if (dgvRptView.Columns[e.ColumnIndex] is DataGridViewButtonColumn && 
     e.RowIndex >= 0) 

    if (dgvRptView.CurrentRow != null) 
    { 
     var row = dgvRptView.CurrentRow.Cells; 

     DateTime age = Convert.ToDateTime(row["MyColumn"].Value); 
     string name = Convert.ToString(row["MyName"].Value); 

     Form Update = new frmUpdateSvcRep(age, name); 
     Update.Show(); 
    } 
} 

更新你的其它形式的构造函数接受这些参数:

public class Update : Form 
{ 
    public Update(DateTime age, string name) 
    { 
     // do whatever you want with the parameters 
    } 

    ... 
} 

这可能是很有诱惑力的只是传递整个dgvRptView.CurrentRow对象,但我建议反对。您的其他表单必须知道DataGridView中的列,以便它可以访问这些值,这不是应该关注的值,并且在列名更改时可能会导致运行时错误。

+0

好吧,我会给它一个镜头。 – 2015-02-12 03:26:00

+0

我明白了。但即时通讯很难确定什么属性datetimepicker属于?你可以帮我吗? – 2015-02-12 05:56:27

+0

DateTimePicker?这是怎么回事?你的意思是DateTime吗? – 2015-02-12 05:57:29

相关问题