Answer the question
In order to leave comments, you need to log in
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("Требуется ввести данные полностью!");
}
};
DG_CPU.SelectionChanged += (sender, args) =>
MainWindow.mw.CPU_Name_lbl.Content = DG_CPU.CurrentCell.Column.GetCellContent(DG_CPU.CurrentCell.Item);
Answer the question
In order to leave comments, you need to log in
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++;
}
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question