Dynamic binding of gridview – List





Dynamic binding of grid view.


This is how it looks

It has a grid which displays first name and last name 2 textboxes are there which helps the user to fill the First name and Last name.
Implementation

 public List<Person> listPerson = new List<Person>();

We ‘ve created an instance of list.Here We use this list to save the class “Person”

Class”Person”

  public class Person
        {
            public string Name
            {
                get;
                set;
            }
            public string LastName
            {
                get;
                set;
            }
        }

We need to create 2 columns and name its headers and bind these columns with the data from the User.In the Xaml we need to bring modifications as shown below.
XAML

 <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=Name}" Width="100 ">
                        </GridViewColumn>
                        <GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding Path=LastName}" Width="100 ">
                        </GridViewColumn>

When we click on the button we need to fill the datagrid with the list
Button Click

private void button_Click(object sender, RoutedEventArgs e)
        {
            Person p = new Person();        
            p.Name = txtName.Text;
            p.LastName = txtLastName.Text;
            listPerson.Add(p);
            ListView1.ItemsSource = listPerson;
            ListView1.Items.Refresh();
        }

This is how the final UI looks like


HappyCoding ! 😉

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: