Answer the question
In order to leave comments, you need to log in
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);
}
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();
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question