Working With ListBox In Windows Phone (Edit Data In the ListBox) : Part 2

In this article we will discuss another example of working with a ListBox.

Here we can edit the Student Data. To begin that follow the Previous Article instructions.

Step 1: Here we continue the previous article in which we add and show the data in the ListBox like this:

windowsphone1.png

Step2: First we write the code to edit the Student Data. To do that we can use the DoubleTap Event of the ListBox (lstStudentName). So when we double-click on the Student Name of the ListBox the following code will be executed:

    private void lstStudentname_DoubleTap(object sender, GestureEventArgs e)
    {
     NavigationService.Navigate(new Uri(string.Format("/StudentDetails.xaml?parameter={0}&action={1}", a.ToString(), "Edit"), UriKind.Relative));
    }

Here we send the QueryString in which we send the student name and another value to edit. To fetch the Student Name from the ListBox we will write the following code:

    var listBoxItem = lstStudentname.ItemContainerGenerator.ContainerFromIndex(lstStudentname.SelectedIndex) as ListBoxItem;
    var txtBlk = FindVisualChildByType<TextBlock>(listBoxItem, "txtBlkExtra");
    a = txtBlk.Text;

After that:

        T FindVisualChildByType<T>(DependencyObject element, String name) where T : class
        {
            if (element is T && (element as FrameworkElement).Name == name)
                return element as T;
            int childcount = VisualTreeHelper.GetChildrenCount(element);
            for (int i = 0; i < childcount; i++)
            {
                T childElement = FindVisualChildByType<T>(VisualTreeHelper.GetChild(element, i), name);
                if (childElement != null)
                    return childElement;
            }
            return null;
        }

Step 3: After that we write the following code in the StudenDetails.cs Page, to fetch the QueryString data:

        private const string strConnectionString = @"isostore:/StudentDB.sdf";
        public string parameterValue;
        public string Action;
        public string id;
        public StudentDetails()
        {
            InitializeComponent();
            using (StudentdataContext context = new StudentdataContext(strConnectionString))
            {
                if (!context.DatabaseExists())
                {
                    context.CreateDatabase();
                }
            } 
        }
       
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        { 
            base.OnNavigatedTo(e);
            parameterValue = NavigationContext.QueryString["parameter"];
            Action = NavigationContext.QueryString["action"]; 
            using (StudentdataContext StudentDB = new StudentdataContext(strConnectionString))
            {
                var a = from b in StudentDB.GetTable<StudentInfo>() where b.Extra == parameterValue.ToString() select b;
                foreach (var x in a)
                {
                    txtfname.Text = x.FirstName;
                    txtlastName.Text = x.LastName;
                    txtMobile.Text = x.Mobile;
                    txtTelephone.Text = x.Telephone;
                    txtEmail.Text = x.Email;
                } 
                if (Action == "EditData")
                {
                    btnAdd.Content = "Edit";                  
                }
                if (Action == "Edit")
                {
                    btnAdd.Content = "Edit Data";                   
                }
            }
        }

Here we use the OnNavigatedTo Event to fetch the QueryString Data.

Step 4: Now we will write the following code in the Add Button. When we double-click on the listBox it will act as in the Edit Button:

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
             using ( StudentdataContext StudentDB = new StudentdataContext(strConnectionString))
             {
                 if (Action == "Edit" || Action == "EditData")
                 {
                     var Student = (from i in StudentDB.GetTable<StudentInfo>()
                                    where i.Extra == parameterValue.ToString()
                                    select i).Single(); 
                     Student.FirstName = txtfname.Text.ToString();
                     Student.LastName = txtlastName.Text.ToString();
                     Student.Extra = txtfname.Text.ToString() + " " + txtlastName.Text.ToString();
                     Student.Mobile = txtMobile.Text.ToString();
                     Student.Telephone = txtTelephone.Text.ToString();
                     Student.Email = txtEmail.Text.ToString();
                     StudentDB.SubmitChanges();
                 }
                
else
                 {                
                 StudentInfo newStudent = new
StudentInfo
                 {                    
                     Extra = txtfname.Text.ToString() + " " + txtlastName.Text.ToString(),
                     FirstName = txtfname.Text.ToString(),
                     LastName = txtlastName.Text.ToString(),
                     Mobile = txtMobile.Text.ToString(),
                     Telephone = txtTelephone.Text.ToString(),
                     Email = txtEmail.Text.ToString() 
                 };
                 var a = from b in StudentDB.GetTable<StudentInfo>() select b;
                 foreach (var x in a)
                 {
                     if (x.Extra == txtfname.Text.ToString() + " " + txtlastName.Text.ToString())
                     {
                         MessageBox.Show("This Name is already Exists");
                         txtfname.Text = "";
                         txtlastName.Text = "";
                         txtMobile.Text = "";
                         txtTelephone.Text = "";
                         txtEmail.Text = "";
                         id = x.Id;
                     }                    
                 }
                 if (txtfname.Text != "")
                 { 
                     StudentDB.Students.InsertOnSubmit(newStudent);
                     StudentDB.SubmitChanges();
                     MessageBox.Show("Add the Student");
                 }                        
             }               
            txtfname.Text = "";
            txtlastName.Text = "";
            txtMobile.Text = "";
            txtTelephone.Text = "";
            txtEmail.Text = "";           
      }

Now we add the Student:

windowsphone2.png

windowsphone3.png

windowsphone4.png

windowsphone5.png