发送datagridview项目到另一个表单

本文关键字:另一个 表单 项目 datagridview 发送 | 更新日期: 2023-09-27 18:18:11

嘿,我写了下面的代码发送我的datagridview值选定行到另一个表单,但我得到了这个错误,我的事件是双击内容,我不知道为什么会发生

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
using System.Windows.Forms;
namespace WindowsFormsApplication12
{
     public partial class Form5 : Form
    {
         public Form5()
        {
            InitializeComponent();
        }
        private void Form5_Load(object sender, EventArgs e)
        {
            tblClassTableAdapter.Fill(dataSet1.tblClass);
        }
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
    }
    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        this.tblClassTableAdapter.FillBy1(this.dataSet1.tblClass, textBox1.Text);

    }
    private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
        new Form6(int.Parse(dataGridView1.SelectedRows[0].Cells[0].Value.ToString())).Show();
    }
}

和我的表格6

    using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication12
{
    public partial class Form6 : Form
    {
        int classid;
        private string p;
        public Form6(int myid)
        {
            classid = myid;
            InitializeComponent();
        }

        public Form6(string p)
        {
            // TODO: Complete member initialization
            this.p = p;
        }
        public void Form6_Load(object sender, EventArgs e)
        {
            textBox1.Text = classid.ToString();
        }
        public DataGridViewRow dataGridViewRow { get; set; }
    }
}

谢谢大家的帮助

发送datagridview项目到另一个表单

DataGridViewCellEventArgs有两个重要的参数:e.rowIndex, e.columnIndex,指定你按的是哪个单元格

顺便说一下,你试图从cell中解析Int,用try/catch包围它,以防解析失败。

试试下面的代码:

try {
    if (e.ColumnIndex > -1 && e.RowIndex > -1)
        new Form6(int.Parse(dataGridView1[e.ColumnIndex,e.RowIndex].Value.ToString())).Show();
}
catch (Exception ex) {
    MessageBox.Show("Error: " + ex.Message);
}

我想它应该对你有帮助,如果是,标记为回答。