how to get row value from database?

2 posts, 0 answers
  1. Aldy
    Aldy avatar
    8 posts
    Member since:
    Sep 2015

    Posted 10 Nov 2015 Link to this post

    I want to get row value after i click a cell. I try using SelectionChanged and follow so many Q&A in many forum and still can't do this. maybe if you guys can give me an example for this, maybe after click a cell it will give message about every value in that row?

    here is my whole code, i hope telerik team can help me.

    XAML

    <Grid>
            <StackPanel x:Name="xPanel">
                <telerik:RadGridView x:Name="dataGrid"
                                     AutoGenerateColumns="False"
                                     ColumnWidth="*"
                                     ShowGroupPanel ="False"
                                     CanUserReorderColumns ="False"
                                     ItemsSource="{Binding}"
                                     SelectionChanged="dataGrid_SelectionChanged">
                    <telerik:RadGridView.Columns>
                        <telerik:GridViewDataColumn Header="idx"
                                                    DataMemberBinding="{Binding idx}" />
                        <telerik:GridViewDataColumn Header="tag"
                                                    DataMemberBinding="{Binding tag}" />
                        <telerik:GridViewDataColumn Header="value"
                                                    DataMemberBinding="{Binding value}" />
                    </telerik:RadGridView.Columns>
                </telerik:RadGridView>
                <telerik:RadButton Content="Update" Height="30" Click="RadButton_Click" />
            </StackPanel>
        </Grid>

    CS

    namespace Setting
    {
        public partial class MainWindow : Window
        {
            ScimoreDataAdapter dataAdp;
            DataSet ds;
     
            public MainWindow()
            {
                InitializeComponent();
     
                string dbInstanceName = "C:\\Users\\Abc\\Documents\\Visual Studio 2010\\Projects\\Setting\\Setting\\configdb";
                ScimoreEmbedded em = new ScimoreEmbedded();
                em.Open(dbInstanceName);
     
                try
                {
                    using (ScimoreConnection cn = em.CreateConnection())
                    {
                        cn.Open();
                        string query = "select idx,tag,value from config.info";
                        dataAdp = new ScimoreDataAdapter(query, cn);
                        DataTable dataTable = new DataTable("info");
                        ds = new System.Data.DataSet();
                        dataAdp.Fill(ds, "info");
                        dataGrid.ItemsSource = ds.Tables[0];
                        dataAdp.Update(ds, "info");
                    }
                }
                catch (Exception)
                {
                }
            }
     
            private void RadButton_Click(object sender, RoutedEventArgs e)
            {
                 
            }
     
            private void dataGrid_SelectionChanged(object sender, SelectionChangeEventArgs e)
            {
                //System.Data.DataRowView CurrentSelected = ((System.Data.DataRowView)dataGrid.SelectedItem);
                //MessageBox.Show(Convert.ToString(CurrentSelected.Row.ItemArray[1]));
            }
        }
    }

  2. Dilyan Traykov
    Admin
    Dilyan Traykov avatar
    371 posts

    Posted 13 Nov 2015 Link to this post

    Hello Aldy,

    You can have a look at the Selection Events page of our documentation. You have correctly approached the Selection Changed event, you just need to combine it with the RadGridView SelectedItem property.
    I've attached a sample project showing how to achieve the desired behaviour as I could not use your data source.

    Here is the important part:

    public MainWindow()
    {
        InitializeComponent();
        this.radGridView.ItemsSource = EmployeeService.GetEmployees(); // change it with your data source
        this.radGridView.SelectionChanged += this.SelectionChanged;
    }
    private void SelectionChanged(object sender, SelectionChangeEventArgs e)
    {
        var employee = this.radGridView.SelectedItem as Employee;
        MessageBox.Show(string.Format("{0} {1}, {2}", employee.FirstName, employee.LastName, employee.IsMarried ? "Married" : "Not Married")); // modify as you like
    }

    Let me know if this helps.

    Regards,
    Dilyan Traykov
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. UI for WPF is Visual Studio 2017 Ready
Back to Top