Filling a ListView from a SQLCE Datareader

Z

Zac Maclean

I have a working version of this in VB, tryign to translate everything over
to C#.
In the C# version this is filling the listview, the first couple columns are
ok, but the last few are going screwy.
In VB, it is working perfectly.

The reason for move is speed. Takes a full 10 - 15 seconds to run this on
VB, <5 in C#.

I adapted this from
http://www.codeguru.com/vb/controls/vbnet_controls/listview/article.php/c3979
to allow for non-string fields to be used.

My VB:
#Region "Listview Data Adapter Use: classvariable.FillListView(ListView,
SQLCEDataSource)"
Public Sub FillListView(ByRef MyListView As ListView, ByRef myData As
SqlCeDataReader)
Dim lvwColumn As ColumnHeader
Dim itmListItem As ListViewItem
Dim strTest As String
Dim strTemp As String
Dim shtCntr As Short
MyListView.Clear()
For shtCntr = 0 To myData.FieldCount() - 1
lvwColumn = New ColumnHeader
lvwColumn.Text = myData.GetName(shtCntr)
MyListView.Columns.Add(lvwColumn)
Next
While myData.Read
itmListItem = New ListViewItem
strTest = IIf(myData.IsDBNull(0), "", myData.Item(0))
itmListItem.Text = strTest
For shtCntr = 1 To myData.FieldCount() - 1
If myData.IsDBNull(shtCntr) Then
itmListItem.SubItems.Add("")
Else
strTemp = myData.Item(shtCntr)
itmListItem.SubItems.Add(strTemp)
End If
Next shtCntr
MyListView.Items.Add(itmListItem)
End While
End Sub


My C#:

#region "Listview Data Adapter Use: classvariable.FillListView(ListView,
SQLCEDataSource)"
public void FillListView(ListView m_ListView, SqlCeDataReader myData)
{
ColumnHeader lvwColumn = new ColumnHeader();
m_ListView.Clear();
for (int acounter = 0; acounter <= (myData.FieldCount - 1);
acounter++)
{
lvwColumn = new ColumnHeader();
lvwColumn.Text = myData.GetName(acounter);
m_ListView.Columns.Add(lvwColumn);
} // End: for (int acounter = 0; acounter <=
(myData.FieldCount - 1); acounter++)
while (myData.Read())
{
string strTest;
if (myData.IsDBNull(0))
strTest = "";
else
strTest = myData.GetString(0);
ListViewItem itmListItem = new ListViewItem(strTest);
for (int bcounter = 1; bcounter <= (myData.FieldCount - 1);
bcounter++)
{
string strTemp = "";
if (myData.IsDBNull(bcounter))
itmListItem.SubItems.Add("");
else
strTemp =
Convert.ToString(myData.GetValue(bcounter));
itmListItem.SubItems.Add(strTemp);
} // End: for (int bcounter = 1; bcounter <=
(myData.FieldCount - 1); bcounter++)
m_ListView.Items.Add(itmListItem);
}// End: while (myData.Read())
}// End: public void FillListView
#endregion
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top