开发者

C# WinForms application: DataGrid and SelectAll

开发者 https://www.devze.com 2023-01-27 09:45 出处:网络
C#, .net 3.5 WinForm application I do have a DataGrid in a modal popup (another) form. I do add objects to the datagrid and want them being selected when the form shows. Sounds easy, because there is

C#, .net 3.5 WinForm application

I do have a DataGrid in a modal popup (another) form. I do add objects to the datagrid and want them being selected when the form shows. Sounds easy, because there is a method "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. 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.

-- about HPT's comment (changing the order)

When I call the modal form (System.Windows.Forms.ShowDialog) I do not have the chance [1] 开发者_如何学Goto call SelectAll after(!) the modal form is displayed - this exactly is the problem.

Next time "my code" is reached is when the DialogResult is passed back. The Visibility is implicitly set to true by the underlying methods (of the .NET framework Forms.ShowDialog).

[1] A possible work around is to have an event when the form becomes visible and then to SelectAll. If I do not find something better I'll try this.


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.

bool WasSelectAllProcessed = false;  && at the form level

Then, in the Activated event when all is visible for the SelectAll to have an impact

if ( ! WasSelectAllProcessed )
   dgv.SelectAll();


you should change your scenario!

  1. adding the objects
  2. set dgv visibility to false
  3. display the modal form
  4. calling the dgv.SelectAll()
  5. set dgv visibility to 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(); like this.Shown += new EventHandler(DialogForm_Shown); and then

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


I have tested it:

SelectAll is ignored when the grid is invisible. The solution is to register an event when the grid becomes visible (e.g. VisibilityChanged) and then to call 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.

Very much the same approach as HPT's and DRapp's suggestion. Thanks for helping me on that.

0

精彩评论

暂无评论...
验证码 换一张
取 消