This is a migrated thread and some comments may be shown as answers.

Row click event to get row ID

3 Answers 759 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Je
Top achievements
Rank 1
Je asked on 11 Jan 2012, 05:10 AM
Hi, please help me on this. i've searched everywhere but seems like noting working and it's stressed me out

i have a gridview and need to enable row click event to get the ID of the users.

currently, im able to click at each cell and display the value belong to it. but it should only display the ID of the row even we click different cell (in the same row).

i dont want this function occur at headers. how to do this? and can it works if we dont display the ID in the gridview?

i disclose my codes below

please help me guys, it's very urgent...tq

xaml:
<telerik:RadGridView Grid.Row="1" x:Name="radGridView"
   telerik:Theming.Theme="Expression_Dark" 
   HorizontalAlignment="Stretch"
   VerticalAlignment="Stretch"
   AutoGenerateColumns="False"
   IsReadOnly="True"
   CanUserFreezeColumns="False"
   RowIndicatorVisibility="Collapsed"
   Margin="0,0,0,27" ShowGroupPanel="False">
<telerik:RadGridView.Columns>
   <telerik:GridViewToggleRowDetailsColumn />
   <telerik:GridViewDataColumn DataMemberBinding="{Binding No}" Header="No."/>
   <telerik:GridViewDataColumn DataMemberBinding="{Binding Adm_Date}" Header="Date Admission" />
   <telerik:GridViewDataColumn DataMemberBinding="{Binding Name}" Header="Name" />
</telerik:RadGridView.Columns>
</telerik:RadGridView>

code behind:
this.radGridView.AddHandler(GridViewCell.MouseLeftButtonDownEvent, new MouseButtonEventHandler(MouseDownOnCell), true);
 
private void MouseDownOnCell(object sender, MouseButtonEventArgs e)
{
  try
   {
       object cellValue = ((UIElement)e.OriginalSource).ParentOfType<GridViewCell>().Value;
       MessageBox.Show("Click on : " + cellValue.ToString());
   }
  catch (Exception ex)
   {
       MessageBox.Show(ex.Message);
       return;
   }
}

3 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 11 Jan 2012, 07:40 AM
Hi,

You can use ParentOfType<T> extension method and find the corresponding row - once you find the cell being clicked, you can call ParentOfType<GridViewRow>() method (in the same manner as you did in your code). 
 

Greetings,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Je
Top achievements
Rank 1
answered on 12 Jan 2012, 06:27 AM
hi maya...thanks for your prompt reply. i think it is almost done, however really appreciate if you can help me on this.

for now,  i have to display the ID in the gridview to get the ID by using UniqueName. And now, here come 2 issues:

1) when click at any row, it is not only ID is displaying but with all the telerik things (you can refer the attached image). In order to get the single ID, i'm using replace() function and i know we shouldn't do it this way and for sure there is better way to handle this.

2) how to get the ID without displaying it in the gridview. i mean, we only click on the name and the ID will be pop up.

please assist me on this...thanks for your hard work

<telerik:GridViewDataColumn UniqueName="Encounter_ID" DataMemberBinding="{Binding Encounter_ID}" Header="Encounter_ID"/>
 
----------------------------------------------------------------------------------------
 
this.radGridView.AddHandler(GridViewCell.MouseLeftButtonDownEvent, new MouseButtonEventHandler(OnSelectionChanged), true);
 
 
private void OnSelectionChanged(object sender, MouseEventArgs e)
        {
            UIElement clicked = e.OriginalSource as UIElement;
            if (clicked != null)
            {
                GridViewRow row = clicked.ParentOfType<GridViewRow>();
                if (row != null)
                {
                    GridViewCell cell = row.ChildrenOfType<GridViewCell>().Where(c => c.Column.UniqueName == "Encounter_ID").FirstOrDefault();
                    if (cell != null)
                    {
                       //MessageBox.Show(cell.ToString()); //this is the original codes i told you
                   
                       //trying to get the singe ID
                        string encounterID = cell.ToString().Replace("Telerik.Windows.Controls.GridView.GridViewCell: ", "");
                        MessageBox.Show(encounterID);
                    }
                }
            }
        }
0
Maya
Telerik team
answered on 12 Jan 2012, 09:32 AM
Hello,

Generally, my suggestion would be to work with the underlying data item instead of getting cell's value. You can find the item through row.Item property, cast it to the required type (MyBusinessObject) and get the value for Encounter_ID. Furthermore, you can handle CurrentCellChanged event of RadGridView instead of MouseLeftButtonDown.

Kind regards,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
GridView
Asked by
Je
Top achievements
Rank 1
Answers by
Maya
Telerik team
Je
Top achievements
Rank 1
Share this question
or