Solved

width of Columns of ViewGrid in .NET C#

Posted on 2011-02-23
23
939 Views
Last Modified: 2012-06-22
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
0
Comment
Question by:tenriquez199
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 8
  • 2
23 Comments
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 34965952
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

0
 
LVL 1

Author Comment

by:tenriquez199
ID: 34973933
Ohmg... I need clear Oracle Database Zone's

and that's it
0
 
LVL 1

Author Comment

by:tenriquez199
ID: 34974254
Thanks
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:tenriquez199
ID: 34974523
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

0
 
LVL 14

Expert Comment

by:systan
ID: 34975818
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;
0
 
LVL 1

Author Comment

by:tenriquez199
ID: 34975895
I realized that "GridView1.Columns.Count"= 0

So when i go to column 0, dont find it ....
0
 
LVL 14

Expert Comment

by:systan
ID: 34976486
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
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 34983552
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, ???

0
 
LVL 1

Author Comment

by:tenriquez199
ID: 35001245
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
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 35007210
>>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.
0
 
LVL 1

Author Comment

by:tenriquez199
ID: 35011418
<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" />
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 35011479
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?
0
 
LVL 1

Author Comment

by:tenriquez199
ID: 35011969
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...

0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 35012018
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.



0
 
LVL 1

Author Comment

by:tenriquez199
ID: 35012284
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."
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 35012442
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?
0
 
LVL 1

Author Comment

by:tenriquez199
ID: 35012471
I did.
But I do not work at all, because the columns do not align
I need fixed columns depending on the type
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 35012499
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.
0
 
LVL 1

Author Comment

by:tenriquez199
ID: 35012554
I need set a fixed size for columns.... the problem its that i've AutoGenerateColumns = true;

Because the columns change depending on the tyoe
0
 
LVL 77

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
ID: 35020765
Sorry for the delay.

Here's the 'news':

From doing the research it appears you cannot access the Columns collection in a gridview autogenerated.

I found a nice workaround on:
http://geekswithblogs.net/michelotti/archive/2006/03/30/73896.aspx

Below is a self-contained page I used for testing.

Hopefully it will provide you enough information.  If not, post back and I'll try to tweak it.
<%@ Page Language="VB" Debug="true" %>

<%@ import namespace = "System.Data" %>

<html>
<title>simple test</title>
<body>

<br/>
<br/>
<script runat="server">

	Function CreateDataSource1() As ICollection
        
        Dim dt As DataTable
        Dim dr As DataRow
        Dim i As Integer

        'create a DataTable
        dt = New DataTable
        dt.Columns.Add(New DataColumn("Day", GetType(String)))
        dt.Columns.Add(New DataColumn("California", GetType(String)))
        dt.Columns.Add(New DataColumn("St.Ethienne", GetType(String)))
        dt.Columns.Add(New DataColumn("Total", GetType(String)))

        dr = dt.NewRow()

        dr(0) = "25/01/11"
        dr(1) = "15000"
        dr(2) = "450"
        dr(3) = "15450"

        dt.Rows.Add(dr)

        CreateDataSource1 = New DataView(dt)
        
    End Function

	Function CreateDataSource2() As ICollection
        
        Dim dt As DataTable
        Dim dr As DataRow
        Dim i As Integer

        'create a DataTable
        dt = New DataTable
        dt.Columns.Add(New DataColumn("Day", GetType(String)))
        dt.Columns.Add(New DataColumn("Good4You", GetType(String)))
        dt.Columns.Add(New DataColumn("Total", GetType(String)))


        dr = dt.NewRow()

        dr(0) = "27/01/11"
        dr(1) = "890"
        dr(2) = "890"

        dt.Rows.Add(dr)

        CreateDataSource2 = New DataView(dt)
        
    End Function


	Sub page_load(sender as object, e as eventargs)
        GridView1.DataSource = CreateDataSource1()
        GridView1.DataBind()

        GridView2.DataSource = CreateDataSource2()
        GridView2.DataBind()

		'from: http://geekswithblogs.net/michelotti/archive/2006/03/30/73896.aspx
		GridView1.Rows(0).Cells(0).Width = Unit.Pixel(200)
		GridView1.Rows(0).Cells(1).Width = Unit.Pixel(200)
		GridView1.Rows(0).Cells(2).Width = Unit.Pixel(200)
		GridView1.Rows(0).Cells(3).Width = Unit.Pixel(200)

		GridView2.Rows(0).Cells(0).Width = Unit.Pixel(200)
		GridView2.Rows(0).Cells(1).Width = Unit.Pixel(400)
		GridView2.Rows(0).Cells(2).Width = Unit.Pixel(200)

	end sub



</script>

<form runat="server">
	<asp:GridView ID="GridView1" runat="server"
				AutoGenerateColumns=true
	/>
	<br/>
	<asp:GridView ID="GridView2" runat="server"
				AutoGenerateColumns=true
	/>

</form>

</body>
</html>

Open in new window

0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question