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

how to get row value from database?

1 Answer 191 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Aldy
Top achievements
Rank 1
Aldy asked on 10 Nov 2015, 10:09 AM

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]));
        }
    }
}

1 Answer, 1 is accepted

Sort by
0
Dilyan Traykov
Telerik team
answered on 13 Nov 2015, 10:50 AM
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
Tags
GridView
Asked by
Aldy
Top achievements
Rank 1
Answers by
Dilyan Traykov
Telerik team
Share this question
or