ListView Control drive me crazy!!

I

Islam Elkhayat

I need help plzzzz... In my windows application i fill listview from simple
access database...
here is the code

//Creating colums
InitializeComponent();
listView_result.Columns.Add("ID",87,HorizontalAlignment.Left);
listView_result.Columns.Add("Name",180,HorizontalAlignment.Left);
listView_result.Columns.Add("Cash",81,HorizontalAlignment.Left);


//Fill the listview
try
{
oleDBConn.open();
SelectCommand.CommandText = "SELECT Serial_no, Name, Cash from Cheque";
OleDbDataReader reader;
reader = SelectCommand.ExecuteReader();
listView_result.Clear();
while(reader.Read())
{
ListViewItem lvi= new ListViewItem(reader["Serial_no"].ToString());
lvi.SubItems.Add(reader["Name"].ToString());
lvi.SubItems.Add(reader["Cash"].ToString());
listView_result.Items.Add(lvi);
}
reader.Close();
oleDBConn.close();
} catch (Exception e) { MessageBox.Show(e.ToString()); }


When i click the fill button the column disappear and the control look like
it's freeze!!!
there is no problem in the connection string or the query, the while loop is
working but when it come to dispaly result the problem rise.
What's wrong... i need urgent help
 
B

Bruce Wood

I prefer to build my list view items this way... perhaps you will have
more success with this:

string[] columnItems = new string[] { new
ListViewItem(reader["Serial_no"].ToString(), reader["Name"].ToString(),
reader["Cash"].ToString() };
ListViewItem lvi = new ListViewItem(columnItems);
 
I

Islam Elkhayat

thanks alot.. when i create Columns collection visually and build my list
using ur array way it's work now great...

1-May i know what's wrong in my way??
2-How can i hide the ID field and use it to retrive the full data when user
bouble click a record??
thanx alot
 
I

Islam Elkhayat

I got the reason for the error.
In my code below i add columns b4 i fill the subitem..for some reason this
coz error, after i moved the "Creating columns" code below the while loop
it's now work perfect!!
I can't understand why this order make an error but at least i solved my
problem ^_*
 
B

Bruce Wood

You can point the ListViewItem directly at your data:

string[] columnItems = new string[] { reader["Serial_no"].ToString(),
reader["Name"].ToString(), reader["Cash"].ToString() };
ListViewItem lvi = new ListViewItem(columnItems);
lvi.Tag = myObject;
....then, when you want it back, just cast lvi.Tag to the correct type.
 
P

Picho

Hi,

Just read your post, I hope you still got it.

I think the problem is that you add the columns and then call
listView_resul.Clear().
you should use listView_result.Items.Clear() instead, since the first one
clears the column headers as well.

Picho

----- Original Message -----
From: "Islam Elkhayat" <[email protected]>
Newsgroups: microsoft.public.dotnet.languages.csharp
Sent: Thursday, January 27, 2005 3:07 AM
Subject: ListView Control drive me crazy!!
 
I

Islam Elkhayat

thanx u r right...
How could i load deffrent language using 2 button for the languages.. i use
thread.currentthread.cultureinfo
but it don't work!!


Picho said:
Hi,

Just read your post, I hope you still got it.

I think the problem is that you add the columns and then call
listView_resul.Clear().
you should use listView_result.Items.Clear() instead, since the first one
clears the column headers as well.

Picho

----- Original Message -----
From: "Islam Elkhayat" <[email protected]>
Newsgroups: microsoft.public.dotnet.languages.csharp
Sent: Thursday, January 27, 2005 3:07 AM
Subject: ListView Control drive me crazy!!

I need help plzzzz... In my windows application i fill listview from
simple access database...
here is the code

//Creating colums
InitializeComponent();
listView_result.Columns.Add("ID",87,HorizontalAlignment.Left);
listView_result.Columns.Add("Name",180,HorizontalAlignment.Left);
listView_result.Columns.Add("Cash",81,HorizontalAlignment.Left);


//Fill the listview
try
{
oleDBConn.open();
SelectCommand.CommandText = "SELECT Serial_no, Name, Cash from Cheque";
OleDbDataReader reader;
reader = SelectCommand.ExecuteReader();
listView_result.Clear();
while(reader.Read())
{
ListViewItem lvi= new ListViewItem(reader["Serial_no"].ToString());
lvi.SubItems.Add(reader["Name"].ToString());
lvi.SubItems.Add(reader["Cash"].ToString());
listView_result.Items.Add(lvi);
}
reader.Close();
oleDBConn.close();
} catch (Exception e) { MessageBox.Show(e.ToString()); }


When i click the fill button the column disappear and the control look
like it's freeze!!!
there is no problem in the connection string or the query, the while loop
is working but when it come to dispaly result the problem rise.
What's wrong... i need urgent help
 
P

Picho

not sure i understood, but if you want to select a language programaticly
(like pressing alt+shift):
enumerate all installed languages using the InputLanguage class in the
System.windows.Forms namespace.

foreach (InputLanguage lang in InputLanguage.InstalledInputLanguages)
{
string name = lang.Culture.DisplayName;

// set the input language:
InputLanguage.CurrentInputLanguage = lang;
}

HTH
Picho

Islam Elkhayat said:
thanx u r right...
How could i load deffrent language using 2 button for the languages.. i
use thread.currentthread.cultureinfo
but it don't work!!


Picho said:
Hi,

Just read your post, I hope you still got it.

I think the problem is that you add the columns and then call
listView_resul.Clear().
you should use listView_result.Items.Clear() instead, since the first one
clears the column headers as well.

Picho

----- Original Message -----
From: "Islam Elkhayat" <[email protected]>
Newsgroups: microsoft.public.dotnet.languages.csharp
Sent: Thursday, January 27, 2005 3:07 AM
Subject: ListView Control drive me crazy!!

I need help plzzzz... In my windows application i fill listview from
simple access database...
here is the code

//Creating colums
InitializeComponent();
listView_result.Columns.Add("ID",87,HorizontalAlignment.Left);
listView_result.Columns.Add("Name",180,HorizontalAlignment.Left);
listView_result.Columns.Add("Cash",81,HorizontalAlignment.Left);


//Fill the listview
try
{
oleDBConn.open();
SelectCommand.CommandText = "SELECT Serial_no, Name, Cash from Cheque";
OleDbDataReader reader;
reader = SelectCommand.ExecuteReader();
listView_result.Clear();
while(reader.Read())
{
ListViewItem lvi= new ListViewItem(reader["Serial_no"].ToString());
lvi.SubItems.Add(reader["Name"].ToString());
lvi.SubItems.Add(reader["Cash"].ToString());
listView_result.Items.Add(lvi);
}
reader.Close();
oleDBConn.close();
} catch (Exception e) { MessageBox.Show(e.ToString()); }


When i click the fill button the column disappear and the control look
like it's freeze!!!
there is no problem in the connection string or the query, the while
loop is working but when it come to dispaly result the problem rise.
What's wrong... i need urgent help
 
B

Bruce Wood

I think that the OP would like to know how to put two buttons on the
screen, or two menu choices. If the user clicks one button (or chooses
one menu item), the languages changes to Japanese (for example); if the
user clicks the other button (or chooses another menu item), the
language changes to English.

Something like that.
 
Top