我正在创建一个 WPF C# 应用程序,它有一个连接到 SQL Server 数据库的数据网格。用户可以选择通过突出显示一行并单击编辑按钮来编辑数据网格中的数据,然后使用突出显示行中的数据填充几个文本框。此时,用户可以编辑数据,单击保存,数据网格会反映所做的更改。直到最近,该功能还运行良好。但是,我被要求添加一个功能,在屏幕上的其他位置显示突出显示的数据行(因为长时间查看数据网格会变得令人厌烦)。因此,当用户单击一行时,数据网格右侧的一系列文本块会更改,以更易于查看的格式显示突出显示的行的数据。该功能也可以正常工作。我现在遇到的问题,就是当一行被突出显示并自动显示文本块中的数据时,如果用户还试图编辑该行,应用程序就会崩溃。用户单击编辑后,数据在文本框中显示得很好(同时在文本块中显示相同的突出显示行);只是在单击保存时,我遇到了问题。
调试程序表明一切运行顺利。但是,单击保存后,调试器跳回到我的 myGridSelectionChanged 事件并说“未处理 NullReferenceException - 对象引用未设置为对象的实例”但是当我重新加载程序时,数据网格反映了我之前尝试进行的更改应用程序崩溃了。我假设这意味着问题与实际编辑数据库无关,而是问题在于文本块无法反映这些编辑。下面是我的一些代码:
这是保存按钮的代码:
private void saveBtn_Click(object sender, RoutedEventArgs e)
{
var stqmDC = new SqtmLinqDataContext();
var selectedRow = EditGrid.GetSelectedRow(myGrid);
var ID = EditGrid.GetCell(myGrid, selectedRow, 0);
string selectedID = ((TextBlock)ID.Content).Text;
int convertedID = Convert.ToInt32(selectedID);
int newQuantity = int.Parse(quantityTxt.Text);
var query = from info in stqmDC.General_Infos
where info.Quote_ID == convertedID
select info;
foreach (General_Info info in query)
{
info.Customer_Name = customerNameTxt.Text;
info.OEM_Name = oemNameTxt.Text;
info.Qty = newQuantity;
info.Quote_Num = quoteNumberTxt.Text;
info.Fab_Drawing_Num = fabDrawingNumTxt.Text;
info.Rfq_Num = rfqNumberTxt.Text;
info.Rev_Num = revNumberTxt.Text;
}
try
{
stqmDC.SubmitChanges();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
var mainTable = from generalInfo in stqmDC.GetTable<General_Info>()
select new
{
generalInfo.Quote_ID,
generalInfo.Open_Quote,
generalInfo.Customer_Name,
generalInfo.OEM_Name,
generalInfo.Qty,
generalInfo.Quote_Num,
generalInfo.Fab_Drawing_Num,
generalInfo.Rfq_Num,
generalInfo.Rev_Num
};
myGrid.ItemsSource = mainTable;
leftSP.Visibility = Visibility.Hidden;
rightSP.Visibility = Visibility.Hidden;
cancelBtn.Visibility = Visibility.Hidden;
submitBtn.Visibility = Visibility.Hidden;
saveBtn.Visibility = Visibility.Hidden;
sendBtn.Visibility = Visibility.Hidden;
}
以及在文本块中显示突出显示的行的代码:
private void myGridSelectionChanged(object sender, SelectionChangedEventArgs e)
{
var rowSelection = EditGrid.GetSelectedRow(myGrid);
var quoteID = EditGrid.GetCell(myGrid, rowSelection, 0);
string quoteIDEdit = ((TextBlock)quoteID.Content).Text;
QuoteIDtxtblk.Text = quoteIDEdit;
var date = EditGrid.GetCell(myGrid, rowSelection, 1);
string dateEdit = ((TextBlock)date.Content).Text;
Datetxtblk.Text = dateEdit;
var custName = EditGrid.GetCell(myGrid, rowSelection, 2);
string custNameEdit = ((TextBlock)custName.Content).Text;
CustomerNametxtblk.Text = custNameEdit;
var OemName = EditGrid.GetCell(myGrid, rowSelection, 3);
string OemNameEdit = ((TextBlock)OemName.Content).Text;
OemNametxtblk.Text = OemNameEdit;
var Quantity = EditGrid.GetCell(myGrid, rowSelection, 4);
string QuantityEdit = ((TextBlock)Quantity.Content).Text;
Quantitytxtblk.Text = QuantityEdit;
var quoteNum = EditGrid.GetCell(myGrid, rowSelection, 5);
string quoteNumEdit = ((TextBlock)quoteNum.Content).Text;
QuoteNumbertxtblk.Text = quoteNumEdit;
var fabDrawing = EditGrid.GetCell(myGrid, rowSelection, 6);
string fabDrawingEdit = ((TextBlock)fabDrawing.Content).Text;
FabDrawingNumbertxtblk.Text = fabDrawingEdit;
var rfqNum = EditGrid.GetCell(myGrid, rowSelection, 7);
string rfqNumEdit = ((TextBlock)rfqNum.Content).Text;
RfqNumbertxtblk.Text = rfqNumEdit;
var revNum = EditGrid.GetCell(myGrid, rowSelection, 8);
string revNumEdit = ((TextBlock)revNum.Content).Text;
RevNumbertxtblk.Text = revNumEdit;
}
提前感谢任何可以提供帮助的人。