0

My really first question here. I come from an electrical engineering background and I have always use C to write program for microcontrollers so I am pretty new to VB net. Here my doubt :

I have different tab (TabControl1, ...2, ..3, etc) in a form and every tab page contains a datagridview (DataGridView1, .... ). and 1 button in common in the form.

How can I populate by pressing the button, the right datagridview base on the selectecTab?

I would do something like this:

selectedTab = TabControl1.SelectedIndex
if selectedTab = 0 then populate DataGridview1
else if selectedTab = 1 then populate DataGridview2
else if selectedTab = 2 then populate DataGridview3

....

But this code is limited because if in the future I need to add more Tab then I have to add more line of code.

Can you show me a smarter way to do it?

thanks a lot

  • Nihilo
Luis Quijada
  • 2,345
  • 1
  • 26
  • 31
Nihilo
  • 41
  • 1
  • 5
  • Build dynamically the datagrid's name using the fixed part of the name and the index and locate the control dynamically (http://stackoverflow.com/questions/3898588/find-control-by-name-from-windows-forms-controls). – Luis Quijada Jan 15 '15 at 11:12
  • 1
    Thank you Luis for the quick answer, also for the useful link. – Nihilo Jan 15 '15 at 12:24
  • At some point I have the following code:DataGridView1.Columns.Remove(DataGridView1.Columns(0)). How can I have the same line of code but with DataGridViewX where x is base on tab Index? – Nihilo Jan 15 '15 at 13:15
  • 1
    Thank you I solved it. I created an array of DataGridView control object and simply used it as following. dataGridMyArray(selectedTab).Columns.Remove(...) – Nihilo Jan 15 '15 at 13:39

0 Answers0