DataGridVeiw正在读取我从数据库显示的旧数据而不是新用户输入的数据。我尝试了Mode = TwoWay但仍然是同样的事情。[读取每一行并进入数据库]有什么帮助吗?
这是我的XAML代码:
<DataGridTextColumn x:Name="DG_Vname" Header=" Vendor Name " MinWidth="50" Width="*" Binding="{Binding Path=VP_Vname}" FontFamily="Calibri" IsReadOnly="True" />
<!--<DataGridTextColumn x:Name="DG_VItemNo" Header=" Vendor SKU# " MinWidth="50" Width="*" Binding="{Binding Path=VP_VendorSKU}" FontFamily="Calibri" Foreground="Blue"/> -->
<DataGridTemplateColumn Header=" Vendor SKU# " Width="125">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox x:Name="DG_VItemNo" HorizontalAlignment="Right" Text="{Binding Path=VP_VendorSKU}" FontFamily="Calibri" TextAlignment="Right" Foreground="Blue" MinWidth="124" Padding="1,0,5,0" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<!-- ================= -->
<!-- <DataGridTextColumn x:Name="DG_VCaseCost" Header=" Case Cost " MinWidth="50" Width="100" Binding="{Binding Path=VP_SKUcaseCost}" FontFamily="Calibri"/> -->
<!-- ================= -->
<DataGridTemplateColumn Header=" Case Cost " Width="100">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox x:Name="DG_VCaseCost" HorizontalAlignment="Right" Text="{Binding Path=VP_SKUcaseCost, Mode=TwoWay}" FontFamily="Calibri" TextAlignment="Right" Foreground="Blue" MinWidth="100" Padding="1,0,5,0" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<!-- ================= -->
<!-- <DataGridTextColumn x:Name="DG_VQtyPerCase" Header=" Qty/Case " MinWidth="50" Width="100" Binding="{Binding Path=VP_QtyInCase}" FontFamily="Calibri" /> -->
<DataGridTemplateColumn Header=" Qty/Case " Width="100">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox x:Name="DG_VQtyPerCase" HorizontalAlignment="Center" Text="{Binding Path=VP_QtyInCase, Mode=TwoWay}" FontFamily="Calibri" Foreground="Blue" MinWidth="100" TextAlignment="Center" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<!-- ================= -->
<DataGridTemplateColumn Header="Preferred" Width="90">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<RadioButton x:Name="DG_isPreferred" HorizontalAlignment="Center" GroupName="Preferred" IsChecked="{Binding Path=VP_isVendorActive, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" MaxWidth="25" VerticalContentAlignment="Center" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<!-- ================= -->
</DataGrid.Columns>
</DataGrid>
这是我的XAML.CS:
private void SaveVendorList()
{
int NOR = DG_VendorPur.Items.Count;
DG_VendorPur.SelectedIndex = 0;
for (int i = 0; i < NOR; i++)
{
DG_VendorPur.SelectedItem = DG_VendorPur.Items[i];
DataRowView DRow = (DataRowView)DG_VendorPur.SelectedItems[0];
string R_No = DRow["VP_SrNo"].ToString();
string R_ProdID = TB_PscanID.Text;
string R_isPref = DRow["VP_isVendorActive"].ToString();
string R_Name = DRow["VP_Vname"].ToString();
string R_SKU = DRow["VP_VendorSKU"].ToString();
string R_CaseCost = DRow["VP_SKUcaseCost"].ToString();
string R_CaseQty = DRow["VP_QtyinCase"].ToString();
MessageBox.Show(R_No+" : "+ R_ProdID+ " : " +R_isPref+" : "+ R_Name + " : " + R_SKU + " : " + R_CaseCost + " : " + R_CaseQty);
using (SQLiteConnection sqliteCon = new SQLiteConnection(dbConnectionString))
using (var cmd = sqliteCon.CreateCommand())
try
{
sqliteCon.Open();
int SrNo = VPrecord + 1;
if ((Save_UpdateVendorList == "Save")&&(int.Parse(R_No) == NOR ))
{
cmd.CommandText = "insert into VendorProducts (VP_SrNo,VP_ProductID,VP_isVendorActive,VP_Vname,VP_VendorSKU,VP_SKUcaseCost,VP_QtyinCase )values(@val1,@val2,@val3,@val4,@val5,@val6,@val7)";
}
else
{
cmd.CommandText = "update VendorProducts set VP_SrNo=@val1 ,VP_ProductID=@val2 ,VP_isVendorActive=@val3 ,VP_Vname=@val4 ,VP_VendorSKU=@val5 ,VP_SKUcaseCost=@val6 ,VP_QtyinCase=@val7 where VP_SrNo =@val1 ";
}
cmd.Parameters.AddWithValue("@val1", R_No);
cmd.Parameters.AddWithValue("@val2", R_ProdID);
cmd.Parameters.AddWithValue("@val3", R_isPref);
cmd.Parameters.AddWithValue("@val4", R_Name);
cmd.Parameters.AddWithValue("@val5", R_SKU);
cmd.Parameters.AddWithValue("@val6", R_CaseCost);
cmd.Parameters.AddWithValue("@val7", R_CaseQty);
cmd.Prepare();
cmd.ExecuteNonQuery();
sqliteCon.Close();
Save_Update = "";
DispDataGrid();
CB_VendorSearch.SelectedIndex = 0;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}