pekira pekira - 3 years ago 72
C# Question

How to add a CellDoubleClick handler to a grid Automatically generated

I am creating multiple pages to tab control

foreach (Menus Menu in AllMenus)
{
TabPage ToAdd = new TabPage();
_ProductsTabs.Controls.Add(ToAdd);
ToAdd.Text = Menu.Menu;
ToAdd.BackColor = Color.FromArgb(39,39,39);
}


for each Tab i create next i add a grid and fill it with data from a SQL table

#region AddGrids
foreach (TabPage _Tab in _ProductsTabs.TabPages)
{
DataGridView ToAdd = new DataGridView();
ToAdd.Size = new Size(1390, 584);
ToAdd.Dock = DockStyle.Fill;
ToAdd.BackgroundColor = Color.FromArgb(39, 39, 39);
ToAdd.DefaultCellStyle.BackColor = Color.FromArgb(41, 41, 41);
ToAdd.AlternatingRowsDefaultCellStyle.BackColor = Color.FromArgb(49,49,49);
ToAdd.GridColor = Color.FromArgb(49, 49, 49);
ToAdd.ForeColor = Color.White;
ToAdd.RowHeadersVisible = false;
ToAdd.AllowUserToResizeRows = false;
ToAdd.AllowUserToAddRows = false;
ToAdd.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
DataGridViewTextBoxColumn _Menu = new DataGridViewTextBoxColumn();
_Menu.HeaderText = "Menu";
_Menu.Name = "_MenuCol";
_Menu.ReadOnly = true;
DataGridViewTextBoxColumn _Name = new DataGridViewTextBoxColumn();
_Name.HeaderText = "Name";
_Name.Name = "_NameCol";
_Name.ReadOnly = true;
DataGridViewTextBoxColumn _Desc = new DataGridViewTextBoxColumn();
_Desc.HeaderText = "Description";
_Desc.Name = "_DescCol";
_Desc.Width = 250;
_Desc.ReadOnly = true;
DataGridViewTextBoxColumn _Money = new DataGridViewTextBoxColumn();
_Money.HeaderText = "Money";
_Money.Name = "_MoneyCol";
_Money.ReadOnly = true;
_Tab.Controls.Add(ToAdd);
ToAdd.Columns.AddRange(new DataGridViewColumn[] { _Menu, _Name, _Desc , _Money });
#region SqlGetEverthing

#region SqlGetAllSubMenus

string CurrSubMenu2 = "FindID";
string Menu = "FindMenu";
IEnumerable<SubMenus> results2;
IEnumerable<Menus> CurMenu;
Menus CurrMenu = new Menus();
using (SqlConnection Con = new SqlConnection("data source=VBSS012;initial catalog=WinTest_Beta;integrated security=True"))
{
Con.Open();
CurMenu = Con.Query<Menus>(Menu, new { Menu = _Tab.Text }, commandType: CommandType.StoredProcedure);
Con.Close();
}
foreach (Menus _menu in CurMenu)
{
CurrMenu = _menu;
}
using (SqlConnection Con = new SqlConnection("data source=VBSS012;initial catalog=WinTest_Beta;integrated security=True"))
{
Con.Open();
results2 = Con.Query<SubMenus>(CurrSubMenu2, new { SubMenu = CurrMenu.id }, commandType: CommandType.StoredProcedure);
CurrSubMenu = results2;
Con.Close();
}

#endregion

#region FillGrid

foreach (SubMenus _SubMenu in CurrSubMenu)
{
if (CurrMenu.id == _SubMenu.MenuID)
{
ToAdd.Rows.Add(_SubMenu.Menu, _SubMenu.Name, _SubMenu.Descripton, _SubMenu.Price);
}
}

ToAdd.Rows.Add("New", "New", "New", "New");

#endregion

#endregion
}


Now i need to create a CellDoubleClick Handler for every grid that is created.
How i do that?

Answer Source

Like any other event handler:

ToAdd.CellDoubleClick += ToAdd_CellDoubleClick;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download