Click here to Skip to main content
15,902,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Window have 2 text boxes ,one button and one Data grid view . when click button , data grid view need to display values of text box!

What I have tried:

private void btn_Click(object sender, RoutedEventArgs e)
{

DataTable dt = new DataTable();
dt.Columns.Add("id");
dt.Columns.Add("name");
DataRow dr = null;

if (dt.Rows.Count > 0)
{
dr = dt.NewRow();


dt.Rows.Add(txt1.Text, txt2.Text);
grid1.ItemsSource = dt.DefaultView;

}
}

In this case Data grid rows got updated , not adding values one by one!

Please anyone suggest a way to accomplish this in WPF!?

Thanks in Advance!
Posted
Updated 7-Sep-17 0:39am

Hello,
Every time you are generating new object of DataTable in button click and then checking whether it contains any row or not . Obviously it does not contain any row. That's Why data is not showing .
First declare the Table globally means out side of button Click and define its columns
In button click event change the code by this way
DataRow dr =dt.NewRow();
dt.Rows.Add(txt1.Text, txt2.Text);
if (dt.Rows.Count > 0)
{
  grid1.ItemsSource =null;  //null the ItemSource just before you set the new value: 
  grid1.ItemsSource = dt.DefaultView;
}

Thanks
 
Share this answer
 
<big><small></small></big>
 
Share this answer
 
Comments
Graeme_Grant 7-Sep-17 7:20am    
This question is 12 months old. Do you think that he is still waiting for your 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