Link to home
Start Free TrialLog in
Avatar of tenriquez199
tenriquez199

asked on

width of Columns of ViewGrid in .NET C#

Hi

Well, I have a problem with Widths of Columns of a ViewGrid.

I need that my ViewGrid has enabled "AutoGenerateColumns", this because the DataSource varies by product type. I have this already.

But I need to set the fixed width columns, for example.

When type of product is Vines, I need the width of all columns= 138 px
When type is  juice, the Width of all columns =140 px

How do I do???
Somebody help me
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

I've done something similar with the older datagrid.  You need to loop through columns and set the width dynamically.

I can't find the old datagrid example that gave me the inspiration but found a gridview link.

Check out the GridView1_RowDataBound example in:
http://msdn.microsoft.com/en-us/library/ms178296.aspx

Avatar of tenriquez199

ASKER

Ohmg... I need clear Oracle Database Zone's

and that's it
Thanks
I do It something like your link
http://msdn.microsoft.com/en-us/library/ms178296.aspx

but i receive this message


Index was out of range. Must be a non-negative and less than the size of the collection.
Parameter name: index

Ok; this is different;

for (int i = 0; i < GridView1.Columns.Count   -  1; i++)  //try minus 1
{
GridView1.Columns[i].ItemStyle.Wrap = True;  //SET to TRUE
//If the Wrap property is false, the column is automatically resized.
}

//for product column
GridView1.Columns[0].ItemStyle.Width = 138;
//for other1 column
GridView1.Columns[1].ItemStyle.Width = 140;
//for other2 column
GridView1.Columns[2].ItemStyle.Width = 100;

Open in new window


>>Index was out of range. Must be a non-negative and less than the size of the collection.
Parameter name: index
The index was our of range, maybe because you oversize the index;
eq.,
GridView1.Columns[   2<if you have 3 columns, because it starts on 0 not 1>    ].ItemStyle.Width = 100;
I realized that "GridView1.Columns.Count"= 0

So when i go to column 0, dont find it ....
Yes, maybe you put the code before the GridView Starts to Load
Put the code snippet after the codes load during startup
eq.,
Page_Load....begin
...
...other codes here that database retrieved to GridView
...
codes here..
...
for (int i = 0; i < GridView1.Columns.Count   -  1; i++)  //try minus 1
{
GridView1.Columns[i].ItemStyle.Wrap = True;  //SET to TRUE
//If the Wrap property is false, the column is automatically resized.
}
...
GridView1.Columns[0].ItemStyle.Width = 138;
...

Open in new window

Page_Load....end
I have a little time to mess with this.  Can you post your datagrid declaration?

Also, where is the 'product' stored?  I assume it's outside the datagrid but is it a label, textbox, ???

Ok I have a soo big viewgrid with SQL DataSource,

On the DB SQL i have somethin like this....

Type-----Brand--------volume---------amount------date
VINES     California         5                 15000            25/01/11
VINES     California         4                  1120             27/01/11
SODAS   Good4you       12                 890               13/01/11
VINES     St.Ethiene         1                 450               25/01/11


In C# .NET i have next

string type;
string sell; //(SELL IN OTHER PART OF CODE, CAN BE "amount","volume")
if (type.Equals("VINES"))
{

SqlDataSource1.SelectCommand = "SELECT [California], [St.Ethiene], [Total]   FROM (SELECT                                                                                                                                                           .                                               [Brand], [" + sell+ "] FROM [Sicom].[dbo].[jde11$] where Type='VINES'" +
                          " and mes='1') p PIVOT (SUM([" + sell+ "]) FOR [Brand] IN ( [California], [St.Ethiene], "+  .                         "[Total])) AS pvt";
        GridView1.DataBind();
        GridView1.Visible = true;
}

everything works fine....
but when i show it...

the columns move a lot....


WITH STRING TYPE= "VINES"
GRIDVIEW 1-------------------------------------------------------------------------------------------
       Day                          California                                      St.Ethienne                               Total
        25/01/11                    15000                                              450                                        15450
----------------------------------------------------------------------------------------------------------

GRIDVIEW2 --------------------------------------------------------------------------------------------
       Day                                                    California                                          St.Ethienne  Total
         27/01/11                                               1120                                                         0     15450
-----------------------------------------------------------------------------------------------------------

WITH STRING TYPE ="SODAS"

GRIDVIEW2 --------------------------------------------------------------------------------------------
       Day                                                    Good4you                                            Total
         27/01/11                                               890                                                   890              
-----------------------------------------------------------------------------------------------------------


the grids are not aligned, that is why we need to encode the column width depending on the type or some other idea.

the other option I've found is to make a grid for each product, but would take more than 100 grids .... is not feasible
>>the other option I've found is to make a grid for each product

The sample you posted looks like you are already using multiple grids:
GRIDVIEW1
...
GRIDVIEW2
...
GRIDVIEW3
...


Please post the aspx code for the page.
<asp:GridView ID="GridView1" runat="server" BackColor="White"
                BorderStyle="None" CssClass="style176" DataSourceID="SqlDataSource37"
                GridLines="Vertical" ShowFooter="True"
                OnRowDataBound="GridView37_RowDataBound" CellSpacing="-1">
                <AlternatingRowStyle BackColor="#F7F7DE" />
                <FooterStyle BackColor="#F7F7DE" />
                <HeaderStyle BackColor="White" Font-Names="Eras Demi ITC" Font-Size="Medium"
                    ForeColor="White" HorizontalAlign="Center" VerticalAlign="Middle" />
            </asp:GridView>
            <asp:GridView ID="GridView2" runat="server" BackColor="White"
                BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4"
                CssClass="style175" DataSourceID="SqlDataSource36" ForeColor="Black"
                GridLines="Vertical" OnRowDataBound="GridView36_RowDataBound"
                ShowHeaderWhenEmpty="True">
                <AlternatingRowStyle BackColor="White" />
                <FooterStyle BackColor="#CCCC99" />
                <HeaderStyle BackColor="White" Font-Bold="True" Font-Names="Eras Demi ITC"
                    Font-Size="Medium" ForeColor="#00CC66" HorizontalAlign="Center" />
                <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
                <RowStyle BackColor="#F7F7DE" />
                <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
                <SortedAscendingCellStyle BackColor="#FBFBF2" />
                <SortedAscendingHeaderStyle BackColor="#848384" />
                <SortedDescendingCellStyle BackColor="#EAEAD3" />
                <SortedDescendingHeaderStyle BackColor="#575357" />
            </asp:GridView>
            <asp:GridView ID="GridView3" runat="server" BackColor="White"
                BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4"
                CssClass="style172" DataSourceID="SqlDataSource33" ForeColor="Black"
                GridLines="Vertical" OnRowDataBound="GridView33_RowDataBound"
                ShowHeaderWhenEmpty="True">
                <AlternatingRowStyle BackColor="White" />
                <FooterStyle BackColor="#CCCC99" />
                <HeaderStyle BackColor="White" Font-Bold="True" Font-Names="Eras Demi ITC"
                    Font-Size="Medium" ForeColor="#00CC66" />
                <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
                <RowStyle BackColor="#F7F7DE" />
                <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
                <SortedAscendingCellStyle BackColor="#FBFBF2" />
                <SortedAscendingHeaderStyle BackColor="#848384" />
                <SortedDescendingCellStyle BackColor="#EAEAD3" />
                <SortedDescendingHeaderStyle BackColor="#575357" />
I'm confused.  You mentioned in http:#a35001245 you didn't want multiple gridviews.  The code you posted confirms you do have multiple gridviews.

Why not just one?
Sorry I do not explain well...
Ok Go Again!!...

I have 15 GridView, but the GV's have 15 columns

I dont work only whit a one GV, because I havent make a Query in SQL that get the information that i need...

So you are wanting ALL the gridviews on the page to have the same widths?

Why can't you specify the same gridview layout for all of them?  In other words: the same fixed widths hardcoded on the design.



Uhmmm Somethin like this...

when type of product is Vines.... the width of ALL GV is 1900 px

whent type is Sodas the Width of ALL GV is 1600 px

and bla bla bla


How I Do these???
""Why can't you specify the same gridview layout for all of them?  In other words: the same fixed widths hardcoded on the design."
No.  Why not set all to 1900px if that is the largest one. Do away with the whole 'dynamic' issue.

Even if you don't have the sql set up to return a single result, can you possibly merge the datasets and use a single grid?
I did.
But I do not work at all, because the columns do not align
I need fixed columns depending on the type
Did you set a fixed size for the columns or just the grid itself?

I'v not done much with gridview but have a lot of datagrid experience.  With datagrids I could set item widths of the individual columns in the grid without doing in code.
I need set a fixed size for columns.... the problem its that i've AutoGenerateColumns = true;

Because the columns change depending on the tyoe
ASKER CERTIFIED SOLUTION
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial