简体   繁体   English

如何动态创建带有面板的数据网格?

[英]how create a datagrid with panel dynamically?

I'm french, so sorry for my bad english... But I hope that you will help me ;)我是法国人,很抱歉我的英语不好...但我希望你能帮助我;)

I'm developing a soft in c# with lot of information store in DB, like information about people.我正在用 C# 开发一个软件,在数据库中有很多信息存储,比如关于人的信息。

I want display these information with a beautiful UI, like that :我想用漂亮的 UI 显示这些信息,如下所示:

For that, I created :为此,我创建了:

  • a first panel in Visual Visual 中的第一个面板
  • a class for create sub panel dynamically用于动态创建子面板的类

I make a loop and for each person, I call a method "createPanel" with the information of each person, and these sub panels created are added to the parent panel.我做了一个循环,对于每个人,我用每个人的信息调用一个方法“createPanel”,并将这些创建的子面板添加到父面板中。 The ID of each person is store in the "tag" parameter of the sub panel.每个人的 ID 存储在子面板的“标签”参数中。

All work fine, but I don't really know how to add event in each sub panel.一切正常,但我真的不知道如何在每个子面板中添加事件。 As you can see in the link above, the user can click on a bookmark, or a icon for show a menu (the round with 3 point = icon "menu").正如您在上面的链接中看到的,用户可以单击书签或图标以显示菜单(带有 3 点的圆形 = 图标“菜单”)。

My question is how to add event for each sub panel dynamically ?我的问题是如何为每个子面板动态添加事件? For example I want display a menu for each sub panel when I click on the icon "menu", get the id store in the "tag" of the sub panel, and show a new form for manage information.例如,当我单击“菜单”图标时,我想为每个子面板显示一个菜单,在子面板的“标签”中获取 id 存储,并显示一个用于管理信息的新表单。

Thx for your help, I can add my code if necessary :)感谢您的帮助,如有必要,我可以添加我的代码:)

Make a own user control for the panels you want to display containing all the controls from the toolbox you need.为要显示的面板创建自己的用户控件,其中包含您需要的工具箱中的所有控件。

public partial class MyControl : UserControl
{
  ...
}

In "MyControl" you define events for all things that can happen and must be handled externally of the control.在“MyControl”中,您为所有可能发生且必须在控件外部处理的事件定义事件。 Same as eg "SelectedIndexChanged" for combobox.与组合框的“SelectedIndexChanged”相同。

Your control class would then look like this:您的控件类将如下所示:

public partial class MyControl : UserControl
{
   public event EventHandler<MyImportantEventA> OnImportantThingHappend;

   //Event-Invoker
   protected virtual void OnOnImportantThingHappend(MyImportantEventA e)
        {
            OnImportantThingHappend?.Invoke(this, e);
        }
}

//Data of your event. Customize as you like/need.
public class MyImportantEventA : EventArgs   
 {
            public string Message { get; set; }
 }

When you add your control dynamically, bind to the event(s) of your control:动态添加控件时,绑定到控件的事件:

myUserControl.OnImportantThingHappend += DoSomethingWithEvent;

Things the control can handle it self, don't need to be exposed as events.控件可以自行处理的事情,不需要作为事件公开。 Eg:例如:

"Close" may be something that must be handled externally. “关闭”可能是必须在外部处理的事情。 You need to remove the control, rearange you controls etc.您需要删除控件,重新排列控件等。

"Show Details" probably is something that can be handled completely inside of your control. “显示详细信息”可能是可以完全在您的控制范围内处理的内容。 Your control shows a message box or a fancy tooltip.您的控件会显示一个消息框或精美的工具提示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM