0

So i successfully dynamiclly add button on DataGridView (DataGridViewButtonColumn) based on tables count in my database with :

//'cmd' type is MySqlCommand and 'cnnct' is MySqlConnection
//this is inside the Main()
string cmdString = "SHOW TABLES";
cmd.CommandText = cmdString;
cmd.Connection = cnnct;
Reader = cmd.ExecuteReader();
while (Reader.Read())
       {
           classSelect.Rows.Add(Reader.GetString(0));
       }
Reader.Close();

then i've prepared a method that will be called when the button is clicked.

protected internal void FormCaller (object sender, EventArgs e)
    {
        //There's should be unique ID for the 'sender', but i don't know how
        if(aioForm == null)
        {
            //'AIO_Form' is a Form
            splash.ShowSplashScreen();
            aioForm = new AIO_Form(this);
            splash.CloseForm();
            this.Visible = false;
            aioForm.Visible = true;
        }
        //and some other code, handling if aioForm isn't null
    }

The problem is, how i can add an EventHandler to the dynamiclly generated button, and it is based on a remote database ?

i've read from Here and Here (both are from StackOverflow) but no help.

Thanks in advance everybody.

Community
  • 1
  • 1
Tommy Aria Pradana
  • 574
  • 1
  • 4
  • 18

1 Answers1

0

After days do this and that, i can provide a workaround. Despite add the EventHandler to the Cell i add it to the DataGrid itself, then the Method which Handle the event should identify the cell that raise the event

add the EventHandler

while (Reader.Read())
{
    var Name = UppercaseWords(Reader.GetString(0));
    kelasSelect.Rows.Add(Name);
}
Reader.Close();
classSelect.CellMouseUp += FormCaller;

Then FormCaller which handle the event

protected internal void FormCaller(object sender, EventArgs e)
{
   DataGridView Sender = (DataGridView)sender;
   string FormName = Sender.CurrentCell.Value.ToString(); //This is how i identify the cell who raise the event
   NForm = new myForm(this);
   NForm.Text = FormName;
}
Tommy Aria Pradana
  • 574
  • 1
  • 4
  • 18