private void dgCorrectionHistory_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("You have just selected " + DataGridTextColumn.HeaderStringFormatProperty.ToString() + row);
}
RowDetailsVisibilityMode="VisibleWhenSelected" SelectedIndex="0" Grid.IsSharedSizeScope="False" HorizontalAlignment="Stretch" IsTabStop="True" TabIndex="1"
CanUserAddRows="False" CanUserResizeColumns="False" CanUserResizeRows="False" IsTextSearchEnabled="True"
Loaded="dgCorrectionHistory_Loaded" EnableRowVirtualization="False" Focusable="False" PreviewKeyDown="dgCorrectionHistory_PreviewKeyDown" PreparingCellForEdit="dgCorrectionHistory_PreparingCellForEdit"
CellEditEnding="dgCorrectionHistory_CellEditEnding" BeginningEdit="dgCorrectionHistory_BeginningEdit" MouseDown="dgCorrectionHistory_MouseDown" LostFocus="dgCorrectionHistory_LostFocus" MouseLeave="dgCorrectionHistory_MouseLeave"
MouseDoubleClick="dgCorrectionHistory_MouseDoubleClick" PreviewMouseDown="dgCorrectionHistory_PreviewMouseDown">
Suchit KhannaPosted May 23, 2009, 2:29 AM
Manu, do you want to get the row click event in your datagrid??
Just add MouseDoubleClick Event to your DataGrid and the following code to the event:
{
DependencyObject source = (DependencyObject)e.OriginalSource;
var row = UIHelper.TryFindParent
if (row == null) //Checking if the double click happened froma row or not
return;
else
// you could access row variable as:
row.Item --> give you the selected row
e.Handled = true;
}
// Add this Class to your project
public static class UIHelper
{
///
/// Finds a parent of a given item on the visual tree.
///
///
/// A direct or indirect child of the
/// queried item.
///
/// type parameter. If not matching item can be found, a null
/// reference is being returned.
public static T TryFindParent
where T : DependencyObject
{
//get parent item
DependencyObject parentObject = GetParentObject(child);
//we've reached the end of the tree
if (parentObject == null) return null;
//check if the parent matches the type we're looking for
T parent = parentObject as T;
if (parent != null)
{
return parent;
}
else
{
//use recursion to proceed with next level
return TryFindParent
}
}
///
/// This method is an alternative to WPF's
///
/// supports content elements. Do note, that for content element,
/// this method falls back to the logical tree of the element!
///
/// The item to be processed.
///
/// null.
public static DependencyObject GetParentObject(DependencyObject child)
{
if (child == null) return null;
ContentElement contentElement = child as ContentElement;
if (contentElement != null)
{
DependencyObject parent = ContentOperations.GetParent(contentElement);
if (parent != null) return parent;
FrameworkContentElement fce = contentElement as FrameworkContentElement;
return fce != null ? fce.Parent : null;
}
//if it's not a ContentElement, rely on VisualTreeHelper
return VisualTreeHelper.GetParent(child);
}
}
Niradhip ChakrabortyPosted Apr 24, 2009, 1:57 AM
My Datagrid html code:
<asp:DataGrid ID="dgViewMyReservation" Width="580px" AutoGenerateColumns="False"
BorderStyle="none" OnItemDataBound="dgViewMyReservation_ItemDataBound" DataKeyField="ReservationId"
BorderColor="gray" runat="server" ShowHeader="false" AlternatingItemStyle-BorderWidth="0">
<ItemStyle CssClass="DetailsGrid">ItemStyle>
<Columns>
<asp:BoundColumn DataField="ReservationId">
<ItemStyle Width="150px" CssClass="text3">ItemStyle>
asp:BoundColumn>
<asp:BoundColumn DataField="EventName">
<ItemStyle Width="270px" CssClass="text3">ItemStyle>
asp:BoundColumn>
<asp:BoundColumn DataField="Type">
<ItemStyle Width="120px" CssClass="text3">ItemStyle>
asp:BoundColumn>
<asp:BoundColumn DataField="Status">
<ItemStyle Width="110px" CssClass="text3">ItemStyle>
asp:BoundColumn>
Columns>
asp:DataGrid>
My Item bound code in page behind is as follows:
public void dgViewMyReservation_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
string strMemberId = e.Item.Cells[0].Text;
string strMarketName = e.Item.Cells[2].Text;
string strStatus = e.Item.Cells[3].Text;
e.Item.Attributes.Add("onmouseover", "OnMenuMouseOver(this)");
e.Item.Attributes.Add("ondblclick", "javascript:return SelectRowOndblClick('" + strMemberId + "','" + strMarketName + "','" + strStatus + "', this);");
}
}
My java script function is as follows:
function SelectRowOndblClick(MemberId, ObjRow)
{
document.frmMyReservation.hdID.value = "";
document.frmMyReservation.hdID.value = MemberId;
location.href = "EditReservation.aspx?Id=" + document.getElementById("hdID").value + "&Status=" + document.getElementById("hdStatus").value + "&Type=" + document.getElementById("hdType").value + "";
}