DataGrid or ListView???

  • Thread starter Thread starter Alex Yakhnin, eMVP
  • Start date Start date
I know this might sounded dumb to you guys, but my
question is: How do you populate a table into a DataGrid??
Which one is better to use, DataGrid or Listview?? I've
tried the following code using ListView, but it does not
recgonize my ListView for some reason, can someone help me
out here???

conn.open()
Dim reader As SqlCeDataReader

Dim cmd As New SqlCeCommand("Select ID, Name, " &
cboLesson.Text & "From Student Where ClassID= '" &
cboClass.Text & "'", conn)
reader = cmd.ExecuteReader
ListView1.Items.Clear()
ListView1.Columns.Add("StudentID", -1,
HorizontalAlignment.Center)
ListView1.Columns.Add("Name", 150,
HorizontalAlignment.Center)
ListView1.Columns.Add(cboLesson.Text, -1,
HorizontalAlignment.Center)

While reader.Read
Dim ls As New ListViewItem(reader.Item("ID").ToString)
ls.SubItems.Add(reader.Item("Name").ToString)
ls.SubItems.Add(reader.Item(cboLesson.Text).ToString)
ListView1.Items.Add(ls)
End While
conn.Close()
 
As to DataGrid, no comment.
As to ListView the following :
- you must tell what does (not) happen.
- try to fill the ListView wit a lot of entrys (Rows)
- does it look as if the rows are being filles (Scrollbar) but no Colums
are being shown?
- if yes (BTW no experince in VB, so all code is C#), try to recreate the
ListView complely


//--------------------------------------------------------------------------
---------------------
//-- Save Data created in
VS -------------------------------------------------------------------

//--------------------------------------------------------------------------
---------------------
Rectangle rt = listViewListen.Bounds;
Size sz_Size = listViewListen.Size;

//--------------------------------------------------------------------------
---------------------
// string s_Name = listViewListen.Name;
tabPageListen.Controls.Remove(listViewListen);
progessBarMainFrame.Value = 6;

//--------------------------------------------------------------------------
---------------------
//
// listViewListen
//
listViewListen = new System.Windows.Forms.ListView();
listViewListen.Bounds = rt;
// listViewListen.AllowColumnReorder = true;
// listViewListen.BorderStyle =
System.Windows.Forms.BorderStyle.FixedSingle;
// this.listViewListen.Dock = System.Windows.Forms.DockStyle.Fill;
listViewListen.FullRowSelect = true;
// listViewListen.GridLines = true;
listViewListen.HeaderStyle =
System.Windows.Forms.ColumnHeaderStyle.Clickable;
// listViewListen.MultiSelect = false;
listViewListen.Size = sz_Size;
// listViewListen.TabIndex = 0;
listViewListen.View = System.Windows.Forms.View.Details;
listViewListen.Font = new System.Drawing.Font("Comic Sans MS", 9.75F,
System.Drawing.FontStyle.Bold);
listViewListen.FullRowSelect = true;
listViewListen.SelectedIndexChanged += new
System.EventHandler(this.listView_SelectedIndexChanged);
listViewListen.ColumnClick += new
System.Windows.Forms.ColumnClickEventHandler(this.listView_ColumnClick);

This Method stopped the problens I had when (re)using a ListView to display
a different DataTable/View.
I have the feeling that this is a Framework/Framework.Compact problem with
ListViews and it would interest me to know if the use of this
logic with VB solves the same problem.
In my Database Applications I prefer to read in the whole Dataset with all
Tables.
I then create a DataView with the selected Data I want to show in the
ListView (with sorting) and fill it.

see : http://www.codeproject.com/useritems/Internationalization.asp for,
among otherthings, C# code how this done.

Hope this helps.

Mark Johnson, Berlin Germany
(e-mail address removed)
 
Back
Top