locked
Printing data in data grid RRS feed

  • Question

  • Hi,

      The input is being taken in a text box. After every input data is being retrieved from database. The new data retrieved should be displayed in a new row. The user can give inputs unless and until he/she clicks on the button. How should I implement this? 


    Tuesday, June 21, 2016 5:51 AM

Answers

  • I don't follow your description.

    You seem to be describing input of data in textboxes for a new record which is to be added to a database.

    I would expect something like:

    Bind the input text boxes to properties of an object which matches your table in the database.

    ( Preferably generated using entity framework ).

    Add that to an observablecollection the datagrid's itemssource is bound to.

    A new row will appear in the datagrid.

    You can also use whatever database update approach you prefer to add the record to the database.

    Or is this new record added by someone else?


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    • Marked as answer by DotNet Wang Sunday, July 10, 2016 8:52 AM
    Tuesday, June 21, 2016 9:55 AM
  • If you set the ItemsSource property of the DataGrid to an ObservableCollection<T>, you could add items to this source collection dynamically to add items to the DataGrid, for example by handling the TextChanged or LostFocus event of the TextBox. When the user clicks the Button, you could set a flag that prevents any more items from being added to the source collection. The following sample should give you the idea:

        public partial class MainWindow : Window
        {
            ObservableCollection<string> sourceCollection = new ObservableCollection<string>();
            bool start = true;
            public MainWindow()
            {
                InitializeComponent();
                dg.ItemsSource = sourceCollection;
            }
    
            private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
            {
                if(start)
                {
                    //get your data...
    
                    //add a new row:
                    sourceCollection.Add("your new item...");
                }
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                start = !start;
            }
        }
    

    <StackPanel>
            <TextBox TextChanged="TextBox_TextChanged" />
            <DataGrid x:Name="dg" AutoGenerateColumns="False">
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding}" />
                </DataGrid.Columns>
            </DataGrid>
            <Button Content="Start/Stop" Click="Button_Click"/>
        </StackPanel>

    Note that the forums are not for anyone to write an entire application or module for you though but the above sample code should hopefully get you started. Please start a new thread if you have a more specific questions about some issue you may run into.

    Hope that helps and good luck.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Marked as answer by DotNet Wang Sunday, July 10, 2016 8:52 AM
    Tuesday, June 21, 2016 6:37 PM

All replies

  • I don't follow your description.

    You seem to be describing input of data in textboxes for a new record which is to be added to a database.

    I would expect something like:

    Bind the input text boxes to properties of an object which matches your table in the database.

    ( Preferably generated using entity framework ).

    Add that to an observablecollection the datagrid's itemssource is bound to.

    A new row will appear in the datagrid.

    You can also use whatever database update approach you prefer to add the record to the database.

    Or is this new record added by someone else?


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    • Marked as answer by DotNet Wang Sunday, July 10, 2016 8:52 AM
    Tuesday, June 21, 2016 9:55 AM
  • If you set the ItemsSource property of the DataGrid to an ObservableCollection<T>, you could add items to this source collection dynamically to add items to the DataGrid, for example by handling the TextChanged or LostFocus event of the TextBox. When the user clicks the Button, you could set a flag that prevents any more items from being added to the source collection. The following sample should give you the idea:

        public partial class MainWindow : Window
        {
            ObservableCollection<string> sourceCollection = new ObservableCollection<string>();
            bool start = true;
            public MainWindow()
            {
                InitializeComponent();
                dg.ItemsSource = sourceCollection;
            }
    
            private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
            {
                if(start)
                {
                    //get your data...
    
                    //add a new row:
                    sourceCollection.Add("your new item...");
                }
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                start = !start;
            }
        }
    

    <StackPanel>
            <TextBox TextChanged="TextBox_TextChanged" />
            <DataGrid x:Name="dg" AutoGenerateColumns="False">
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding}" />
                </DataGrid.Columns>
            </DataGrid>
            <Button Content="Start/Stop" Click="Button_Click"/>
        </StackPanel>

    Note that the forums are not for anyone to write an entire application or module for you though but the above sample code should hopefully get you started. Please start a new thread if you have a more specific questions about some issue you may run into.

    Hope that helps and good luck.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Marked as answer by DotNet Wang Sunday, July 10, 2016 8:52 AM
    Tuesday, June 21, 2016 6:37 PM