如何从其他表单访问DataGridView?

时间:2015-11-04 17:58:45

标签: c#

我想从另一个表单访问DataGridView。我只想从DataGridView获取一个值,并将其显示在另一个表单的文本框中。

如图所示: 我单击编辑行按钮弹出Form2,所以当我单击button1时,textbox1应该具有row [0] cell [0]的值(即" 1")但是我得到此错误: 未处理的类型' System.ArgumentOutOfRangeException'发生在mscorlib.dll。

其他信息:指数超出范围。必须是非负数且小于集合的大小。

我有什么遗漏的吗?我使DataGridView Modifiers = Public但仍然没有工作。请帮忙谢谢。

Photo

3 个答案:

答案 0 :(得分:0)

$query2 = "INSERT INTO istoric SET id_user = '".$user."',id_equipment = '".$nr_inv."',start = '".$startdate."',end = '".$enddate."',comment = '".$comment."'"; $id = "INSERT INTO `istoric`(`condition`) SELECT `status` FROM `echipament`WHERE `nr_inventar` = '".$nr_inv."'"; 表示已选择的行。您应该使用SelectedRows属性。

答案 1 :(得分:0)

  1. Form2

    中创建属性

    public int Qty {get; set;}

  2. 2。
      private void Edit_Line_Click(object sender, EventArgs e)   
      {
         Form2 frm2 = new Form2();
            frm2.Qty=this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
         if(frm2.ShowDialog()==DialogResult.OK)
         {
            this.dataGridView1.SelectedRows[0].Cells[0].Value = frm2.textBox1.Text;
         }
      }
    

    这只是一个想法,你必须努力获得准确的输出。

答案 2 :(得分:0)

尝试适合我的工作。

享受... !!!

enter image description here

我选择了行包含Anjum文本 输出量 enter image description here

相同: enter image description here

代码表单1

using System;
using System.Windows.Forms;

namespace DataGridViewCellAcessing
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            dataGridView1.Rows.Add(new string[] {
                    "Anjum"});
            dataGridView1.Rows.Add(new string[] {
                    "Khuram"});
            dataGridView1.Rows.Add(new string[] {
                    "Shahzad"});
        }

       

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            Form2 form2 = new Form2();
            form2.Edit(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
   
            form2.Show();
        }
    }
}

代码表单2

using System;
using System.Windows.Forms;

namespace DataGridViewCellAcessing
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
          
        }
        public void Edit(string Values) {
           
                textBox1.Text = Values;

           
        }
    }
}