13 May 2009

Clear the initial Selection on the DataGridView in Windows App

I have been battling with disabling the selection of the first row in a Windows App datagridview and I tried so many solutions including having code in event handlers to solve the problem.
For example, I had
<DataGridViewName>_Selection changed where I had something like

if (SelectionAllowed)
{
if (<DataGridViewName>.CurrentRow != null)
{
}
}

SelectionAllowed is defined as a Boolean variable which I was set to true on a button click or when populating the DataGridView. This worked but had some issues.

Now I came up with a solution which, I think, would work for all property settings of the DataGridView. Mine has the following properties
MultiSelect = false
SelectionMode = FullRowSelect

What you need is
<DataGridViewName>_CellClick event handler
<DataGridViewName>_VisibleChanged event handler

In VisibleChanged handler
<DataGridViewName>.ClearSelection();

In _CellClick handler

if (
<DataGridViewName>.CurrentRow != null)
{
// Whatever you wanted to do in here
}

That's it... nothing more.

Note: I have only tested this in my situation. It might not work in yours.

PS,
Musa

24 February 2009

Validating using asp.net validation in custom javascript

Wow,
I just found how easy it is to have custom javascript at the same time using ASP.Net validators to validate user input (Client Side).
In my situation, the ASP.Net validators were not firing because I had hooked custom javascript to a button click.
e.g.
function ValidateAddressAddition()
{
if (confirm('Are you sure you wish to add a previous address?'))
{
return true;
}
return false;
}

The above code will block ASP.Net validation but if I add lines in Red, it would work fine
function ValidateAddressAddition()
{
if (Page_ClientValidate())
{
if (confirm('Are you sure you wish to add a previous address?'))
{
return true;
}
}
return false;
}