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>();
4

0 回答 0