2016-05-18 9 views
-1

Ich habe ein benutzerdefiniertes DataGrid, in dem eine der Spalten eine TextBox enthält, die an eine Tabellenspalte gebunden ist. Ich habe eine DataTable, um das Grid zu füllen, das nicht wirklich in der tatsächlichen Datenbank vorhanden ist (nur das Dataset), das verwendet wird, um eine DataTable, die mit der tatsächlichen Datenbank verknüpft ist, zu füllen und zu aktualisieren. Alles andere fügt nur gut ein, aber nicht das Kommentarfeld. Stattdessen endet es immer als das, was ich initialisiert habe ("") - die Benutzereingabe wird nicht gespeichert. Ich dachte, die Bindung würde die DataTable automatisch aktualisieren, aber das scheint nicht der Fall zu sein. Was könnte eine gute Lösung sein?C# WPF - DataGrid Binding Probleme

Die XAML für das Datagrid:

<DataGrid CanUserAddRows="False" CanUserDeleteRows="False" x:Name="itemGrid" AutoGenerateColumns="False" ItemsSource="{Binding}" HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch" MinHeight="400" Height="400" Width="800" > 
    <DataGrid.Columns> 
     <DataGridTextColumn IsReadOnly="True" Header="Name" Binding="{Binding Name}" CanUserResize="False" /> 
     <DataGridTextColumn IsReadOnly="True" Header="Description" Binding="{Binding Description}" /> 
     <DataGridTextColumn IsReadOnly="True" Header="Points Possible" Binding="{Binding Points}" /> 

     <DataGridTemplateColumn Header="Deductions" Width="50"> 
      <DataGridTemplateColumn.CellTemplate> 
       <DataTemplate> 
         <ComboBox Height="40" VerticalAlignment="Center" HorizontalAlignment="Center" ItemsSource="{Binding Score}" SelectedIndex="0" SelectionChanged="updateScore" /> 
       </DataTemplate> 
      </DataGridTemplateColumn.CellTemplate> 
       <DataGridTemplateColumn.CellEditingTemplate> 
        <DataTemplate> 
         <ComboBox ItemsSource="{Binding Score}" SelectedIndex="0" SelectionChanged="updateScore" Height="40" VerticalAlignment="Center" HorizontalAlignment="Center" /> 
        </DataTemplate> 
       </DataGridTemplateColumn.CellEditingTemplate> 
      </DataGridTemplateColumn> 
      <DataGridTextColumn IsReadOnly="True" Width="50" Header="Score" Binding="{Binding Current}" /> 
      <DataGridTemplateColumn Header="Comments" MinWidth="100" Width="300"> 
      <DataGridTemplateColumn.CellTemplate> 
       <DataTemplate> 
        <TextBox Text="{Binding Comments}" Margin="10" Width="Auto" Height="100"></TextBox> 
       </DataTemplate> 
      </DataGridTemplateColumn.CellTemplate> 
       <DataGridTemplateColumn.CellEditingTemplate> 
        <DataTemplate> 
         <TextBox Text="{Binding Comments, Mode=TwoWay}" Margin="10" Width="Auto" Height="100"></TextBox> 
        </DataTemplate> 
       </DataGridTemplateColumn.CellEditingTemplate> 
      </DataGridTemplateColumn> 
    </DataGrid.Columns> 
</DataGrid> 

und einige Auszüge aus meinem C# -Code

try { con.Open(); } 
     catch (SqlException er) { Console.Write(er); } 

     String query = "SELECT * from dbo.locations"; 
     locAdapter = new SqlDataAdapter(query, con); 
     locAdapter.Fill(ds, "Locations"); 



     query = "SELECT * from dbo.report_summary"; 
     reportAdapter = new SqlDataAdapter(query, con); 
     reportAdapter.Fill(ds, "Reports"); 
     SqlCommand insert = new SqlCommand("INSERT into dbo.report_summary (report_id, inspector, employee, room, date, score, locationID) " + " VALUES (@report_id, @inspector, @employee, @room, @date, @score, @locationID)", con); 
     insert.Parameters.Add("@report_id", SqlDbType.Char, 5, "report_id"); 
     insert.Parameters.Add("@room", SqlDbType.Int, 4, "room"); 
     insert.Parameters.Add("@inspector", SqlDbType.Int, 5, "inspector"); 
     insert.Parameters.Add("@employee", SqlDbType.Int, 4, "employee"); 
     insert.Parameters.Add("@date", SqlDbType.Date, 50, "date"); 
     insert.Parameters.Add("@score", SqlDbType.Int, 4, "score"); 
     insert.Parameters.Add("@locationID", SqlDbType.Int, 4, "locationID"); 
     reportAdapter.InsertCommand = insert; 


     query = "SELECT * from dbo.report_details"; 
     detailsAdapter = new SqlDataAdapter(query, con); 
     detailsAdapter.Fill(ds, "Details"); 

    insert = new SqlCommand("INSERT into dbo.report_details (reportID, itemID, points, comments) " + " VALUES (@reportID, @itemID, @points, @comments)", con); 
     insert.Parameters.Add("@reportID", SqlDbType.Char, 5, "reportID"); 
     insert.Parameters.Add("@itemID", SqlDbType.Int, 5, "itemID"); 
     insert.Parameters.Add("@points", SqlDbType.Int, 4, "points"); 
     insert.Parameters.Add("@comments", SqlDbType.Text, 150, "comments"); 

     detailsAdapter.InsertCommand = insert; 


     locationComboBox.DataContext = ds.Tables["Locations"]; 
     locationComboBox.DisplayMemberPath = "locName"; 



     DataTable grid = new DataTable("Grid"); 
     grid.Columns.Add("ID", typeof(int)); 
     grid.Columns.Add("Name", typeof(String)); 
     grid.Columns.Add("Description", typeof(String)); 
     grid.Columns.Add("Points", typeof(int)); 
     grid.Columns.Add("Score", typeof(List<int>)); 
     grid.Columns.Add("Current", typeof(int)); 
     grid.Columns.Add("Comments", typeof(String));  



     query = "SELECT itemID, name, description, points, category FROM dbo.items"; 

     SqlDataReader reader = new SqlCommand(query, con).ExecuteReader(); 

     while (reader.Read()) 
     { 
      DataRow row = grid.NewRow(); 

      row["ID"] = reader["itemID"]; 
      row["Name"] = reader["name"]; 
      row["Description"] = reader["description"]; 
      row["Points"] = reader["points"]; 
      totalPoints += (int)reader["points"]; 
      row["Current"] = reader["points"]; 
      row["Comments"] = ""; 
      int pointsPossible = (int)reader["points"]; 
      List<int> rowList = new List<int>(); 
      for (int i = pointsPossible; i >= 0; i--) 
      { 
       rowList.Add(i); 
      } 
      rowList.Sort(); 
      row["Score"] = rowList; 


      grid.Rows.Add(row); 



     } 
     ds.Tables.Add(grid); 

     itemGrid.ItemsSource = ds.Tables["Grid"].DefaultView; 






private void updateDatabase() 
     { 
      SqlTransaction tran = con.BeginTransaction(); 

      reportAdapter.InsertCommand.Transaction = tran; 

      detailsAdapter.InsertCommand.Transaction = tran; 

      DataRow reportRow = ds.Tables["Reports"].NewRow(); 

      reportRow["report_id"] = reportID; 
      DataRowView inspectorSelection = (DataRowView)inspectorBox.SelectedItem; 
      reportRow["inspector"] = Int16.Parse(inspectorSelection["empID"].ToString()); 

      DataRowView empSelection = (DataRowView)employeeBox.SelectedItem; 
      reportRow["employee"] = Int16.Parse(inspectorSelection["empID"].ToString()); 

      DataRowView locationSelection = (DataRowView)locationComboBox.SelectedItem; 
      reportRow["locationID"] = Int16.Parse(locationSelection["locID"].ToString()); 


      reportRow["room"] = Int16.Parse(roomTextBox.Text); 

      reportRow["date"] = DateTime.Now.ToString("yyy-MM-dd"); 



      reportRow["score"] = currentPoints; 

      ds.Tables["Reports"].Rows.Add(reportRow); 

      // update report_details dataset 

      foreach (DataRow row in ds.Tables["Grid"].Rows) 
      { 

       DataRow reportDetailsRow = ds.Tables["Details"].NewRow(); 

       reportDetailsRow["reportID"] = reportID; 
       reportDetailsRow["itemID"] = row["ID"]; 
       reportDetailsRow["points"] = row["Current"]; 
      // figure out why comments are not being inserted 
       reportDetailsRow["comments"] = row["Comments"]; 

       ds.Tables["Details"].Rows.Add(reportDetailsRow); 

      } 

      // update tables as single transaction 
      try 
      { 

       reportAdapter.Update(ds, "Reports"); 

       detailsAdapter.Update(ds, "Details"); 
       tran.Commit(); 


      } 
      catch (SqlException sqlEr) 
      { 
       MessageBox.Show(sqlEr.Message); 
       tran.Rollback(); 
      } 

     } 

Danke so sehr für jede Führung.

Antwort

0

Nur eine Vermutung, aber DataTable hat eine AcceptChanges Funktion, die Sie möglicherweise anrufen müssen, bevor die Änderungen tatsächlich widergespiegelt werden.