Datagridview将值传递到文本框

问题描述:

我使用c#.net Windows应用程序,我需要在双击datagrid rowcell时将值传递给另一种形式的文本框,并且需要编辑并再次保存到另一种形式的同一个网格中

Hi iam using c#.net windows application i need to pass values to textboxes in another form on double click of datagrid rowcell and need to edit and again save to the same grid which is another form

纳伦德拉,

请执行以下步骤,可能会对您有所帮助.

1.以您要打开的形式声明一个构造函数和一个公共变量.
Hi Narendra,

Follow this steps it may help you.

1. declare a constructor and a public variable in the form which you want to open.
public  string strvalue = string.Empty;
        public Form2()
        {
            InitializeComponent();
        }
        public Form2(string str)
        {
            strvalue = str;
            InitializeComponent();
        }



2.像这样打开form2



2.Open form2 like this

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1)
            {
                if (e.ColumnIndex != 1)
                {
                    if (!string.IsNullOrEmpty(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString()))
                    {
                        string strValue = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
                        Form2 _Form2 = new Form2(strValue);
                        _Form2.ShowDialog();
                        dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = _Form2.strvalue;
                        dataGridView1.Refresh();
                    }
                }
            }
        }


3.在事件上关闭form2并在变量中设置值.


3. Close form2 on a event and set value in variable.

private void button1_Click(object sender, EventArgs e)
       {
           strvalue = textBox1.Text;
           this.Close();
       }


希望这对您有帮助,

Hope this will help you,

private void GetValueFromDGVToTextBox()
        {
            int row = this.dataGridView1.CurrentCell.RowIndex;
            if (row > -1)
            {
                string value = this.dataGridView1[1, row].FormattedValue.ToString();
                textBox1.Text = value;
            }
        }