So i'm working on a school project using wpf .net framework.
I have a DB that has column: id, name, date, startTime, endTime, host.
And in my software i want to add these values in a Datatable. that worked using MysqlDataAdapter and filling the dataTable.
but now after all these values are added i want to programmaticly add a new Column named TotalTime, the row values of this column is (endTime - startTime) * 60 to calculate totalTime in minutes.
I think its redundant to add this column in the DB so that's why i added it by code.
But the issue i'm facing is when i filled the datatable en try to add the NewRow to the column, the rows will add it in a new row instead of add it in the first row of the filled data.
How can i add the NewRow to this datagrid on line 1 instead of creating new empty rows like the images.
Thank you in advance!
The code for filling the datatable:
public void GetProgrammaOverzicht(int zenderId)
{
try
{
Programmas.programmaDataTable.Clear();
var query = $"SELECT naam, datum, begin_tijd, eind_tijd, presentator FROM programmas WHERE zenderId={zenderId}";
ConnectionVariables.conn.Open();
using (MySqlCommand cmdSel = new MySqlCommand(query, ConnectionVariables.conn))
{
MySqlDataAdapter da = new MySqlDataAdapter(cmdSel);
da.Fill(Programmas.programmaDataTable);
}
ConnectionVariables.conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
Console.WriteLine(ex.Message);
}
}
And the code where i add the new row + calculate getting the data from DB
public void CalculateTotalTime(int zenderId)
{
try
{
Programmas.beginTijd.Clear();
Programmas.eindTijd.Clear();
var query = $"SELECT begin_tijd, eind_tijd FROM programmas WHERE zenderId={zenderId}";
var cmd = new MySqlCommand(query, ConnectionVariables.conn);
ConnectionVariables.conn.Open();
var queryresult = cmd.ExecuteReader();
if (queryresult.HasRows)
{
while (queryresult.Read())
{
Programmas.beginTijd.Add(queryresult.GetString(0));
Programmas.eindTijd.Add(queryresult.GetString(1));
}
}
else
{
MessageBox.Show("Kan duur in minuten niet berekenen");
}
queryresult.Close();
ConnectionVariables.conn.Close();
}
catch (Exception ex)
{
ConnectionVariables.conn.Close();
MessageBox.Show(ex.Message);
Console.WriteLine(ex.Message);
}
}
private void OpenProgrammaOverzichtBtn(object sender, RoutedEventArgs e)
{
int currentZenderId;
string currentZender;
currentZender = this.Name.Remove(0, 6);
currentZenderId = Int32.Parse(currentZender);
ProgrammaOverzichtDialog.IsOpen = true;
zenderClass.GetProgrammaOverzicht(currentZenderId);
zenderClass.CalculateTotalTime(currentZenderId);
if (Programmas.programmaDataTable.Columns.Contains("Duur in minuten"))
{
Console.WriteLine("Column duur in minuten bestaat al");
}
else
{
DataColumn column = new DataColumn();
column.ColumnName = "Duur in minuten";
Programmas.programmaDataTable.Columns.Add(column);
}
DataRow row;
for (int i = 0; i < Programmas.beginTijd.Count; i++)
{
string eind = Programmas.eindTijd[i];
int eindTijd = int.Parse(eind.Remove(2, 3));
string begin = Programmas.beginTijd[i];
int beginTijd = int.Parse(begin.Remove(2, 3));
int totaal = (eindTijd - beginTijd) * 60;
row = Programmas.programmaDataTable.NewRow();
row["Duur in minuten"] = totaal;
Programmas.programmaDataTable.Rows.InsertAt(row, i);
}
programmaOverzichtGrid.DataContext = Programmas.programmaDataTable;
}
}
And the xaml where the button is located to open the DATAGRID
<materialDesign:DialogHost Name="ProgrammaOverzichtDialog" Background="#FF3F3F46">
<materialDesign:DialogHost.DialogContent >
<StackPanel>
<DataGrid x:Name="programmaOverzichtGrid" AutoGenerateColumns="True" ItemsSource="{Binding}"/>
</StackPanel>
</materialDesign:DialogHost.DialogContent>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<Button FontSize="20px" Foreground="White" Content="programmaoverzicht" Click="OpenProgrammaOverzichtBtn" x:Name="programmaOverzichtBtn"/>
</StackPanel>
</materialDesign:DialogHost>
Oke a fellow programmer i know gave me a option to try and it worked!
Answer: In my method "OpenProgrammaOverzichtBtn" i needed to change from
row = Programmas.programmaDataTable.NewRow();
row["Duur in minuten"] = totaal;
Programmas.programmaDataTable.Rows.InsertAt(row, i);
To:
row = Programmas.programmaDataTable.Rows[i];
row["Duur in minuten"] = totaal;
//Programmas.programmaDataTable.Rows.InsertAt(row, i);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.