Ownerdrawing only the first column of a ListView

S

Sin Jeong-hun

What I am trying to do is quite simple (at least I thought so). I want
to render the first column by myself, and all other parts to be drawn
normally.

For example, I just wanted to draw the ID column by myself, and Col 1,
Col 2 normally (default drawing)
---------------------------
| ID | Col 1 | Col 2 | <-Column headers
---------------------------
| 1 | a | b |
| 2 | | |
| 3 | a | |
| 4 | a | b |
--------------------------

I wanted make it a class so the code was like:
protected override void
OnDrawColumnHeader(DrawListViewColumnHeaderEventArgs e)
{
e.DrawDefault = true;
}

protected override void
OnDrawSubItem(DrawListViewSubItemEventArgs e)
{
if (e.ColumnIndex ==0 )
{
e.Graphics.FillRectangle(new LinearGradientBrush(new
Point(0, 0), new Point(5, 5), Color.Red, Color.White), e.Bounds);
e.DrawText();
}
else
{
e.DrawDefault = true;
}
}

It didn't work! For each item, e.ColumIndex == 0 was called twice, and
I don't know why. and the code above also fills Col1 and Col2 of ID 2.
Those columns are null (no items). I just want to draw the first
column myself and leave all others alone, I don't know why that
seemingly simple task is so difficult. After spending a lot of hours
searching Google and writing and scratching codes, I came here to ask
for some help.
 
P

Peter Duniho

Sin said:
What I am trying to do is quite simple (at least I thought so). I want
to render the first column by myself, and all other parts to be drawn
normally.

[...]
I wanted make it a class so the code was like:
protected override void
OnDrawColumnHeader(DrawListViewColumnHeaderEventArgs e)
{
e.DrawDefault = true;
}

protected override void
OnDrawSubItem(DrawListViewSubItemEventArgs e)
{
if (e.ColumnIndex ==0 )
{
e.Graphics.FillRectangle(new LinearGradientBrush(new
Point(0, 0), new Point(5, 5), Color.Red, Color.White), e.Bounds);
e.DrawText();
}
else
{
e.DrawDefault = true;
}
}

Neither of those overrides calls the base implementation. I'm surprised
you get any sort of useful results at all, assuming that's in fact the
code you really have.

If it's not the code you really have, then that just reinforces the
point I always make: you need to post a concise-but-complete code
example that reliably demonstrates the problem.

I don't know for sure that someone will have an easy answer for you.
But for sure, if you don't post a sufficiently detailed question, the
odds of getting the help you want are greatly reduced.

Pete
 
V

vanderghast

Be sure to have

this.listView1.OwnerDraw = true;

And I chose the following code, to paint the data in the first column, in
red:
---------------------------
private void listView1_DrawItem(object sender,
DrawListViewItemEventArgs e)
{
// item == first column
e.DrawBackground();
using (Brush b = new SolidBrush(Color.Red))
{
e.Graphics.DrawString(listView1.Items[e.ItemIndex].Text,
listView1.Font, b, e.Bounds);
}
}

private void listView1_DrawSubItem(object sender,
DrawListViewSubItemEventArgs e)
{
// don't draw the item (column 0) again...
if (e.ColumnIndex > 0) e.DrawDefault = true;
}

private void listView1_DrawColumnHeader(object sender,
DrawListViewColumnHeaderEventArgs e)
{
e.DrawDefault = true;
}
 

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