C# 如何使用辅助文本框编辑datagridview单元格?

C# 如何使用辅助文本框编辑datagridview单元格?,c#,winforms,datagridview,C#,Winforms,Datagridview,我有带有datagridview的表单1(2列,1列id,1列文本) 我还有“编辑按钮”。当我单击“编辑按钮”,文本列将显示在表单2的文本框中 在表单2中我有“选择按钮”来编辑路径和“保存按钮” 如何通过按“保存按钮”将表单2中编辑的文本传递到表单1中的列datagridview 表单1中的代码编辑按钮(dgv_sourcefolder为datagridview1): 表单2中的代码选择按钮: private void Btn_select_Click(object sender, EventA

我有带有
datagridview
表单1(2列,1列id,1列文本)

我还有
“编辑按钮”
。当我单击
“编辑按钮”
,文本列将显示在表单2的文本框中

表单2中我有“
选择按钮
”来编辑路径和
“保存按钮”

如何通过按
“保存按钮”
表单2中编辑的文本传递到表单1中的列datagridview

表单1中的代码编辑按钮(dgv_sourcefolder为datagridview1):

表单2中的代码选择按钮:

private void Btn_select_Click(object sender, EventArgs e)
    {          
        FolderBrowserDialog fbd = new FolderBrowserDialog();         
        if(fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            textBox1.Text = fbd.SelectedPath;
        }

    }

你的表格应该是这样的

public class Form2 : Form 
    {

        private DataGridViewRow dataGridViewRow;
        public Form2(DataGridViewRow row) 
        {
            dataGridViewRow = row;
        }
        private void Btn_select_Click(object sender, EventArgs e)
        { 
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
               textBox1.Text = fbd.SelectedPath;
            }

        }
        private void Btn_Save_Click(object sender, EventArgs e)
        { 
             this.dataGridViewRow.Cells[1].Value = textBox1.Text;
        }
    }
it主要形式

private void DGV_sourcefolder_CellClick(object sender, DataGridViewCellEventArgs e)
    {          
        if (DGV_sourcefolder.Columns[e.ColumnIndex].Name == "Edit")
        {
            string y = "";
            int i;
            i = ((DataGridView)sender).SelectedCells[0].RowIndex;
            y = ((DataGridView)sender).Rows[i].Cells[1].Value.ToString();
            //MessageBox.Show(y.ToString());
            DTO.data = y;
            var row =  ((DataGridView)sender).Rows[i];
            Form2 form = new Form2(row);
            form.Show();
            Hide();
        }
    }

不需要对数据网格中的数据做任何假设,您可以使用DataGridViewCell的ParseFormattedValue方法和FormattedValue属性使form2中的文本框与数据网格中的文本框一样工作。如果在数据网格中不使用字符串作为值类型,这将有所帮助

public Form2
{
    TextBox _myTextBox; 
    public Form2()
    { ... }

    public DataGridViewCell CurrentCell {get;set;}

    protected override void OnLoad()
    {
        Assert(CurrentCell != null);
        _myTextBox = CurrentCell.FormatedValue;
    }

    public SubmitBtn_clicked(...)
    {
        try
        {
           var cellValue = CurrentCell.ParseFormattedValue(_myTextBox.Text,  
                     CurrentCell.Style, (TypeConverter)null, (TypeConverter)null);
           CurrentCell.Value = cellValue;
        }
        catch(FormatException)
        {/*user entered value that cant be parsed*/ }
        catch(ArgumentException)
        {/*_myTextBox.Text was null or cell's FormattedValueType is not string*/}
    }
}

谢谢,我已经试过了,但是弹出窗口显示“对象引用未设置为对象的实例。”在“保存”按钮。我将尝试修复:)确保此“var row=((DataGridView)sender).Rows[I];”不应为空对象。
public Form2
{
    TextBox _myTextBox; 
    public Form2()
    { ... }

    public DataGridViewCell CurrentCell {get;set;}

    protected override void OnLoad()
    {
        Assert(CurrentCell != null);
        _myTextBox = CurrentCell.FormatedValue;
    }

    public SubmitBtn_clicked(...)
    {
        try
        {
           var cellValue = CurrentCell.ParseFormattedValue(_myTextBox.Text,  
                     CurrentCell.Style, (TypeConverter)null, (TypeConverter)null);
           CurrentCell.Value = cellValue;
        }
        catch(FormatException)
        {/*user entered value that cant be parsed*/ }
        catch(ArgumentException)
        {/*_myTextBox.Text was null or cell's FormattedValueType is not string*/}
    }
}