简体   繁体   English

C#WinForms应用程序:DataGrid和SelectAll

[英]C# WinForms application: DataGrid and SelectAll

C#, .net 3.5 WinForm application C#、. net 3.5 WinForm应用程序

I do have a DataGrid in a modal popup (another) form. 我确实有模式弹出式窗口(另一个)形式的DataGrid。 I do add objects to the datagrid and want them being selected when the form shows. 我确实将对象添加到数据网格,并希望在显示表单时选择它们。 Sounds easy, because there is a method "SelectAll". 听起来很简单,因为这里有一个方法“ SelectAll”。

However, this method only seems to work when the datagrid is already visible. 但是,该方法似乎仅在数据网格已经可见时才起作用。 My order is: 1) adding the objects 2) calling select all 3) display the modal form. 我的命令是:1)添加对象2)调用全选3)显示模式形式。 But when it shows, the objects are displayed but not selected. 但是,当显示时,将显示但未选择对象。

DialogResult r = myDialogForm.ShowModalDialog(); // objects are added, and SelectAll was called

Has someone an idea how I could accomplish the SelectAll - even when the DataGrid is not yet Visible. 有人知道我如何才能完成SelectAll-即使DataGrid尚不可见。

-- about HPT's comment (changing the order) -关于HPT的评论(更改顺序)

When I call the modal form ( System.Windows.Forms.ShowDialog ) I do not have the chance [1] to call SelectAll after(!) the modal form is displayed - this exactly is the problem. 当我调用模式形式( System.Windows.Forms.ShowDialog )时,我没有机会[1]调用SelectAll after(!),显示了模式形式-这正是问题所在。

Next time "my code" is reached is when the DialogResult is passed back. 下次到达“我的代码”是将DialogResult返回。 The Visibility is implicitly set to true by the underlying methods (of the .NET framework Forms.ShowDialog ). (.NET框架Forms.ShowDialog )基础方法将Visibility隐式设置为true。

[1] A possible work around is to have an event when the form becomes visible and then to SelectAll . [1]可能的解决方法是在表单可见时发生一个事件,然后选择SelectAll If I do not find something better I'll try this. 如果找不到更好的东西,我会尝试的。

you should change your scenario! 你应该改变你的情况!

  1. adding the objects 添加对象
  2. set dgv visibility to false 将dgv可见性设置为false
  3. display the modal form 显示模态形式
  4. calling the dgv.SelectAll() 调用dgv.SelectAll()
  5. set dgv visibility to true 将dgv可见性设置为true

EDITED 已编辑

you can just handle shown event of form. 您可以只处理显示的表单事件。 the scenario would be: 该方案将是:

DialogForm myDF = new DialogForm();
//here you add data to your dgv in myDF
myDF.ShowDialog();

set the dgv.Visible to false , you can handle the Shown event in DialogForm Cunstructor after InitializeComponent(); dgv.Visible设置为false ,您可以在InitializeComponent();之后处理DialogForm Cunstructor中的Shown事件InitializeComponent(); like this.Shown += new EventHandler(DialogForm_Shown); 像这样this.Shown += new EventHandler(DialogForm_Shown); and then 接着

    void DialogForm_Shown(object sender, EventArgs e)
    {
       dgv.SelectAll();
       dgv.Show();
    }

Have you tried adding the SelectAll to the Activated() event... However, I would create a form variable Boolean to identify if the SelectAll was already handled so it doesn't do every time you may Alt-Tab to another application and back (yeah I know, some people do that in between dialog prompts) and would otherwise re-selectall again. 您是否尝试过将SelectAll添加到Activated()事件中...但是,我将创建一个表单变量布尔值以标识SelectAll是否已被处理,因此每次您Alt-Tab切换到另一个应用程序并返回时它都不会执行(是的,我知道,有些人在对话框提示之间执行此操作),否则将再次重新选择全部。

bool WasSelectAllProcessed = false;  && at the form level

Then, in the Activated event when all is visible for the SelectAll to have an impact 然后,在“已激活”事件中,当全都可见时,SelectAll将产生影响

if ( ! WasSelectAllProcessed )
   dgv.SelectAll();

I have tested it: 我已经测试过了:

SelectAll is ignored when the grid is invisible. 当网格不可见时,将忽略SelectAll The solution is to register an event when the grid becomes visible (eg VisibilityChanged ) and then to call SelectAll . 解决方案是在网格变得可见时(例如VisibilityChanged )注册一个事件,然后调用SelectAll

Of course, a flag whether this is completed will help to avoid redundant calls. 当然,标记是否完成将有助于避免重复调用。 Also it is a good approach to call SelectAll directly when the grid is already visible. 同样,当网格已经可见时,直接调用SelectAll是一个很好的方法。

Very much the same approach as HPT's and DRapp's suggestion. 与HPT和DRapp的建议几乎相同。 Thanks for helping me on that. 多谢您的协助。

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

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