关于DataGird和用户控制 [英] About DataGird and User-Control

查看:67
本文介绍了关于DataGird和用户控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我有一个使用C#创建的窗口窗体应用程序,还创建了一个具有不同控件的UserControl窗体,我也有一个主窗体,其中包含从数据库中获取值的datagrid,我有一个选定的单元格事件,它会在每次单击每行时打开我的Usercontrol表单,我希望能够获取所选行的值并将其传递到自动创建的UserControl上,因此UserControl表单中的其他文本框可以具有我的主要表格中Datagrid的值.这可能吗 ??请提供示例代码.谢谢

Hi guys, I have a window form application which i created with C# in it i created a UserControl Form with different control, i also have a main form which contain datagrid with values gotten from my database, i have a selected cell event, that opens my Usercontrol form any time each row is click, i want to be able to get the value of the selected row and pass it onto my UserControl that is automatically created, so the the different text box in my Usercontrol form can have the values of my Datagrid in my main form. Is this possible ?? please includes example codes. Thanks

推荐答案

void SelectedCellHandler(object sender, EventArgs e){
 object value = ((DataGrid)sender).SelectedCell.Value;
 Control newControl = new MyMagicUserControl(value);
 // do whatever you already do with the control
}



然后在用户控件中,将该值作为构造函数参数,并对其进行任何所需的操作,例如



and in the user control, accept the value as a constructor parameter and do whatever you want with it, e.g.

class MyMagicUserControl: UserControl {
 public MyMagicUserControl(object value){
  // things that are already here e.g. InitializeComponent
  someTextBox.Text = value.ToString();
 }
}


没有人会给"您代码,但这是您需要执行的步骤.

1.为网格的selectedindex更改事件创建事件处理程序.

2.创建一个用户控件并创建对其的访问.基本上可以通过以下两种方式完成此操作:
2.a.创建可以在用户控件上设置和/或获取控件属性的公共属性.
2.b.创建具有对象的set/get属性的对象类,然后通过用户控件中的公共属性或构造函数将该对象完全传递给用户控件.
2.c.的任何变化.

3.对于selectedindexchanged事件中触发的每个事件,您需要:
3.a 从表单中删除当前用户控件
3.b 创建新的用户控件并传递信息,如2
中所述 3.c.将新创建的用户控件添加到表单中.
->或者,您可以保留用户控件并仅更改其数据.


希望这会有所帮助.
No one will ''give'' you code, but here are the steps you need to go through.

1. Create an event handler for the selectedindex changed event of your grid.

2. Create a user control and create an access to it. This basically can be done in a couple of ways:
2.a. Create public properties that can set and/or get the properties of the controls on the user control.
2.b. Create an object class with set/get properties of the object and pass that object entirely to the user control via a public property in your user control, or via the constructor.
2.c. Any variation of this.

3. For each event fired in the selectedindexchanged event you need to:
3.a remove the current user control from the form
3.b create the new user control and pass the information like described in 2
3.c. Add the newly created user control to the form.
--> Alternatively you can keep the user control and just change its data.


Hope this helps.


这篇关于关于DataGird和用户控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆