Question #37965

Hi, how can i get the selected column value of a gridview inside a listview? I am using WPF C#.
For example, i want to get the fifth column data of a selected row, how can i do it?

Expert's answer

Answer on Question#37965- Programming, C#

1. Hi, how can i get the selected column value of a gridview inside a listview? I am using WPF C#.

For example, i want to get the fifth column data of a selected row, how can i do it?

Solution.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        DataTable myTable = new DataTable();
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            myTable.Columns.Add("1_String");
            myTable.Columns.Add("2_String");
            myTable.Columns.Add("3_String");
            foreach(DataColumn col in myTable.Columns)
            {
                dataGrid1.Columns.Add( new DataGridTextColumn()
                {
                    Header=col.ColumnName,
                    Binding= new Binding(String.Format("[0]"), col.ColumnName)
                });
            }
            dataGrid1.DataContext = myTable;
        }
        int i = 0;
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            DataRow row = myTable.NewRow();
            myTable.Rows.Add(i*5, i + 5, "Select any cell!");
            dataGrid1.Items.Add(myTable.Rows[i]);
            i++;
        }
    }
}
private void dataGrid1_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    textBox1.Text = "1_String: " + myTable.Rows[dataGrid1.SelectedIndex][0].ToString() + " 2_String: "+ myTable.Rows[dataGrid1.SelectedIndex][1].ToString();
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS