0

I've created a UserControl with a DataGridView. In my code I add this UserControl to a TabControl-TabPage on a Form. This works well.

Now I want to do a double click on the DataGridView of the UserControl, select the row as an object and use this data on another TabPage which is on the initial Form.

How can I get access to the UserControl event an my Form?

I tried it that way(first answer) How to use UserControl but I can't use it on my Form. So this is not working.

And how to do a DataGridViewCellEvent to use?

EDIT: I tried the following way now: UserControl:

 public partial class ucData : UserControl
    {
        public ucData(string Number)
        {
            InitializeComponent();
public string Data
        {
            get { return textBox1.Text; }
            set { textBox1.Text = value; }
        }
        public event EventHandler DataAvailable;
        /// <summary>
        /// Called to signal to subscribers that new data is available
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnDataAvailable(EventArgs e)
        {
            EventHandler eh = DataAvailable;
            if (eh != null)
            {
                eh(this, e);
            }
        }
....

...
private void dataGridView_CellDoubleClick(object sender, EventArgs e)
        {
            OnDataAvailable(null);
        }

And on my Main Form:

...
 void Test()
        {
            UserControls.ucData uc = new ucData(null);
            uc.DataAvailable += new EventHandler(child_DataAvailable);
            uc.Data = "Hello!";
        }
        void child_DataAvailable(object sender, EventArgs e)
        {
            UserControls.ucData child = sender as UserControls.ucData;
            if (child != null)
            {
                MessageBox.Show(child.Data);
            }
        }
...

But if I do a doubleClick on the UserControl no MessageBox will appear. Why? What is wrong? Can anyone help please.

Jocelyn
  • 133
  • 1
  • 1
  • 10

2 Answers2

0

Try this:

UserControl

public partial class UserControl1: UserControl
{
    public event DataGridViewCellEventHandler dataGridView1_CellDoubleClickEvent;

    public UserControl1()
    {
        InitializeComponent();
    }

    private void UserControl1_Load(object sender, EventArgs e)
    {
        this.dataGridView1.CellDoubleClick += dataGridView1_CellDoubleClickEvent;
    }

    private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
        dataGridView1_CellDoubleClickEvent?.Invoke(this.dataGridView1, e);
    }
}

Form

private void Form1_Load(object sender, EventArgs e)
    {
        userControl11.dataGridView1_CellDoubleClickEvent += new DataGridViewCellEventHandler(dataGridView1_CellDoubleClick);
     }        

    private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
        //Your code for the "dataGridView_CellDoubleClick" event
    }
  • ```private void Form1_Load(object sender, EventArgs e) { userControl11.dataGridView1_CellDoubleClickEvent += new DataGridViewCellEventHandler(dataGridView1_CellDoubleClick); } ``` On that line I receive an error: CS0120: An object reference is required for the nonstatic field, method, or property 'foo' – Jocelyn Sep 30 '22 at 18:38
  • I add ```userControl1 uc = new userControl1();```before I call the doubleClick event in the Form_Load Event. But nothing is happening. I try to show a MessageBox by doubleclicking the dataGridView. – Jocelyn Sep 30 '22 at 18:52
  • @Jocelyn, I added the control to the form, it is my grid. That is, the control is created in the screen designer. It is a control added through the toolbox,, is yours different from this? – Anderson Constantino Sep 30 '22 at 20:05
  • I add the UserControl only with code at runtime. – Jocelyn Sep 30 '22 at 20:14
  • Now I changed it an add the UserControl with the designer on my form. But the doubleClick event doesn't work. I want to show a message box. – Jocelyn Oct 01 '22 at 19:06
  • @Jocelyn, Confirm that within your control you are invoking the event within the grid event itself (**dataGridView1_CellDoubleClick**) that you want to capture. I also advise you to isolate the test, make a separate control with exactly the same code that I sent you and test it separately and then evaluate which part of the code was different in your real implementation. I assure you that the example I created is working perfectly. I uploaded the code on github to help: https://github.com/andesbh/WindowsFormsControlLibrary1 – Anderson Constantino Oct 03 '22 at 10:38
  • thanks but I still have trouble. 1.) Is it necessary, that I have two namespaces? I have the UserControl and the Form in the same. But I think my problem is on another part. In the meantime I can add the UserControl to the form ONCE but when I will go to the designer again I get an error about my connection. I make an answer to show the code of the connection and the repository. – Jocelyn Oct 04 '22 at 12:07
  • A class has a path that contains namespaces like System.Net.Socket where Socket is the class. So you can use the full path (including namespaces) to reference a class or put the entire code into one namespace. – jdweng Oct 04 '22 at 13:17
  • @AndersonConstantino Thanks for your help. Some more questions: Do I have to do the DataBinding of the dataGridView in the Form and not in the UserControl? And for the CellDoubleClickEvent => how can I receive data of the selected row? – Jocelyn Oct 04 '22 at 19:51
  • @Jocelyn, I believe that in the most common situation data binding should be done on the form, maybe some more generic handling of the data should be done on the control. The use of the control is for you to be able to somehow customize some component and reuse it. To retrieve the selected item, you can use this code, following the example I sent in the answer: **private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { var selectedData = ((DataGridView)sender).Rows[e.RowIndex].Cells[e.ColumnIndex].Value; }** – Anderson Constantino Oct 04 '22 at 20:24
0
public class CustomerRepository : ICustomerRepository
{
    public IEnumerable<Customer> GetAllCustomer()
    {
        using (IDbConnection db = new MySqlConnection(AppConnection.ConnectionString))
        {
            string q = @"SELECT ....";
            return db.Query<Customer>(q, commandType: CommandType.Text);
        }
    }
}

I use the repository in my UserControl to show all data.

The error message: System.NullReferenceException: Object reference not set to an instance of an object. ...AppConnection..cctor()

Here my AppConnection:

public static class AppConnection
{
    public static string ConnectionString = ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
}
Kevy Granero
  • 643
  • 5
  • 17
Jocelyn
  • 133
  • 1
  • 1
  • 10