在窗口应用程序中传递值 [英] Passing Value in Window Application

查看:89
本文介绍了在窗口应用程序中传递值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在窗口应用程序中的按钮单击事件中将文本框的值(放置在窗口表单上)传递到datagridview的选定行(放置在另一个窗口表单上).
给我一个合适的答案..

解决方案

您无法更改现有事件中传递的内容.您需要编写自己的事件,或者通常编写自己的代码以使用所需的值来响应现有事件.


您的问题尚不清楚,但我发布的代码部分可能会对您有所帮助...

 如果(dataGridView1.DataSource!= )
{
    DataTable dt =(DataTable)dataGridView1.DataSource;
    如果(dataGridView1.SelectedRows.Count ><  = 返回; }
    如果(dt.Rows.Contains(dataGridView1.SelectedRows [ 0 ]))
    {
        DataRow [] dr = dt.Rows.Find(dt.Rows [ 0 ] [" 字段名称"]);
        如果(长度Dr.Length <  =  0 )返回// 更改数据行的值
        dr [ 1 ] = txtName.Text; // 设置相关值
        dr [ 2 ] = txtcity.Text; // 设置相关值
        // 现在接受更改... 
        dt.AcceptChanges();
        dataGridView1.Refresh();
    }
} 


选项太多...是的,您应该提供更多信息:).您真的必须使用点击事件吗?
如果是,请在编辑"表单中创建一个新事件,将文本框的值作为参数传递,并使用datagridview订阅该事件.或者,您也可以通过编辑"表单构造函数/自定义显示方法传递对datagridview的引用,尽管我强烈建议您不惜一切代价避免这种做法.
如果您只需要在表单中设置值-即编辑"表单是模态的或可能是模态的-那么,只需通过构造函数/自定义显示方法返回新的文本值

i want to pass a textbox''s value (place on a window form) to a datagridview''s selected row (place on another window form) on a click event of a button in window application.
give me a suitable answer plz..

解决方案

You can''t change what gets passed in the existing event. You''ll need to write your own event, or generally write your own code to respond to the existing event, with the values you''re looking for.


Your question is not clear, but i''m posting portion of code that may help you......

if (dataGridView1.DataSource != null)
{
    DataTable dt = (DataTable)dataGridView1.DataSource;
    if (dataGridView1.SelectedRows.Count <= 0) { return; }
    if (dt.Rows.Contains(dataGridView1.SelectedRows[0]))
    {
        DataRow[] dr = dt.Rows.Find(dt.Rows[0]["fieldname"]);
        if (dr.Length <= 0) return;
        //change the value of datarow
        dr[1] = txtName.Text; //  set related value
        dr[2] = txtcity.Text; //  set related value
        // now accept the change...
        dt.AcceptChanges();
        dataGridView1.Refresh();
    }
}


There are so many options... and yes, you should have provided more information :). Do you really have to use click event?
If yes - create a new event in your "edit" form, pass textbox''s value as an argument and subscribe to this event in form with datagridview. Or you can pass a reference to your datagridview in "edit" form constructor/custom show method, though I strongly recommend you to avoid this practice at all costs.
If you just need to set value in your form - ie "edit" form is modal or may be made so - well, just return new text value via constructor/custom display method


这篇关于在窗口应用程序中传递值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆