ユーザーがデータベースに情報を追加できるポップアップデータグリッドがあります。ユーザーが「同意する」を2回押す必要があることを除いて、完全に機能します。最初に別の行を追加するとき、2回目は正しい関数を実行します。(ユーザーは画面上の任意のボタンを押すことができ、その行が追加されます)どうすればよいですか?
XAML
<DataGrid Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" FontSize="17" AutoGenerateColumns="False" EnableRowVirtualization="True"
Margin="15" Name="addProductDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" Visibility="Collapsed" CanUserAddRows="True">
<DataGrid.Columns>
<DataGridTextColumn x:Name="prodNameColumn1" Binding="{Binding One}" Header="Name" Width="250"/>
<DataGridTextColumn x:Name="prodPriceColumn1" Binding="{Binding Two}" Header="Price" Width="100" />
<DataGridTextColumn x:Name="prodCategoryNameColumn1" Binding="{Binding Three}" Header="Category" Width="200"/>
</DataGrid.Columns>
</DataGrid>
コード
AddInput.Visibility = System.Windows.Visibility.Collapsed;
_currentDataGrid.Visibility = System.Windows.Visibility.Collapsed;
MenuDirectEntities db = new MenuDirectEntities();
BindingList<product> tempproduct = new BindingList<product>();
BindingList<Category> tempCategory = new BindingList<Category>();
String productName = "";
Decimal price = 0.00M;
String categoryName = "";
for (int i = 0; i < addProductDataGrid.Items.Count; i++)
{
TextBlock x = addProductDataGrid.Columns[0].GetCellContent(addProductDataGrid.Items[i]) as TextBlock;
if (x != null)
{
if (!x.Text.Equals(""))
{
// Console.WriteLine(x.GetType());
// Console.WriteLine("Product " + i + ": " + x.Text);
productName = x.Text;
}
else
{
break;
}
}
TextBlock y = addProductDataGrid.Columns[1].GetCellContent(addProductDataGrid.Items[i]) as TextBlock;
if (y != null)
{
if (!y.Text.Equals(""))
{
// Console.WriteLine("Price " + i + ": " + y.Text);
price = Convert.ToDecimal(y.Text);
}
else
{
break;
}
}
TextBlock z = addProductDataGrid.Columns[2].GetCellContent(addProductDataGrid.Items[i]) as TextBlock;
if (z != null)
{
if (!z.Text.Equals(""))
{
//Console.WriteLine("Category " + i + ": " + z.Text);
categoryName = z.Text;
}
else
{
break;
}
}
theSender.SendToDB(new product() { prodName = productName, prodPrice = price, prodCategoryName = categoryName });
}
foreach (product theT in db.products)
{
tempproduct.Add(theT);
}
productDataGrid.ItemsSource = tempproduct;
addProductDataGrid.ItemsSource = new BindingList<TestGridItem>();
}
}
private void cancelAddButton_Click(object sender, RoutedEventArgs e)
{
AddInput.Visibility = System.Windows.Visibility.Collapsed;
_currentDataGrid.Visibility = System.Windows.Visibility.Collapsed;
}
追加情報
public class TestGridItem
{
public string One { get; set; }
public string Two { get; set; }
public string Three { get; set; }
public string Four { get; set; }
}
addProductDataGrid.ItemsSource = new BindingList<TestGridItem>();