N
N
Nikolay2016-06-17 13:43:31
WPF
Nikolay, 2016-06-17 13:43:31

How to get 1st cell value from row with selected DataGrid cell?

Filling comes from the database:

static class Companies
    {
        public static System.Data.DataSet companies = new System.Data.DataSet();

        public static System.Data.DataView Get()
        {
            User.connect.Open();
            MySqlCommand cmd = new MySqlCommand("SELECT * FROM data", User.connect);
            MySqlDataAdapter adapter = new MySqlDataAdapter();
            adapter.SelectCommand = cmd;
            adapter.Fill(companies);
            User.connect.Close();
            return new System.Data.DataView(companies.Tables[0]);
        }
    }

private void grid_Loaded(object sender, RoutedEventArgs e)
        {
            grid.ItemsSource = Companies.Get();
            Hide_grid_columns(grid);
        }

Then a certain cell is edited, and to add it to the database, you need to get the value of the cell in the first column of the same row - the essence of id. I'm trying like this:
private void grid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        {
            TextBox t = e.EditingElement as TextBox;
            CheckBox c = e.EditingElement as CheckBox;
            string db_column = e.Column.Header.ToString();
            var ci = new DataGridCellInfo(e.Row.Item, grid.Columns[0]);
            var index = ci.Column.GetCellContent(ci.Item) as TextBlock;
            var query = @"";
            if (db_column.ToString() != "specification")
            {
                query = @"UPDATE data SET " + db_column + " = '" + t.Text + "' WHERE id = " + index.Text;
            }
            else
            {
                var b = 0;
                if (c.IsChecked == true)
                {
                    b = 1;
                }
                query = @"UPDATE data SET " + db_column + " = '" + b + "' WHERE id = " + index.Text;
            }

            try
            {
                User.connect.Open();
                MySqlCommand update = new MySqlCommand(query, User.connect);
                int numRowsUpdated = update.ExecuteNonQuery();
                User.connect.Close();
            }
            catch (MySqlException exSql)
            {
                Console.Error.WriteLine("Error - SafeMySql: SQL Exception: " + query);
                Console.Error.WriteLine(exSql.StackTrace);
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine("Error - SafeMySql: Exception: " + query);
                Console.Error.WriteLine(ex.StackTrace);
            }
            dispatcherTimer.Start();
        }

But doesn't work.
How can I get that cell, or somehow update the row in the database in a different way?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rou1997, 2016-06-17
@Rou1997

MySqlCommandBuilder.

N
Nikolay, 2016-06-17
@tryvols

Companies.adapter.Update((System.Data.DataSet)grid.ItemsSource);
gives a conversion error, how to fix it?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question