DataGridViewRowEnter事件动态创建TabPage,Gridview NullReferenceException未处理

时间:2013-11-24 09:17:03

标签: c# winforms gridview textbox tabpage

以下是我在下面的代码

中使用的变量
        int k = 0;
        int k1 = 0;
        int kl = 0;
        TabPage[] tab = new TabPage[10];
        DataGridView[] rtb = new DataGridView[10];

以下是基于Tabpage输入

创建的GridView TextBox1
if (e.KeyCode == Keys.Enter)
{
                    k = 0;
                    //s = 0;
                    k = Convert.ToInt32(textBox1.Text);
                    k1 = k - tabControl2.TabPages.Count;

                    for (int i = 0; i < k1; i++)
                    {
                        tab[i] = new TabPage();

                        //tab[i] = new TabPage();
                        rtb[i] = new DataGridView();
                        //Start Gridview
                        rtb[i] = new System.Windows.Forms.DataGridView();
                        rtb[i].Location = new System.Drawing.Point(0, 50);
                        rtb[i].Size = new System.Drawing.Size(1020, 150);
                        //ID Column
                        rtb[i].Columns.Add("tr_id", "ID");
                        rtb[i].Columns["tr_id"].ReadOnly = true;
                        rtb[i].Columns["tr_id"].Width = 1;
                        //rtb[i].Columns["tr_id"].ReadOnly = true;

                        rtb[i].Columns.Add("Counter", "SNo");
                        rtb[i].Columns["Counter"].ReadOnly = true;

                        rtb[i].Columns["tr_id"].ReadOnly = true;
                        rtb[i].Columns["tr_id"].Width = 10;

                        // Color Column
                        rtb[i].Columns.Add("color_name", "Color Name");
                        rtb[i].Columns["color_name"].ReadOnly = true;
                        rtb[i].Columns.Add("color_no", "Pantone No.");
                        rtb[i].Columns["color_no"].ReadOnly = true;

                        //End Gridview

                        tab[i].Controls.Add(rtb[i]);
                        tab[i].Location = new System.Drawing.Point(4, 22);
                        tab[i].Name = "tab" + i.ToString();
                        tab[i].Padding = new System.Windows.Forms.Padding(3);
                        tab[i].Size = new System.Drawing.Size(400, 242);
                        tab[i].Text = "Article" + i.ToString();
                        tab[i].UseVisualStyleBackColor = true;
                        tabControl2.TabPages.Add(tab[i]);

                        //MessageBox.Show(""+tab[i].Text);

                        rtb[i].RowEnter += new System.Windows.Forms.DataGridViewCellEventHandler(dataGridView_RowEnter);      
}

现在这是我的GridviewRowEnter事件

private void dataGridView_RowEnter(object sender, DataGridViewCellEventArgs e)
{
            try
            {
                kl = 0;
                kl = Convert.ToInt32(tabControl2.TabPages.Count);

                for (int i = 0; i < kl; i++)
                {
                    //var rtb = new DataGridView();
                    for (int j = 0; j < rtb[i].Rows.Count; j++)
                    {
                        rtb[i].Rows[j].Cells[0].Value = j + 1;
                    }
                }
                kl = 0;
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
}

我将一些数据输入TextBox 2并按 Enter ,然后2 TabPagesGridViews已创建且DataGridViewRowEnter事件正常工作。< / p>

之后我在Gridview上输入了一些数据,它会在Column1上生成一个序列号,但当我还需要一个TabPageGridView时,我会去在TextBox和第2版以及第3类和Enter之后,它会创建一个而不会丢失数据,但这次我的事件显示Exception显示在此处:

enter image description here

  

也点击这里   http://s21.postimg.org/u8dlj7itz/Untitled.jpg

0 个答案:

没有答案