locked
How to access columns data from the silverlight datagrid ? RRS feed

  • Question

  • Hi friends,

    I'm having the following table "Lectures_Table" with following columns in it (L_ID int, Lecturename varchar(MAX), Lurl varchar(MAX), Limage varchar(MAX)).

    I want to access the "Lurl" field using button click event can anybody tell me how can I do it.

    Wednesday, March 21, 2012 11:59 AM

Answers

  • Because the datagrid is using BINDING, then you can simply get the currentSelectedItem which will be of the type you bound to...   lecturesDataSource

    So to get values you look for the DataGrid.SelectedItem and cast it to type of the collection's base type  like lecture.

    Code would look like this:

    Lecture lecture = (Lecture)myDataGrid.SelectedItem; 

    Keep in mind that you can also use binding to set a property value as well:

    <DataGrid ItemsSource="{Binding Path=Lectures}" 
                  SelectedItem="{Binding Path=SelectedLecture, Mode=TwoWay}"/> 
    
    Where SelectedLecture IS of type Lecture and has a public getter setter, and a backing store, as well as the datagrid knowing the DATACONTEXT.


    JP Cowboy Coders Unite!

    • Marked as answer by Atinesh Friday, March 23, 2012 9:20 PM
    Thursday, March 22, 2012 9:30 PM

All replies

  • Can you provide more details? It seems like you are trying to access a SQL server from Silverlight? That requires a web service of some sort.
    Wednesday, March 21, 2012 12:57 PM
    Moderator
  • Here's the xaml

    <riaControls:DomainDataSource QueryName="GetLectures_Table" x:Name="lecturesDataSource" LoadSize="20" AutoLoad="True">
                        <riaControls:DomainDataSource.DomainContext>
                            <ds:ATI_Virtual_Classroom />
                        </riaControls:DomainDataSource.DomainContext>
                    </riaControls:DomainDataSource>
                	<sdk:DataGrid x:Name="dgrdlectures" Height="200" Width="500" HorizontalAlignment="Left" Margin="20,159,0,121" IsReadOnly="True" 
                              ItemsSource="{Binding Data, ElementName=lecturesDataSource}" />
                	<sdk:DataPager Margin="20,0,120,76" PageSize="5" Source="{Binding Data, ElementName=lecturesDataSource}" VerticalAlignment="Bottom" Height="41"/>
                    <Button x:Name="btnplay" Content="Play" Height="35" Width="100" HorizontalAlignment="Left" Margin="20,0,0,37" VerticalAlignment="Bottom" d:LayoutOverrides="Width" Click="btnplay_Click"/>
    at the button click event I want to store selected row's "Lurl" value in the string variable.


    • Edited by Atinesh Wednesday, March 21, 2012 3:46 PM
    Wednesday, March 21, 2012 3:33 PM
  • This might be a good question for the WCF RIA services forum, I am not very familiar with it.

    http://forums.silverlight.net/53.aspx

    Wednesday, March 21, 2012 3:58 PM
    Moderator
  • Actually I want to access the selected rows field on button click event.

    Ex: When I click the button then selected row's "Lurl" field should get stored in some string variable.

    Thursday, March 22, 2012 8:42 PM
  • Because the datagrid is using BINDING, then you can simply get the currentSelectedItem which will be of the type you bound to...   lecturesDataSource

    So to get values you look for the DataGrid.SelectedItem and cast it to type of the collection's base type  like lecture.

    Code would look like this:

    Lecture lecture = (Lecture)myDataGrid.SelectedItem; 

    Keep in mind that you can also use binding to set a property value as well:

    <DataGrid ItemsSource="{Binding Path=Lectures}" 
                  SelectedItem="{Binding Path=SelectedLecture, Mode=TwoWay}"/> 
    
    Where SelectedLecture IS of type Lecture and has a public getter setter, and a backing store, as well as the datagrid knowing the DATACONTEXT.


    JP Cowboy Coders Unite!

    • Marked as answer by Atinesh Friday, March 23, 2012 9:20 PM
    Thursday, March 22, 2012 9:30 PM
  • Hi Mr. Javaman II,

    I'm having the table name "Lectures_Table" and I want to access the tables selected row's "Lurl" field in the button click event. Could you please tell me how can I do it using the code behind. I'm using the following xaml code

    <riaControls:DomainDataSource QueryName="GetLectures_Table" x:Name="lecturesDataSource" LoadSize="20" AutoLoad="True">
                        <riaControls:DomainDataSource.DomainContext>
                            <ds:ATI_Virtual_Classroom />
                        </riaControls:DomainDataSource.DomainContext>
                    </riaControls:DomainDataSource>
                    <sdk:DataGrid x:Name="dgrdlectures" Height="500" Width="500" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="20,50,0,121" IsReadOnly="True" 
                              ItemsSource="{Binding Data, ElementName=lecturesDataSource}">
                        <sdk:DataGrid.Columns>
                            <sdk:DataGridTemplateColumn Header="Lecture Pic">
                                <sdk:DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <Image Height="150" Width="150" Source="{Binding Limage}"/>
                                    </DataTemplate>
                                </sdk:DataGridTemplateColumn.CellTemplate>
                            </sdk:DataGridTemplateColumn>
                        </sdk:DataGrid.Columns>
                    </sdk:DataGrid>
                    <sdk:DataPager Margin="20,0,120,205" PageSize="3" VerticalAlignment="Bottom" Source="{Binding Data, ElementName=lecturesDataSource}" Height="41" d:LayoutOverrides="VerticalAlignment"/>
                    <Button x:Name="btnplay" Content="Play" Width="100" HorizontalAlignment="Left" Margin="20,0,0,166" d:LayoutOverrides="Width" Click="btnplay_Click" Height="35" VerticalAlignment="Bottom"/>

    Friday, March 23, 2012 12:35 PM
  • Is this a homework assignement?

    I'm pretty sure I already pretty much gave away the answer?  Did you try the sugesstion above?


    JP Cowboy Coders Unite!

    Friday, March 23, 2012 12:51 PM