private void btnAddItem_Click(object sender, EventArgs e)
{
try
{
if (txtBoxnum.Text != "")
{
tbAddItem.TabPages.Clear();
int boxNum = Convert.ToInt32(txtBoxnum.Text.Trim());
for (int i = 0; i < boxNum; i++)
{
//------------Add tab Pages------------------------
TabPage dynamicTabPage = new TabPage();
dynamicTabPage.Name = "dynTabPage"+i;
dynamicTabPage.Text = "Box" + (i + 1);
dynamicTabPage.BackColor = Color.Gray;
DataGridView gd = new DataGridView();
gd.Name = "gdData";
gd.Width = 1530;
gd.BackColor = Color.Gray;
gd.AutoGenerateColumns = false;
//------------Add grid header columns--------------
gd.Columns.Add("slno", "SlNo");
gd.Columns.Add("item", "Item");
gd.Columns.Add("Qty", "Quantity");
gd.Columns.Add("Price", "Price");
gd.Columns[0].Width = 30;
gd.Columns[1].Width = 440;
gd.Columns[2].Width = 100;
gd.Columns[3].Width = 140;
dynamicTabPage.Controls.Add(gd);
this.tbAddItem.TabPages.Add(dynamicTabPage);
gd.EditMode = DataGridViewEditMode.EditOnKeystroke;
}
}
else
{
tbAddItem.TabPages.Clear();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
}
}
我的胜利表单有一个标签控件。在按钮点击事件(btnAddItem_Click
)dynamically
上添加标签页并将datagridview
添加到标签控件。 TabPages的数量取决于在textBox boxNum中输入的数字。每次动态添加的tabPages都会动态添加datagridview
。
我想在点击保存按钮上将每个datagridview
值保存到database
。
答案 0 :(得分:1)
private void btnSave_Click(object sender, EventArgs e)
{
foreach (Control tab in tbAddItem.Controls)
{
TabPage tabPage = (TabPage)tab;
DataGridView dgv = (DataGridView)tabPage.Controls[0];
//iterate through the DataGridView saving to the Database
dgv...
}
}