【问题标题】:how to edit datagridview cells using a secondary textbox?如何使用辅助文本框编辑 datagridview 单元格?
【发布时间】:2015-12-03 08:52:50
【问题描述】:

我有 form1datagridview(2 列,1 列 id,1 列文本)。

我也有"edit button"。当我点击"edit button" 时,文本列将显示在form2 的文本框中。

form2 我有“select button”来编辑路径和"save button"

如何通过按"save button"form2 中的编辑文本传递到form1 中的datagridview 列。

form1中的代码编辑按钮(dgv_sourcefolder是datagridview1):

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;
            Form2 form = new Form2();
            form.Show();
            Hide();
        }
    }

form2 中的代码选择按钮:

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;
        }

    }

【问题讨论】:

    标签: c# winforms datagridview


    【解决方案1】:

    你的表单应该是这样的。

    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;
            }
        }
    

    主要形式

    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();
            }
        }
    

    【讨论】:

    • 谢谢,我试过了,但弹出的窗口显示“对象引用未设置为对象的实例。”在保存按钮。我会尝试修复:)
    • 确保这个“var row = ((DataGridView)sender).Rows[i];”不应为空对象。
    【解决方案2】:

    不假设数据网格中的数据,您可以利用 DataGridViewCell 的 ParseFormattedValue 方法和 FormatedValue 属性使 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*/}
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-15
      • 1970-01-01
      相关资源
      最近更新 更多