【问题标题】:Add some data to listview or datagrid and save those data in database?将一些数据添加到列表视图或数据网格并将这些数据保存在数据库中?
【发布时间】:2013-03-26 14:37:43
【问题描述】:

当我点击添加按钮时,我想将combobox 项目和测试框文本添加到datagrid 或ListView 下方,最后我点击save button 我想save them in my database 表?

如何添加值并保存它们?示例界面图像如下所示:

【问题讨论】:

  • 你使用哪个数据库?

标签: c# wpf entity-framework-4


【解决方案1】:

这是一个复杂的问题,我想没有一个正确的答案。我会建议你start with this lab。 另外there 你可以找到一个关于数据访问的不错的快速入门指令。

【讨论】:

  • 如果你看到我附加的图片,你会明白我在说什么。我不认为它很复杂。请任何人都可以给我一个答案?
【解决方案2】:

当我单击添加按钮时,我想将组合框项和测试框文本添加到数据网格或 ListView 下方,

你可以使用这个示例代码:

C#

public ObservableCollection<Model> ModelListe { get; set; }

    public MainWindow()
    {
        InitializeComponent();

        comboBox1.Items.Add("Test1");
        comboBox1.Items.Add("Test2");
        comboBox1.Items.Add("Test3");
        comboBox1.Items.Add("Test4");
        comboBox1.Items.Add("Test5");



        ModelListe = new ObservableCollection<Model>();
        DataContext = this;
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        if (textBox1.Text != "")
        {
            if (comboBox1.SelectedItem != null)
            {
                Model model = new Model();
                model.Component = comboBox1.SelectedItem.ToString();
                model.Question = textBox1.Text;

                ModelListe.Add(model);
            }
        }
    }

public class Model
{
    public string Component { get; set; }
    public string Question { get; set; }
}

XAML

    <ComboBox Height="29" HorizontalAlignment="Left" Margin="12,14,0,0" Name="comboBox1" VerticalAlignment="Top" Width="199" />
    <TextBox Height="29" HorizontalAlignment="Left" Margin="12,53,0,0" Name="textBox1" VerticalAlignment="Top" Width="199" />
    <DataGrid ItemsSource="{Binding ModelListe}" AutoGenerateColumns="False" Height="97" HorizontalAlignment="Left" Margin="12,92,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="199" >
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Component}" CanUserReorder="False" Header="Component" Width="100" />
            <DataGridTextColumn Binding="{Binding Question}" CanUserReorder="False" Header="Question" Width="100" />
        </DataGrid.Columns>
    </DataGrid>
    <Button Content="Add" Height="29" HorizontalAlignment="Left" Margin="214,53,0,0" Name="button1" VerticalAlignment="Top" Width="41" Click="button1_Click" />

希望能帮到你。

【讨论】:

  • 非常感谢您的回答对我帮助很大,还有一件事,当我单击保存按钮时,我需要将所有数据保存在我的实体模型数据库中。因此,我必须使用 for 循环数据网格中的每个记录。如果可以请告诉我如何使用 for-each 循环读取每一行单元格的值???
猜你喜欢
  • 2015-09-22
  • 1970-01-01
  • 2011-12-04
  • 1970-01-01
  • 2014-02-17
  • 2016-06-18
  • 2014-09-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多