listview columnwidth to largest subitem?

G

Guest

Hi,
I have a datareader filling a listview, after that I want to set my listview
column width to the largest subitem in the column. You could do that by
setting width to -1.

I already found out about a bug that you could not set -1 by the columns.add
method
(http://support.microsoft.com/default.aspx?scid=kb;en-us;316572)

But it won't work like they descripe there either.


CODE
private void Form1_Load(object sender, System.EventArgs e)
{ ListViewItem item = new ListViewItem();

listView1.View = View.Details;
item.Text = "This is Item 1";
item.SubItems.Add("This is subitem 1 for item 1");
item.SubItems.Add("This is subitem 2 for item 1");
listView1.Items.Add(item);

ListViewItem item1 = new ListViewItem();
item1.Text = "This is Item 2";
item1.SubItems.Add("This is subitem 1 for item 2");
item1.SubItems.Add("This is subitem 2 for item 2");
listView1.Items.Add(item1);
listView1.Columns.Add("Column 1",-1, HorizontalAlignment.Left);
listView1.Columns.Add("Column 2", -2, HorizontalAlignment.Left);
listView1.Columns.Add("Column 3", -1, HorizontalAlignment.Left);
listView1.Columns[0].Width = -1;

}
So in the add method it will not work but

listView1.Columns[0].Width = -1;

should set column[0].width to the largest subitem in the column ... but it
doesn't...
Can anyone tell me how I can solve this?

(it works fine on the .NET framework, not on the .NET compact framework)
 
S

Serg Kuryata [MS]

Hello,

The best approach for setting width of listview columns to -1 (or -2) is to
do this only after adding all columns and items to the listview control.
This is due to design specifics of the native listview control.

Could you please post a simple application that reproduces the problem? Do
you have the .NET Compact Framework Service Pack 2 installed?

Thank you,
Sergiy.

This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
| Thread-Topic: listview columnwidth to largest subitem?
| thread-index: AcSI3dUXPN1JQ/MUQjmHTAZ/Dp9bZQ==
| X-WBNR-Posting-Host: 194.78.205.198
| From: "=?Utf-8?B?RGF2eQ==?=" <[email protected]>
| Subject: listview columnwidth to largest subitem?
| Date: Sun, 22 Aug 2004 23:53:01 -0700
| Lines: 42
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Newsgroups: microsoft.public.dotnet.framework.compactframework
| NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.1.29
| Path: cpmsftngxa10.phx.gbl!TK2MSFTNGXA03.phx.gbl
| Xref: cpmsftngxa10.phx.gbl
microsoft.public.dotnet.framework.compactframework:59816
| X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework
|
| Hi,
| I have a datareader filling a listview, after that I want to set my
listview
| column width to the largest subitem in the column. You could do that by
| setting width to -1.
|
| I already found out about a bug that you could not set -1 by the
columns.add
| method
|
(http://support.microsoft.com/default.aspx?scid=kb;en-us;316572)
|
| But it won't work like they descripe there either.
|
|
| CODE
| private void Form1_Load(object sender, System.EventArgs e)
| { ListViewItem item = new ListViewItem();
|
| listView1.View = View.Details;
| item.Text = "This is Item 1";
| item.SubItems.Add("This is subitem 1 for item 1");
| item.SubItems.Add("This is subitem 2 for item 1");
| listView1.Items.Add(item);
|
| ListViewItem item1 = new ListViewItem();
| item1.Text = "This is Item 2";
| item1.SubItems.Add("This is subitem 1 for item 2");
| item1.SubItems.Add("This is subitem 2 for item 2");
| listView1.Items.Add(item1);
| listView1.Columns.Add("Column 1",-1,
HorizontalAlignment.Left);
| listView1.Columns.Add("Column 2", -2,
HorizontalAlignment.Left);
| listView1.Columns.Add("Column 3", -1,
HorizontalAlignment.Left);
| listView1.Columns[0].Width = -1;
|
| }
| So in the add method it will not work but
|
| listView1.Columns[0].Width = -1;
|
| should set column[0].width to the largest subitem in the column ... but
it
| doesn't...
| Can anyone tell me how I can solve this?
|
| (it works fine on the .NET framework, not on the .NET compact framework)
|
 
G

Guest

Hi,

I have the servicepack 2 installed and the -2 option does work well, here is
my code...

while (Zoek.dr.Read() == true)
{
ListViewItem product = new ListViewItem();
product.Text = Zoek.dr.GetValue(0).ToString();


for (int k = 1; k<i+1; k++)
{
product.SubItems.Add(Zoek.dr.GetValue(k).ToString());

}

lsvRes.Items.Add(product);
}
for (int j = 0; j < i+1; j++
{lsvRes.Columns.Add(Zoek.dr.GetName(j),lsvRes.Width/2,HorizontalAlignment.Left);
lsvRes.Columns[j].Width = -1;
}

Zoek.dr is the datareader I use to fill listviewitems. After that I fill in
the listview with these items and AFTER that I add the columns + widths. I
don't think there is anything wrong with my code because it works on the .NET
framework, it looks more like a malfunction of the .NET compact framework to
me


Serg Kuryata said:
Hello,

The best approach for setting width of listview columns to -1 (or -2) is to
do this only after adding all columns and items to the listview control.
This is due to design specifics of the native listview control.

Could you please post a simple application that reproduces the problem? Do
you have the .NET Compact Framework Service Pack 2 installed?

Thank you,
Sergiy.

This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
| Thread-Topic: listview columnwidth to largest subitem?
| thread-index: AcSI3dUXPN1JQ/MUQjmHTAZ/Dp9bZQ==
| X-WBNR-Posting-Host: 194.78.205.198
| From: "=?Utf-8?B?RGF2eQ==?=" <[email protected]>
| Subject: listview columnwidth to largest subitem?
| Date: Sun, 22 Aug 2004 23:53:01 -0700
| Lines: 42
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Newsgroups: microsoft.public.dotnet.framework.compactframework
| NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.1.29
| Path: cpmsftngxa10.phx.gbl!TK2MSFTNGXA03.phx.gbl
| Xref: cpmsftngxa10.phx.gbl
microsoft.public.dotnet.framework.compactframework:59816
| X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework
|
| Hi,
| I have a datareader filling a listview, after that I want to set my
listview
| column width to the largest subitem in the column. You could do that by
| setting width to -1.
|
| I already found out about a bug that you could not set -1 by the
columns.add
| method
|
(http://support.microsoft.com/default.aspx?scid=kb;en-us;316572)
|
| But it won't work like they descripe there either.
|
|
| CODE
| private void Form1_Load(object sender, System.EventArgs e)
| { ListViewItem item = new ListViewItem();
|
| listView1.View = View.Details;
| item.Text = "This is Item 1";
| item.SubItems.Add("This is subitem 1 for item 1");
| item.SubItems.Add("This is subitem 2 for item 1");
| listView1.Items.Add(item);
|
| ListViewItem item1 = new ListViewItem();
| item1.Text = "This is Item 2";
| item1.SubItems.Add("This is subitem 1 for item 2");
| item1.SubItems.Add("This is subitem 2 for item 2");
| listView1.Items.Add(item1);
| listView1.Columns.Add("Column 1",-1,
HorizontalAlignment.Left);
| listView1.Columns.Add("Column 2", -2,
HorizontalAlignment.Left);
| listView1.Columns.Add("Column 3", -1,
HorizontalAlignment.Left);
| listView1.Columns[0].Width = -1;
|
| }
| So in the add method it will not work but
|
| listView1.Columns[0].Width = -1;
|
| should set column[0].width to the largest subitem in the column ... but
it
| doesn't...
| Can anyone tell me how I can solve this?
|
| (it works fine on the .NET framework, not on the .NET compact framework)
|
 
G

Guest

Oops, here's the code again with a better layout, btw the first code I posted
is a good example application to show the problem...

while (Zoek.dr.Read() == true)
{
ListViewItem product = new ListViewItem();
product.Text = Zoek.dr.GetValue(0).ToString();
for (int k = 1; k<i+1; k++)
{
product.SubItems.Add(Zoek.dr.GetValue(k).ToString());
}
lsvRes.Items.Add(product);
}
for (int j = 0; j < i+1; j++)
{
lsvRes.Columns.Add(Zoek.dr.GetName(j),lsvRes.Width/2,HorizontalAlignment.Left);
lsvRes.Columns[j].Width = -1;
}

Thx
Davy
 
S

Serg Kuryata [MS]

Yes, this is a known issue. What I would recommend you to do is to try to
modify your code in the following way:

for (int j = 0; j < i+1; j++)
{

lsvRes.Columns.Add(Zoek.dr.GetName(j),lsvRes.Width/2,HorizontalAlignment.Lef
t);
lsvRes.Columns[j].Width = -1;
}


while (Zoek.dr.Read() == true)
{
ListViewItem product = new ListViewItem();
product.Text = Zoek.dr.GetValue(0).ToString();
for (int k = 1; k<i+1; k++)
{
product.SubItems.Add(Zoek.dr.GetValue(k).ToString());
}
lsvRes.Items.Add(product);
}

for (int j = 0; j < lsvRes.Columns.Count; j++)
lsvRes.Columns[j].Width = -1;


Please let me know if that works for you.

Thank you,
Sergiy.

This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
| Thread-Topic: listview columnwidth to largest subitem?
| thread-index: AcSKcHcBzpeJaTraSTu1hoKA9+7MFg==
| X-WBNR-Posting-Host: 194.78.205.198
| From: "=?Utf-8?B?RGF2eQ==?=" <[email protected]>
| References: <[email protected]>
<[email protected]>
<[email protected]>
| Subject: RE: listview columnwidth to largest subitem?
| Date: Tue, 24 Aug 2004 23:55:11 -0700
| Lines: 23
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Newsgroups: microsoft.public.dotnet.framework.compactframework
| NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.1.29
| Path: cpmsftngxa10.phx.gbl!TK2MSFTNGXA03.phx.gbl
| Xref: cpmsftngxa10.phx.gbl
microsoft.public.dotnet.framework.compactframework:59961
| X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework
|
| Oops, here's the code again with a better layout, btw the first code I
posted
| is a good example application to show the problem...
|
| while (Zoek.dr.Read() == true)
| {
| ListViewItem product = new ListViewItem();
| product.Text = Zoek.dr.GetValue(0).ToString();
| for (int k = 1; k<i+1; k++)
| {
| product.SubItems.Add(Zoek.dr.GetValue(k).ToString());
| }
| lsvRes.Items.Add(product);
| }
| for (int j = 0; j < i+1; j++)
| {
|
lsvRes.Columns.Add(Zoek.dr.GetName(j),lsvRes.Width/2,HorizontalAlignment.Lef
t);
| lsvRes.Columns[j].Width = -1;
| }
|
| Thx
| Davy
|
|
|
 

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