Showing posts with label list. Show all posts
Showing posts with label list. Show all posts

Mar 2, 2010

Populate list from untyped DataSet

A table in a dataset is used to populate a list box with employee name from the employee table. With the dataset the name of the table and field must be known at design time. if they are misspelled or mistyped an error will be generated only at runtime.

Define the dataset.

private DataSet dsEmp;


//Call this on your button click event
PopulateListFromDS();


private void PopulateListFromDS()

{

string s;

int i;

//Clear items in ListBox

lstResults.Items.Clear();



for(i=0; i <=dsEmp.Tables["EmplyeeDS"].Rows.Count - 1; i++)

{

// Check to see if row is flagged deleted.

if (dsEmp.Tables["EmplyeeDS"].Rows.RowState != DataRowState.Deleted)

{

// Get the product name for each record.

s = dsEmp.Tables["EmplyeeDS"].Rows["EmpName"].ToString();

// Add product name to the list box

lstResults.Items.Add(s);

}

}

}

Feb 26, 2010

Populate list from typed DataSet

private Employee tdsEmp;

//Call this on your button click event
PopulateListFromTDS();

A table in a typed dataset is used to populate a list box with employee name from the employee table. A typed dataset provides design time verification of table names and field names before the program is run.

private void PopulateListFromTDS()

{

string s;

int i;

//Clear Items in listBox

lstResults.Items.Clear();

for(i=0;i<=tdsEmp.EmpTDS.Count - 1;i++) //EmpTDS is table

{

// Check to see if row is flagged deleted.

if (tdsEmp.EmpTDS.RowState != DataRowState.Deleted)

{

s = tdsEmp.EmpTDS.empName.ToString();

lstResults.Items.Add(s);

}

}

}