Gayashan
Gayashan

Reputation: 351

Add some data to listview or datagrid and save those data in database?

When i click add button i want to add combobox item and testbox text into below datagrid or ListView, and then finally i clicked save button i want to save them in my database table?

how do i add values and save them? Sample Interface Image looks like: enter image description here

Upvotes: 1

Views: 599

Answers (2)

Karl_Schuhmann
Karl_Schuhmann

Reputation: 1332

When i click add button i want to add combobox item and testbox text into below datagrid or ListView,

U can use this example code for this:

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" />

I hope, that I can help you.

Upvotes: 1

Denys Denysenko
Denys Denysenko

Reputation: 7894

This is a complex question and I guess there is no single right answer to it. I'll advice you to start with this lab. Also there you can find a nice jumpstart instruction on data access.

Upvotes: 1

Related Questions