以下是我在下面的代码
中使用的变量 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 TabPages
和GridViews
已创建且DataGridViewRowEnter
事件正常工作。< / p>
之后我在Gridview
上输入了一些数据,它会在Column1
上生成一个序列号,但当我还需要一个TabPage
和GridView
时,我会去在TextBox
和第2版以及第3类和Enter
之后,它会创建一个而不会丢失数据,但这次我的事件显示Exception
显示在此处: