将datagridview项发送到另一个表单

时间:2014-08-15 08:31:00

标签: c# datagridview

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

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

谢谢你们的帮助

1 个答案:

答案 0 :(得分:0)

DataGridViewCellEventArgs有两个重要的参数: e.rowIndex,e.columnIndex,用于指定您按下的单元格。

顺便说一下,你试图从单元格解析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);
}

我认为它应该对你有所帮助,如果是,请标记为已回答。