Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
When I Select a row in DataGrid for first time SelectedItem gets the value. Again if i select the same row in dataGrid then the selectedItem became null. Please give some idea...

SQL
<DataGrid Name="dgDbConnection"ItemsSource="{Binding ObjDBConnectionObsvList}" SelectedItem="{Binding ObjDBConnectionSelectedDatam,Mode=TwoWay}"/
>
Posted
Updated 5-Feb-13 20:32pm
v4

1 solution

Hi,

this is the default behavior of the DataGrid, you can select and unselect a row in a DataGrid.

To prevent this, listen to the DataGrid.SelectionChanged event.

In the event handler you could type e.g.:

C#
private void SelectionChanged(object sender, SelectionChangedEventArgs e)
{
   DataGrid g = sender as DataGrid;

   // checks if no rows where added (to the selected rows)
   // but at least 1 row removed from the selected rows of the grid
   if (g != null && e.AddedItems.Count == 0 && e.RemovedItems.Count > 0)
   {
      // this selects the first row in the e.RemovedItems collection
      g.SelectedItem = e.RemovedItems[0];
   }
}


This should prevent the deselection of the row.

Hope this helps.

Regards,

Thomas.
 
Share this answer
 
Comments
suganyass 6-Feb-13 0:07am    
Hi Thomas... Thank u for ur reply... but it doesn't work. selection changed event not fired after selecting the column twice.
Thomas Duwe 6-Feb-13 3:28am    
This is for row selection, not column....as you asked in your original question.
suganyass 6-Feb-13 3:36am    
sorry its row only...
Thomas Duwe 6-Feb-13 3:44am    
Ah sorry, how do you select the row twice? Only with left-mouse-click?
If so, then the deselection of the row shouldn't take place, it says selected, thats the default behavior of the DataGrid.
If this isn't the behavior you see, then you must have some code somewhere changing the selected row of the datagrid.
suganyass 6-Feb-13 4:08am    
In first click i got the value in Selected Item and again click the same row that selected Item became null. Did you understand my problem....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900