DataTable In Xamarin iOS

Introduction

Xamarin is a platform to develop cross-platform and multi-platform apps (for example, Windows phone, Android, iOS). In Xamarin platform, the code sharing concept is used. In Xamarin Studio, Visual Studio is also available.
Xamarin

Prerequisites
  • Xamarin Studio.
  • Xcode.
The steps given below are required to be followed in order to Store Values Using DataTable in Xamarin iOS, using Xamarin Studio.

Step 1

Go To Xamarin Studio.

Click New Solution—> select iOS—>select App--> Choose single View App. Afterwards, click Next.

Xamarin

Step 2

In this step, configure your app. Give the app name (Ex:sample), Organization Identifier. Afterwards, click Next.

Xamarin

Step 3

In this step, give your project name (Ex: Sample) and solution name (Ex: Sample). Give the path of your project. Afterwards, click Create.

Xamarin

Step 4

Subsequently, go to the solution. In the solution, you will get all the files and sources in your project. Now, select Main.storyboard and double click to open Main.storyboard page.

Xamarin

Step 5

After opening the Main.storyboard, you can design this page as per your desire.

Xamarin

Step 6

In this step, design your app using Storyboard, Toolbox, and Properties.
  1. TextField(txtValue1).
  2. TextField(txtValue2).
  3. Button(btnStore).
  4. Button(btnRetrive).
  5. Lable(lblValue1).
  6. Lable(lblValue2).

    Xamarin
Step 7

In this step, you need to add System.Data reference.

Go to Solution Explorer—>Reference—>Right Click—>Edit Reference—>Select System.Data.

Xamarin

Step 8

Go to Solution Explorer —> Reference —> Check System.Data.

Xamarin

Step 9

In this step, go to the ViewController.cs page. Write the code given below.

ViewController.cs
  1. using System;  
  2. using UIKit;  
  3. using System.Data;  
  4. namespace XamariniOSDataTable {  
  5.     public partial class ViewController: UIViewController {  
  6.         protected ViewController(IntPtr handle): base(handle) {  
  7.             // Note: this .ctor should not contain any initialization logic.  
  8.         }  
  9.         public override void ViewDidLoad() {  
  10.             base.ViewDidLoad();  
  11.         }  
  12.         partial void UIButton16_TouchUpInside(UIButton sender) {  
  13.             DataTable table = GetValue(txtValue1.Text, txtValue2.Text);  
  14.             foreach(DataRow row in table.Rows) {  
  15.                 lblValue1.Text = row[0].ToString();  
  16.                 lblValue2.Text = row[1].ToString();  
  17.             }  
  18.         }  
  19.         partial void UIButton11_TouchUpInside(UIButton sender) {  
  20.             DataTable table = GetValue(txtValue1.Text, txtValue2.Text);  
  21.             UIAlertView alert = new UIAlertView("Success""Data has been Stored Successfully."null"Close"null);  
  22.             alert.Show();  
  23.         }  
  24.         public DataTable GetValue(string v1, string v2) {  
  25.             DataTable table = new DataTable();  
  26.             table.Columns.Add("Value1"typeof(string));  
  27.             table.Columns.Add("Value2"typeof(string));  
  28.             table.Rows.Add(v1, v2);  
  29.             return table;  
  30.         }  
  31.         public override void DidReceiveMemoryWarning() {  
  32.             base.DidReceiveMemoryWarning();  
  33.             // Release any cached data, images, etc that aren't in use.  
  34.         }  
  35.     }  
  36. }  
Xamarin

Step 10

Now, go to Run option. Choose Debug and the list of available iPhone and iPad simulators will come up. You can choose any one simulator and run it.

Xamarin

Output

After a few seconds, the app will start running on your iPhone simulator. You will see your app working successfully.

Xamarin

Xamarin

Xamarin

You can store and retrieve the data.

Summary

This was the process of how to store values in Xamarin iOS using DataTable.