Click here to Skip to main content
15,908,841 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my Application has two windows . Main Window contains one Data Grid view , and one Button (Add). when click button it opens another window and it contains 2 text boxes, and button.

On the window 2 , when click button, text box values need to send and display to the Main window DataGrid!

This is the 2 files!

C#
public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btn_Add_Click(object sender, RoutedEventArgs e)
        {
            Window1 win = new Window1(this);
            win.Show();
        }

    }



window1.cs
C#
 public Window1()
            {
                InitializeComponent();     
            }
            private MainWindow m = null;
            public Window1(Window callingFrom)
            {
                m = callingFrom as MainWindow;
                InitializeComponent();
                DataTable dt = new DataTable();
                dt.Columns.Add("Name");
                dt.Columns.Add("ID");
                DataRow dr = dt.NewRow();
                m.dataGrid1.ItemsSource = dt.DefaultView;
                m.dataGrid1.UpdateLayout();
            }
      private void btn_Click(object sender, RoutedEventArgs e)
            {
                DataView dv = m.dataGrid1.ItemsSource as DataView;

                DataTable dt = dv.Table;
                DataRow dr = dt.NewRow();
                dr["Name"] = txt1.Text;
                dr["ID"] = txt2.Text;
                dt.Rows.Add(dr);
               // this.Close();
                m.dataGrid1.UpdateLayout();
            }

}


What I have tried:

The problem is when close the window1 and again open the window1 to add values to Data grid view, Main window's datagrid view got replaced insted of adding values! (It is updating values one by one up to close the window 1 )

How can resolve this!

Thank you!
Posted
Updated 5-Aug-16 2:41am

1 solution

As I understand, problem is through window1, you are updating datagrid of mainwindow; and after closing window1 and opening it again your datagrid is got cleared.

As it is correct, so it is happen because of

DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("ID");
DataRow dr = dt.NewRow();
m.dataGrid1.ItemsSource = dt.DefaultView;


in this line you are creating new datatable and adding as itemsource to datagrid.

So solution is, send existing itemsource as datatable and update it in window1 and it will reflect.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900