A
A
Anton Burkin2014-11-09 22:56:41
Programming
Anton Burkin, 2014-11-09 22:56:41

How to extract mouse clicked row from C# wpf DataGrid?

There are two windows (main and side). There are two labels on the main one, on a secondary DataGrid with an MS_SQL table connected to it. It is required to transfer two cells from the selected line with the mouse to the main form. In Windows Forms, this is done very easily and logically, but in WPF, the datagrid simply simply does not mention the row (row)

//Мой первый вариант приложения на Windows Forms
dataGridView1.CellMouseClick += (sender, args) => // перекидывание значений на главное окно
            {
                try
                {
                    Program.Mw.CPU_lbl_vendor.Text = string.Format("Cpu Vendor: {0}",
                        dataGridView1.SelectedRows[0].Cells[1].Value.ToString());
                    Program.Mw.CPU_lbl_name.Text = string.Format("Name: {0}",
                        dataGridView1.SelectedRows[0].Cells[2].Value.ToString());
                    Program.Mw.CPU_lbl_price.Text = string.Format("Price: {0}",
                        dataGridView1.SelectedRows[0].Cells[6].Value.ToString());
                }
                catch (Exception)
                {
                    MessageBox.Show("Требуется ввести данные полностью!");
                }
            };

With WPF, I have already googled all Russian, English and not only the Internet, well, I don’t even know what to do anymore, so I came here.
So far, I am extracting the element selected with the mouse through very large gardens like this:
DG_CPU.SelectionChanged += (sender, args) =>
                MainWindow.mw.CPU_Name_lbl.Content = DG_CPU.CurrentCell.Column.GetCellContent(DG_CPU.CurrentCell.Item);

But this is not the case, because I need specific fields from the string.
Now you say they say: "Here is Windows Forms, everything works for you there, so use it," but people, you want to grow all the same.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Anton Burkin, 2014-11-10
@the_RAMZAY

UPD . I found a more adequate solution to the problem:

public CPU_window()
        {
            InitializeComponent();
            DG_CPU.SelectionMode = DataGridSelectionMode.Extended;
            //DG_CPU.SelectionChanged += (sender, args) =>
            //    MainWindow.mw.CPU_Name_lbl.Content = DG_CPU.CurrentCell.Column.GetCellContent(DG_CPU.CurrentCell.Item);
            DG_CPU.SelectionChanged += (sender, args) =>
            {
                MainWindow.mw.CPU_Vender_lbl.Content = string.Format("Vender is: {0}",
                    (DG_CPU.SelectedItem as CPU).Vendor);
                MainWindow.mw.CPU_Name_lbl.Content = string.Format("Name is: {0}", (DG_CPU.SelectedItem as CPU).Name);
                MainWindow.mw.CPU_Price_lbl.Content = (DG_CPU.SelectedItem as CPU).Price;
            };
            up.Click += (sender, args) => (DG_CPU.SelectedItem as CPU).Price++;
        }

The main trick is that you should cast the SelectedItem property of your datagrid to the table class described in the model (in my case, I chose Linq to SQL, and therefore the table name in sql is identical to the name in the C# table class), the properties of which are identical to the table column names.

A
Alm1111, 2015-12-21
@Alm1111

Guys! Everything is quite simple, no need to be smart)))
//We cast the string to the class whose object is displayed in the table
notes n = (notes)dg1.Items[dg1.SelectedIndex];
//We call one of the methods of the object, the value of which it takes from the table (text column message)
MessageBox.Show(n.message);
//My class "notes" is a relational database entity created using the ADO.NET model

V
Vitaly, 2014-11-10
@vipuhoff

I advise you to look here , everything is described in some detail. I can’t say anything about the performance of this method, but it looks pretty rough.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question