我有一个自定义DataGrid,其中一列包含一个绑定到Table列的TextBox。我有一个DataTable来填充实际数据库(只是数据集)中实际上不存在的Grid,该数据库用于填充和更新与实际数据库链接的DataTable。其他所有插入都很好,但不是注释字段。相反,它总是最终被我初始化为("") - 用户的输入不会被保存。我认为Binding会自动更新DataTable,但似乎并非如此。什么可能是一个好的解决方案?
DataGrid的XAML:
<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>
以及我的C#代码中的一些片段
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();
}
}
非常感谢您的任何指导。