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!
- adding the objects
- set dgv visibility to false
- display the modal form
- calling the
dgv.SelectAll()
- 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.
精彩评论