Click here to Skip to main content
15,909,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to dynamically set width of a gridview column but when I apply the following code, I see no change in the browser.

.aspx code is:

C#
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" PageSize="5" CellSpacing="4" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" CellPadding="4" DataKeyNames="File_Name" GridLines="None" OnRowEditing="GridView1_RowEditing" OnRowDeleting="GridView1_RowDeleting" OnRowUpdated="GridView1_RowUpdated" OnRowUpdating="GridView1_RowUpdating" ForeColor="#333333" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnPageIndexChanging="GridView1_PageIndexChanging" AllowSorting="True" OnSorting="GridView1_Sorting" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_OnRowDataBound" >
                <PagerSettings Mode="NumericFirstLast" FirstPageText="First" LastPageText="Last" PageButtonCount="10" Position="Bottom" />
                    <AlternatingRowStyle BackColor="White" />
                    <Columns >
                        <asp:TemplateField HeaderText="File">
                            <ItemTemplate>
                                <asp:LinkButton ID="lnkbtnFileName" runat="server" CommandArgument='<%# Eval("File_Name") %>' CommandName="Download" Text='<%# Eval("File_Name") %>'></asp:LinkButton>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                <EditRowStyle BackColor="#2461BF" />
                <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                <RowStyle BackColor="#EFF3FB" />
                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                <SortedAscendingCellStyle BackColor="#F5F7FB" />
                <SortedAscendingHeaderStyle BackColor="#6D95E1" />
                <SortedDescendingCellStyle BackColor="#E9EBEF" />
                <SortedDescendingHeaderStyle BackColor="#4870BE" />


            </asp:GridView>


.cs code is:

C#
protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
{
    //SET WIDTH OF THE COLUMNS
    if (e.Row.RowType == DataControlRowType.Header)
    {
        e.Row.Cells[0].Width = Unit.Pixel(500); //DATE
        e.Row.Cells[1].Width = Unit.Pixel(100); //CUSTOMER
        e.Row.Cells[2].Width = Unit.Pixel(100); //DESTINATION


    }
}


Can someone please help?
Posted
Updated 23-Aug-14 3:41am
v3
Comments
Surendra0x2 23-Aug-14 9:38am    
Instead of DataControlRowTYpe.Header use if (e.Row.RowType == DataControlRowType.DataRow)
{
[no name] 23-Aug-14 10:32am    
@Surendra0x2 using that giving, `Index was out of range. Must be non-negative and less than the size of the collection` error.
Surendra0x2 23-Aug-14 14:38pm    
May be your Giving Wrong Indexing to column.
protected void GrdView_RowDataBound(object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.DataRow)
{
grdListings.Columns[1].ItemStyle.Width = 50;
grdListings.Columns[2].ItemStyle.Width = 150;
}
}

This code is not wroking?
Surendra0x2 23-Aug-14 14:41pm    
As you posted your code there is only one Column. then why r u Indexing 1 and 2 just simply
write
protected void GrdView_RowDataBound(object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.DataRow)
{
grdListings.Columns[0].ItemStyle.Width = 50;
}
}

You can set it like this
C#
GridView1.Columns[0].ItemStyle.Width = 50;

Refer these links too
How to: Set GridView Web Server Control Column Width Dynamically
Asp.net Set Gridview Column Width Dynamically in C#, VB.NET
 
Share this answer
 
Comments
[no name] 23-Aug-14 10:35am    
@Dilan Shaminda , Code running without any error with your suggestion but desired output is not seen.
Dilan Shaminda 23-Aug-14 12:44pm    
i tried it and it works fine.Try setting AutoGenerateColumns="False"
[no name] 24-Aug-14 13:38pm    
@Dilan Shaminda , I'm binding data dynamically from sql server. Did you do the same thing? Do you want me to paste binding data function here for you to see?
Dilan Shaminda 25-Aug-14 0:31am    
i created a datatable and add some values to it and then bind it to the gridview.
[no name] 25-Aug-14 3:41am    
@Dilan Shaminda, Unfortunately, no desired change to the width of the column gained through both our snippets. It is strange to see when I apply background color to the column as:

if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].Width = 100;
e.Row.Cells[0].Style["border-right"] = "2px solid #666666";
e.Row.Cells[0].BackColor = System.Drawing.Color.CornflowerBlue;

e.Row.Cells[1].Width = 200;
e.Row.Cells[1].Style["border-right"] = "2px solid #666666";
e.Row.Cells[1].BackColor = System.Drawing.Color.Lime;

e.Row.Cells[2].Width = 400;
e.Row.Cells[2].Style["border-right"] = "2px solid #666666";
e.Row.Cells[2].BackColor = System.Drawing.Color.Red;

e.Row.Cells[3].Width = 800;
e.Row.Cells[3].Style["border-right"] = "2px solid #666666";
e.Row.Cells[3].BackColor = System.Drawing.Color.Yellow;
}

i see the desired results as: (http://tinypic.com/r/x35zc3/8)

But specifically with the width, this code is not working. If you notice, I deliberately set width of the first column 100px, second column 200px. third column 400px, and fourth column 800px which means columns width should double as they go from 1 to 2 to 3 to 4 in number. But thats not the case with the output.
 
Share this answer
 
Comments
[no name] 23-Aug-14 10:17am    
@Surendra0x2 , Did following modification as guided by the article you pasted:

protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
//SET WIDTH OF THE COLUMNS
if (e.Row.RowType == DataControlRowType.Header)
{

GridView1.Columns[4].ItemStyle.Width = 50;

}
}
and getting following error:

`Index was out of range. Must be non-negative and less than the size of the collection.`

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900