to bind or not to bind that is the question?

  • Thread starter Thread starter Wiredless
  • Start date Start date
W

Wiredless

Hi,

What are the pros and cons to data binding?

I will have tables (and datagrid or listviews) that may need to load/display
data in the 10,000 records 40 fields range.

I have seen post where it took 8+ seconds to load 1200 records of 30 fields.
using bound controls.

so

Would it be faster not to bind the controls and just load them directly from
the database?

so

What are the pros and cons to data binding?

Thanks
 
I think you answered your own question.

Binding is slower than the manual process. The obvious advantage is less
code.

I will not go into the usual discussion on "why would you ever want to load
over 1000 on a small display".

Finally, you mentioned ListViews which don't support databinding so the
decision there is made for you.

Cheers
Daniel
 
Why don't you see for yourself? Just compile simple test below and run it.

It uses NorthwindDemo.sdf database which comes with VS. Add it to your
project and set action to "Content" so VS would deploy it.

First run involves JITing, so disregard it.



using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlServerCe;
using System.IO;
using System.Reflection;

namespace lvtest
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Button button6;
private System.Windows.Forms.Button button7;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.MainMenu mainMenu1;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.listView1 = new System.Windows.Forms.ListView();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button4 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.listBox1 = new System.Windows.Forms.ListBox();
this.button6 = new System.Windows.Forms.Button();
this.button7 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
//
// mainMenu1
//
this.mainMenu1.MenuItems.Add(this.menuItem1);
//
// menuItem1
//
this.menuItem1.Text = "Quit";
this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
//
// listView1
//
this.listView1.FullRowSelect = true;
this.listView1.Location = new System.Drawing.Point(8, 8);
this.listView1.Size = new System.Drawing.Size(224, 168);
this.listView1.View = System.Windows.Forms.View.Details;
//
// dataGrid1
//
this.dataGrid1.Location = new System.Drawing.Point(8, 8);
this.dataGrid1.Size = new System.Drawing.Size(224, 168);
this.dataGrid1.Text = "dataGrid1";
//
// button1
//
this.button1.Location = new System.Drawing.Point(8, 192);
this.button1.Size = new System.Drawing.Size(64, 20);
this.button1.Text = "Listview";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(88, 192);
this.button2.Size = new System.Drawing.Size(64, 20);
this.button2.Text = "Grid";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(8, 240);
this.textBox1.Size = new System.Drawing.Size(144, 22);
this.textBox1.Text = "Click button to start..";
//
// button4
//
this.button4.Location = new System.Drawing.Point(88, 216);
this.button4.Size = new System.Drawing.Size(64, 20);
this.button4.Text = "Reader";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// button5
//
this.button5.Location = new System.Drawing.Point(8, 216);
this.button5.Size = new System.Drawing.Size(64, 20);
this.button5.Text = "Fill";
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// listBox1
//
this.listBox1.Location = new System.Drawing.Point(8, 8);
this.listBox1.Size = new System.Drawing.Size(224, 170);
//
// button6
//
this.button6.Location = new System.Drawing.Point(168, 192);
this.button6.Size = new System.Drawing.Size(64, 20);
this.button6.Text = "ListBox";
this.button6.Click += new System.EventHandler(this.button6_Click);
//
// button7
//
this.button7.Location = new System.Drawing.Point(168, 216);
this.button7.Size = new System.Drawing.Size(64, 20);
this.button7.Text = "LB<>DS";
this.button7.Click += new System.EventHandler(this.button7_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(168, 240);
this.button3.Size = new System.Drawing.Size(64, 20);
this.button3.Text = "LB<>AR";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// Form1
//
this.Controls.Add(this.button3);
this.Controls.Add(this.button7);
this.Controls.Add(this.button6);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.button5);
this.Controls.Add(this.button4);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.dataGrid1);
this.Controls.Add(this.listView1);
this.Menu = this.mainMenu1;
this.Text = "Form1";

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>

static void Main()
{
Application.Run(new Form1());
}

private void chooseControl ( Control activeControl )
{
this.listView1.Hide();
this.dataGrid1.Hide();
this.listBox1.Hide();

activeControl.BringToFront();
activeControl.Show();
activeControl.Invalidate();
Application.DoEvents();
}

private void button1_Click(object sender, System.EventArgs e)
{
try
{
this.textBox1.Text = "Testing listview...";

this.listView1.Clear();
this.listView1.DataBindings.Clear();

this.chooseControl(this.listView1);

string path =
Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);

SqlCeConnection con = new
SqlCeConnection(String.Format("DataSource={0}\\NorthwindDemo.sdf", path));

con.Open();

SqlCeCommand cmd = con.CreateCommand();

cmd.CommandText = "Customers";

cmd.CommandType = CommandType.TableDirect;

int ticks = System.Environment.TickCount; // Start timer

SqlCeDataReader r = cmd.ExecuteReader(); // Get reader

this.listView1.BeginUpdate();

for (int i = 0; i < r.FieldCount; i++ ) // Set up columns
{
this.listView1.Columns.Add(r.GetName(i), -2,
HorizontalAlignment.Center);
}

string[] columns = new string[r.FieldCount];

while (r.Read())
{
for (int i = 0; i < r.FieldCount; i++ ) // Load data from the reader
{
columns = r.ToString();
}

this.listView1.Items.Add(new ListViewItem(columns));
// And populate ListView with it.
}

this.listView1.EndUpdate();

ticks = System.Environment.TickCount - ticks;
// Stop timer

r.Close();
cmd.Dispose();
con.Close();

this.textBox1.Text = String.Format("{0} rows in {1} ms",
this.listView1.Items.Count, ticks);

}
catch (Exception ex)
{
this.textBox1.Text = String.Format ("Exception: {0}", ex);
}
}

private void button2_Click(object sender, System.EventArgs e)
{
try
{
this.textBox1.Text = "Testing datagrid...";

this.dataGrid1.DataSource = null;

this.chooseControl(this.dataGrid1);

string path =
Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);

SqlCeConnection con = new
SqlCeConnection(String.Format("DataSource={0}\\NorthwindDemo.sdf", path));

con.Open();

SqlCeCommand cmd = con.CreateCommand();

cmd.CommandText = "Customers";

cmd.CommandType = CommandType.TableDirect;

SqlCeDataAdapter da = new SqlCeDataAdapter(cmd);

DataTable table = new DataTable();

int ticks = System.Environment.TickCount; // Start timer

da.Fill(table); // Get data

this.dataGrid1.DataSource = table; // Show data

ticks = System.Environment.TickCount - ticks;
// Stop timer

da.Dispose();
cmd.Dispose();
con.Close();

this.textBox1.Text = String.Format("{0} rows in {1} ms",
table.Rows.Count, ticks);

}
catch (Exception ex)
{
this.textBox1.Text = String.Format ("Exception: {0}", ex);
}
}

private void button5_Click(object sender, System.EventArgs e)
{
try
{
this.textBox1.Text = "Testing Fill()...";

string path =
Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);

SqlCeConnection con = new
SqlCeConnection(String.Format("DataSource={0}\\NorthwindDemo.sdf", path));

con.Open();

SqlCeCommand cmd = con.CreateCommand();

cmd.CommandText = "Customers";

cmd.CommandType = CommandType.TableDirect;

SqlCeDataAdapter da = new SqlCeDataAdapter(cmd);

DataTable table = new DataTable();

int ticks = System.Environment.TickCount; // Start timer

da.Fill(table); // Get data

ticks = System.Environment.TickCount - ticks;
// Stop timer

da.Dispose();
cmd.Dispose();
con.Close();

this.textBox1.Text = String.Format("{0} rows in {1} ms",
table.Rows.Count, ticks);

}
catch (Exception ex)
{
this.textBox1.Text = String.Format ("Exception: {0}", ex);
}
}

private void button4_Click(object sender, System.EventArgs e)
{
try {

this.textBox1.Text = "Testing Reader...";

string path =
Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);

SqlCeConnection con = new
SqlCeConnection(String.Format("DataSource={0}\\NorthwindDemo.sdf", path));

con.Open();

SqlCeCommand cmd = con.CreateCommand();

cmd.CommandText = "Customers";

cmd.CommandType = CommandType.TableDirect;

int ticks = System.Environment.TickCount; // Start timer

SqlCeDataReader r = cmd.ExecuteReader(); // Get reader

object dummy;
int rows = 0;

for (int i = 0; i < r.FieldCount; i++ ) // Set up columns
{
dummy = r.GetName(i);
}

while (r.Read())
{
for (int i = 0; i < r.FieldCount; i++ ) // Load data from the reader
{
dummy = r;
}
rows++;
}

ticks = System.Environment.TickCount - ticks;
// Stop timer

r.Close();
cmd.Dispose();
con.Close();

this.textBox1.Text = String.Format("{0} rows in {1} ms", rows, ticks);
}
catch (Exception ex)
{
this.textBox1.Text = String.Format ("Exception: {0}", ex);
}
}


private void button6_Click(object sender, System.EventArgs e)
{
try
{
this.textBox1.Text = "Testing ListBox direct...";

this.listBox1.DataSource = null;
this.listBox1.DisplayMember = null;
this.listBox1.DataBindings.Clear();
this.listBox1.Items.Clear();
this.chooseControl(this.listBox1);

string path =
Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);

SqlCeConnection con = new
SqlCeConnection(String.Format("DataSource={0}\\NorthwindDemo.sdf", path));

con.Open();

SqlCeCommand cmd = con.CreateCommand();

cmd.CommandText = "Customers";

cmd.CommandType = CommandType.TableDirect;

int ticks = System.Environment.TickCount; // Start timer

SqlCeDataReader r = cmd.ExecuteReader(); // Get reader

object dummy;
int rows = 0;

for (int i = 0; i < r.FieldCount; i++ ) // Set up columns
{
dummy = r.GetName(i);
}

while (r.Read())
{
this.listBox1.Items.Add(r["Address"]);

for (int i = 0; i < r.FieldCount; i++ ) // Load data from the reader
{
dummy = r;
}
rows++;
}

ticks = System.Environment.TickCount - ticks;
// Stop timer

r.Close();
cmd.Dispose();
con.Close();

this.textBox1.Text = String.Format("{0} rows in {1} ms", rows, ticks);
}
catch (Exception ex)
{
this.textBox1.Text = String.Format ("Exception: {0}", ex);
}
}

private void button7_Click(object sender, System.EventArgs e)
{
try
{
this.textBox1.Text = "Testing ListBox binding with DataSet...";

this.listBox1.DataSource = null;
this.listBox1.DisplayMember = null;
this.listBox1.DataBindings.Clear();
this.listBox1.Items.Clear();
this.chooseControl(this.listBox1);

string path =
Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);

SqlCeConnection con = new
SqlCeConnection(String.Format("DataSource={0}\\NorthwindDemo.sdf", path));

con.Open();

SqlCeCommand cmd = con.CreateCommand();

cmd.CommandText = "Customers";

cmd.CommandType = CommandType.TableDirect;

SqlCeDataAdapter da = new SqlCeDataAdapter(cmd);

DataTable table = new DataTable();

int ticks = System.Environment.TickCount; // Start timer

da.Fill(table); // Get data

this.listBox1.DisplayMember = "Address";
this.listBox1.DataSource = table;

ticks = System.Environment.TickCount - ticks;
// Stop timer

da.Dispose();
cmd.Dispose();
con.Close();

this.textBox1.Text = String.Format("{0} rows in {1} ms",
table.Rows.Count, ticks);

}
catch (Exception ex)
{
this.textBox1.Text = String.Format ("Exception: {0}", ex);
}
}

private void menuItem1_Click(object sender, System.EventArgs e)
{
this.Close();
}

public class Customer
{
private string _customerID;
private int _s_Generation;
private Byte[] _s_RowLineage;
private string _CompanyName;
private string _ContactName;
private string _ContactTitle;
private string _Address;
private string _City;
private string _Region;
private string _PostalCode;
private string _Country;
private string _Phone;
private string _Fax;
private Guid _rowguid;

public Customer() {}

public Customer(SqlCeDataReader r)
{
if (!r.IsDBNull(0)) this.customerID = r.GetString(0);
if (!r.IsDBNull(1)) this.s_Generation = r.GetInt32(1);

if (!r.IsDBNull(2))
{
this.s_RowLineage = new Byte[r.GetBytes(2, 0, null, 0, 0)];
r.GetBytes(2, 0, this.s_RowLineage, 0, this.s_RowLineage.Length);
}

if (!r.IsDBNull(3)) this.CompanyName = r.GetString(3);
if (!r.IsDBNull(4)) this.ContactName = r.GetString(4);
if (!r.IsDBNull(5)) this.ContactTitle = r.GetString(5);
if (!r.IsDBNull(6)) this.Address = r.GetString(6);
if (!r.IsDBNull(7)) this.City = r.GetString(7);
if (!r.IsDBNull(8)) this.Region = r.GetString(8);
if (!r.IsDBNull(9)) this.PostalCode = r.GetString(9);
if (!r.IsDBNull(10)) this.Country = r.GetString(10);
if (!r.IsDBNull(11)) this.Phone = r.GetString(11);
if (!r.IsDBNull(12)) this.Fax = r.GetString(12);
if (!r.IsDBNull(13)) this.rowguid = r.GetGuid(13);
}


public string customerID { get{ return _customerID; } set {
_customerID = value;} }
public int s_Generation { get{ return _s_Generation;} set {
_s_Generation = value;} }
public Byte[] s_RowLineage { get{ return _s_RowLineage;} set {
_s_RowLineage = value;} }
public string CompanyName { get{ return _CompanyName; } set {
_CompanyName = value;} }
public string ContactName { get{ return _ContactName; } set {
_ContactName = value;} }
public string ContactTitle { get{ return _ContactTitle;} set {
_ContactTitle = value;} }
public string Address { get{ return _Address; } set {
_Address = value;} }
public string City { get{ return _City; } set {
_City = value;} }
public string Region { get{ return _Region; } set {
_Region = value;} }
public string PostalCode { get{ return _PostalCode; } set {
_PostalCode = value;} }
public string Country { get{ return _Country; } set {
_Country = value;} }
public string Phone { get{ return _Phone; } set {
_Phone = value;} }
public string Fax { get{ return _Fax; } set {
_Fax = value;} }
public Guid rowguid { get{ return _rowguid; } set {
_rowguid = value;} }


}


private void button3_Click(object sender, System.EventArgs e)
{
try {
this.textBox1.Text = "Testing ListBox binding with ArrayList...";

this.listBox1.DataSource = null;
this.listBox1.DisplayMember = null;
this.listBox1.DataBindings.Clear();
this.listBox1.Items.Clear();
this.chooseControl(this.listBox1);

ArrayList customers = new ArrayList();


string path =
Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);

SqlCeConnection con = new
SqlCeConnection(String.Format("DataSource={0}\\NorthwindDemo.sdf", path));

con.Open();

SqlCeCommand cmd = con.CreateCommand();

cmd.CommandText = "Customers";

cmd.CommandType = CommandType.TableDirect;

int ticks = System.Environment.TickCount; // Start timer

SqlCeDataReader r = cmd.ExecuteReader(); // Get reader

object dummy;

for (int i = 0; i < r.FieldCount; i++ ) // Set up columns
{
dummy = r.GetName(i);
}

while (r.Read())
{
customers.Add(new Customer(r));
}


this.listBox1.DisplayMember = "Address";
this.listBox1.DataSource = customers;

ticks = System.Environment.TickCount - ticks;
// Stop timer

r.Dispose();
cmd.Dispose();
con.Close();

this.textBox1.Text = String.Format("{0} rows in {1} ms",
customers.Count, ticks);

}
catch (Exception ex)
{
this.textBox1.Text = String.Format ("Exception: {0}", ex);
}
}
}
}


Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.

*** Want to find answers instantly? Here's how... ***

1. Go to
http://groups-beta.google.com/group/microsoft.public.dotnet.framework.compactframework?hl=en
2. Type your question in the text box near "Search this group" button.
3. Hit "Search this group" button.
4. Read answer(s).
 
Back
Top