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.
CodePudding user response:
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
}
CodePudding user response:
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;
}