Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello guys,

I am new on silverlight so I would like to know that how to select a row or cell in the datagrid in a silverlight application...

Is there anyone who can help me about this?

Regards

Haluk
Posted

1 solution

Hi,
Saying you have a data grid and a button on your main page:
XML
<Grid x:Name="LayoutRoot" Background="White" >

        <sdk:DataGrid Name="dg" SelectionChanged="DgSelectionChanged" Width="200" Height="200"/>
        <Button Content="Start" Click="ButtonClick" Width="200" Height="20" Margin="12,24,188,256" />
    </Grid>


Event for loading some data when button is clicked:

CSS
private void ButtonClick(object sender, RoutedEventArgs e)
        {
            var persons = new List<Person>
                              {
                                  new Person {Name = "Name 1", Address = "Address 1"},
                                  new Person {Name = "Name 2", Address = "Address 2"}
                              };
            dg.AutoGenerateColumns = true;
            dg.ItemsSource = persons;
            dg.SelectionMode = DataGridSelectionMode.Single;
        }


Where person:

C#
public class Person
    {
        public string Name { get; set; }

        public string Address { get; set; }
    }



And when selection changes fire the following event that will do the job you need:

private void DgSelectionChanged(object sender, SelectionChangedEventArgs e)
{
var dataGrid = sender as DataGrid;

if (dataGrid != null)
{
int selectedIndex = dataGrid.SelectedIndex;
if (selectedIndex > -1)
{
DataGridColumn column = dataGrid.Columns[0];
FrameworkElement fe = column.GetCellContent(dataGrid.SelectedItem);
FrameworkElement result = GetParent(fe, typeof(DataGridCell));

if (result != null)
{
var cell = (DataGridCell)result;
//changes the forecolor
cell.Foreground = new SolidColorBrush(Colors.Blue);
//how to get cell value?

var block = fe as TextBlock;
if (block != null)
{
string cellText = block.Text;
MessageBox.Show(cellText);
}
}
}
}
}

private FrameworkElement GetParent(FrameworkElement child, Type targetType)
{
object parent = child.Parent;
if (parent != null)
{
if (parent.GetType() == targetType)
{
return (FrameworkElement)parent;
}
return GetParent((FrameworkElement)parent, targetType);
}
return null;
}

Let me know if this helped you.
 
Share this answer
 
Comments
haluk_78 24-Aug-12 5:32am    
Well done Dragos.... Thank you so much... I love you... :-p
haluk_78 24-Aug-12 5:44am    
Dear brother,

I also would like to know that how to open an asp page from silverlight page. Could you help me about this pls?
Vlad-Dragos 24-Aug-12 9:43am    
Uri serverUri = Application.Current.Host.Source;
Uri uri = new Uri(string.Concat(serverUri.Scheme, "://", serverUri.Host.ToString(), ":", serverUri.Port.ToString(), "/ReportPage.aspx?"));
HtmlPage.Window.Navigate(uri, "_blank");
haluk_78 25-Aug-12 3:26am    
Thank you brother... I found it already...

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