将数据从文本框传递到datagridview的选定单元格 [英] passing the data from textbox to the selected cell of datagridview

查看:160
本文介绍了将数据从文本框传递到datagridview的选定单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用winform,我有一个datagridview ..我已将选定的单元格值转移到这样的新表单



I am working with winform, I have a datagridview.. i have transferred the selected cell value to a new form like this

private void editToolStripMenuItem_Click(object sender, EventArgs e)
    {

        Form2 f2 = new Form2();
            f2.label1.Text = dataGridView1.SelectedCells[0].Value.ToString();
            f2.ShowDialog();
    }



但是现在我想将form2中的Textbox值转移到该单元格中我可以这样做吗?表示form2中有一个文本框,它是textbox1,按钮是按钮1,当点击按钮然后我想将textbox.text传输到被选中的单元格



我在一个名为edit的菜单中有一个contextmenustrip,当点击编辑时会写入它将值传输到标签我可以将值重新传输到我选择的那个单元格



这是第二个表单按钮事件,其中我有代码但是当按下按钮时它会出现索引超出范围的错误。




But now i want to transference the Textbox value which is in the form2 to that cell can i do that? means there is a textbox in the form2 which is textbox1 and a button i.e button1 when button is clicked then i want to transfer the textbox.text to the cell which was selected

I have a contextmenustrip in a menu named edit is written when edit is clicked it transfers the value to the label can i retransfer the value to that cell which i have selected

this is the second form button event where i have wirtten the code but when button is pressed it errors with index out of range.

public Form2()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
       Records fs = new Records();
       textBox1.Text =  fs.dataGridView1.SelectedCells[0].Value.ToString();
        fs.Show();
    }

推荐答案

好的,如果你想将数据从Form2传回Form1并将其放回到数据来自,然后除非你关闭Form2以返回数据,那么你唯一的选择是使用一个事件。



这并不困难,但它需要一个您需要做的工作很少。

您需要Form2处理的Form2事件和Form2中返回数据的Property。

Form2:

Ok, if you want to transfer data back from Form2 to Form1 and put it back in the place that data came from, then unless you close Form2to return the data, then your only option is to use an Event.

This isn''t difficult, but it takes a little work on your part.
You need an Event in Form2 which Form1 handles, and a Property in Form2 which returns the data.
Form2:
/// <summary>
/// Event to indicate Data Available
/// </summary>
public event EventHandler DataAvailable;
/// <summary>
/// Called to signal to subscribers that aata is available
/// </summary>
/// <param name="e"></param>
protected virtual void OnDataAvailable(EventArgs e)
    {
    EventHandler eh = DataAvailable;
    if (eh != null)
        {
        eh(this, e);
        }
    }
void returnDataButton_Click(object sender, EventArgs e)
    {
    OnDataAvailable(null);
    }
public string Data
    {
    get { return myTextBox.Text; }
    set { myTextBox.Text = value; }
    }

Form1:

Form1:

private void showForm2Button_Click(object sender, EventArgs e)
    {
    Form2 form2Instance = new Form2();
    form2Instance.DataAvailable += new EventHandler(form2Instance_DataAvailable);
    form2Instance.ShowDialog();
    }

void form2Instance_DataAvailable(object sender, EventArgs e)
    {
    Form2 f = sender as Form2;
    if (f != null)
        {
        textBox2.Text = f.Data;
        }
    }



代码的作用是在Form2中创建一个事件,告诉Form1新数据可用 - 然后处理程序选择它从该属性,通过报告数据的表格实例。



(希望现在你对实例,事件和<$ c $感到满意c> as 语法,所以通过一点阅读一切都有意义)


What the code does is create an event in Form2, that tells Form1 that new data is available - the handler then picks it up from the property, via the instance of the form that reported data was available.

(Hopefully, by now you are comfortable with instances, events and as syntax so it all makes sense with a little reading)


这篇关于将数据从文本框传递到datagridview的选定单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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