N
N
Nikolay2016-05-20 23:51:33
WPF
Nikolay, 2016-05-20 23:51:33

How to get data from selected cell of DataGrid?

When clicking, I need to get the data from the cell that was clicked. How can i do this?

class MyTable
    {
        public MyTable(bool Specification, int Account, string Customer, double Amount, double PrePayment, double Payment, double Payment2, double Debt, string siemens_specification, int siemens_order, long siemens_account, double siemens_amount, double siemens_prepayment, double siemens_payment_1, double siemens_payment_2, double siemens_debt)
        {
            this.Спецификация = Specification;
            this.Счет = Account;
            this.Заказчик = Customer;
            this.Сумма = Amount;
            this.Предоплата = PrePayment;
            this.Оплата = Payment;
            this.Оплатa = Payment2;
            this.Долг = Debt;
            this.СпецификацияSiemens = siemens_specification;
            this.НомерЗаказаSiemens = siemens_order;
            this.НомерCчетаSiemens = siemens_account;
            this.СуммаSiemens = siemens_amount;
            this.ПредоплатаSiemens = siemens_prepayment;
            this.ОплатаSiemens = siemens_payment_1;
            this.ОплатaSiemens = siemens_payment_2;
            this.ДолгSiemens = siemens_debt;
        }

        public bool Спецификация { get; set; }
        public int Счет { get; set; }
        public string Заказчик { get; set; }
        public double Сумма { get; set; }
        public double Предоплата { get; set; }
        public double Оплата { get; set; }
        public double Оплатa { get; set; }
        public double Долг { get; set; }
        public string СпецификацияSiemens { get; set; }
        public int НомерЗаказаSiemens { get; set; }
        public long НомерCчетаSiemens { get; set; }
        public double СуммаSiemens { get; set; }
        public double ПредоплатаSiemens { get; set; }
        public double ОплатаSiemens { get; set; }
        public double ОплатaSiemens { get; set; }
        public double ДолгSiemens { get; set; }
    }

<DataGrid x:Name="grid" Margin="20,206,17,10" Loaded="grid_Loaded" MouseUp="grid_MouseUp" HorizontalContentAlignment="Stretch" SelectionUnit="Cell"/>

static class Companies
    {
        private static List<MyTable> companies = new List<MyTable>();

        public static List<MyTable> Get()
        {
            User.connect.Open();
            MySqlCommand cmd = new MySqlCommand(Query.Select.Companies, User.connect);
            MySqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                companies.Add
                (
                    new MyTable
                    (
                        reader.GetBoolean(1), 
                        reader.GetInt32(2), 
                        reader.GetString(3), 
                        reader.GetDouble(4),
                        reader.GetDouble(5),
                        reader.GetDouble(6),
                        reader.GetDouble(7),
                        reader.GetDouble(8),
                        reader.GetString(9),
                        reader.GetInt32(10),
                        reader.GetInt64(11),
                        reader.GetDouble(12),
                        reader.GetDouble(13),
                        reader.GetDouble(14),
                        reader.GetDouble(15),
                        reader.GetDouble(16)
                    )
                );
            }
            User.connect.Close();

            return companies;
        }
    }

private void grid_Loaded(object sender, RoutedEventArgs e)
        {
            grid.ItemsSource = Companies.Get();
            Hide_grid_columns(grid);
        }
private void grid_MouseUp(object sender, MouseButtonEventArgs e)
        {
            MyTable path = grid.SelectedItem as MyTable;
            MessageBox.Show(" Account: " + path.Счет + "\n Company: " + path.Заказчик + "\n Amount: " + path.Сумма
                + "\n Prepayment: " + path.Предоплата + "\n Siemens: " + path.НомерCчетаSiemens);
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
#
#algooptimize #bottize, 2016-05-20
@tryvols

try datagrid.currentcell
//head on, dirty, null checks omitted
var cell = this.list.CurrentCell;
var binding = (Binding)((DataGridBoundColumn) cell.Column).Binding;
varname = binding.Path.Path;
var value = cell.Item.GetType().GetProperty(name).GetValue(cell.Item);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question