DataGridView - Differnt Column Widths

G

Guest

I am trying to do a 'simple' task in a DGV control.
The problem is this.
I have an unbound DGV with all 'AllowUser' options disabled.
RowHeader.Visible=False
ColumnHeader.Text is labelled as needed.
For simplicity the DGV has 7 columns and 15 rows.
The 15 rows are populated and the DGV is made visible.
Everything is just fine.
But now I want rows 6,7 and 11,12 to have different column spacing to the
other rows.
I have not been able to find a way.
I tried hanlding the DGV.CellFormatting Event but cant get the desired
results.
In other words I want one DGV control to have different column widths
depending on row number?

Can anyone out there help?

Thanks
 
R

RobinS

You can't do this with the DataGridView control.

And please don't multi-post. If you want to post the same
message to more than one group, please post them all at
the same time, so if someone in one group provides a
solution, people in the other groups can see it and choose
to help someone else.

Thanks.
Robin S.
 
G

Guest

You can set up a Style and apply your settings to it. Then you apply the
Style to the Datagrid.

in my function where setting up the dgv, I use:
dgvAll.TableStyles.Add(createStyle());

where createStyle() =
//********************************
//* Define Style and pass back as a Style
//********************************
private DataGridTableStyle createStyle() {
// Define a table style layout and bind
DataGridTableStyle myStyle = new DataGridTableStyle();
myStyle.MappingName = "Project";
myStyle.BackColor = Color.LightGreen;
myStyle.AlternatingBackColor = Color.LightYellow;
myStyle.SelectionForeColor = Color.DarkGray;

// Definitions
DataGridTextBoxColumn bpuType = new DataGridTextBoxColumn();
DataGridTextBoxColumn stockCode = new DataGridTextBoxColumn();
DataGridTextBoxColumn puCode = new DataGridTextBoxColumn();

// Mapping Name
bpuType.MappingName = "BPU_TYPE";
stockCode.MappingName = "STOCK_CODE";
puCode.MappingName = "PU_CODE";

// Header
bpuType.HeaderText = "BPU TYPE ";
stockCode.HeaderText = "STOCK CODE ";
puCode.HeaderText = "PU CODE ";

// Special cases
bpuType.NullText = " ";

// Alignment and Width (if not default)
bpuType.Width = 70;
puCode.Width = 100;
bpuType.Alignment = HorizontalAlignment.Right;

// Add to theme
myStyle.GridColumnStyles.Add(bpuType);
myStyle.GridColumnStyles.Add(busCategory);
myStyle.GridColumnStyles.Add(puCode);

return myStyle;
}
 
R

RobinS

This is pretty spiffy, and I'm going to store it away for future use.

But I think what he wants is the *same* column to have *different*
widths depending on the row. Is that possible? I didn't think so,
but if I'm wrong, I'd love to see the code. Unless the code
below does that, and I'm just not reading it correctly.

Robin S.
---------------------------
 
G

Guest

Hmmm... didn't catch that (doh)

If what you're reading is correct, he wants to "merge" cells like Excel can
do. I don't think the DataGrid can actually do that by itself, but I'm not
100% positive that it can't -- I've NEVER even THOUGHT of trying that.

Thanks for compliment... no this just sets each column a certain width, not
a cell...
 
R

RobinS

It's okay, it was still nice of you to provide that excellent example
for the rest of us.

It doesn't even sound like he wants to merge cells, he just wants
the columns to be different widths. I guess merging cells is sort
of one way to accomplish that.

I know you can make it appear as if the column has a merged
cell by making the line between two cells disappear (I've done
it between rows, so I'm assuming it would work between two
columns), but that still doesn't make one larger like the
MergeCells in Excel.

Maybe the original poster will elaborate on his requirements...

Robin S.
----------------------------
 

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