Dynamically setting GridView Column width in SharePoint when AutoGenerate columns=false

Hi All,

We have one Requirement.

We have One Gridview, Using that gridview we bind List Data. But we need to set all the columns values are same width.

I tried following references in google,but it's not working.

http://www.dotnetjalps.com/2013/07/Dynamically-setting-GridView-Column-width-in-ASP-Net.html

http://www.aspdotnet-suresh.com/2013/01/aspnet-set-gridview-column-width.html

Please find the attached screen shot.

I need all the colums same width...

Can any one suggest me how to do...

Thanks,

February 3rd, 2015 5:13pm

Hi,

From your description, my understanding is that you want to set all columns with the same width in gridview.

You could try these code below:

for (int i = 0; i < spGrid.Columns.Count; i++)

{

spGrid.HeaderRow.Cells[i].Width = 50;

}

The screenshot below is my result:

Best Regards,

Free Windows Admin Tool Kit Click here and download it now
February 5th, 2015 5:41am

Hi Vincent,

I tried above code it's not working for me..

Please see the my gridview design code.

<asp:GridView ID="GridView1" runat="server" CssClass="gridlines" AllowSorting="true" EmptyDataText="No Data Found" AutoGenerateColumns="False" EnableModelValidation="True" width="100%" BorderStyle="Solid" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound" OnSorting="GridView1_Sorting"  >
                                <AlternatingRowStyle BackColor="#B2B2B2"  />
                             
                                
                    <Columns>
                        <asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" Visible="true" ItemStyle-Width="10%"/>
                        <asp:BoundField DataField="V_LastName" HeaderText="Last Name" SortExpression="V_LastName" ItemStyle-Width="30%"/>
                        <asp:BoundField DataField="V_FirtsName" HeaderText="First Name" SortExpression="V_FirtsName" ItemStyle-Width="30%"/>
                       
                        
                        <asp:BoundField DataField="StartDate" HeaderText="Start Date" DataFormatString="{0:d}" SortExpression="StartDate" ItemStyle-Width="15%"/>
                        <asp:BoundField DataField="Supporter_x0020_ID" HeaderText="Supporter ID" SortExpression="Supporter_x0020_ID"/>
                        <asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" ItemStyle-Width="200px"/>

                        <asp:TemplateField HeaderText="Email" ItemStyle-Width="15%">
                            <HeaderStyle BackColor="#B2B2B2" Font-Bold="True" ForeColor="#59753D" HorizontalAlign="Left" Wrap="False"/>
                            <ItemTemplate>
                                <div style="width: 40px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis">
                                <ItemStyle HorizontalAlign="Center"/>
                               <asp:HyperLink runat="server" ID="hyperemail" Text='<%# Eval("email")%>'></asp:HyperLink>
                            </ItemTemplate>

                        </asp:TemplateField>
                        
                        <asp:BoundField DataField="Level" HeaderText="Level" SortExpression="Level"   />
                        <%--<asp:BoundField DataField="Active" HeaderText="Active" SortExpression="Active" />--%>
                            <asp:TemplateField HeaderText="Active" >
                            <ItemStyle HorizontalAlign="Center"/>
                            <ItemTemplate>
                               
                             <asp:Image runat="server" ID="ImgActive" ImageUrl="/PublishingImages/check.gif" Visible="false" />
                            </ItemTemplate>

                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="UserName" Visible="false">
                            <ItemStyle HorizontalAlign="Center"/>
                            <ItemTemplate>
                               
                               <asp:Label ID="lblUserName" runat="server" Text='<%# Eval("UserName")%>'></asp:Label>
                            </ItemTemplate>

                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Hours">
                       <HeaderStyle BackColor="#B2B2B2" Font-Bold="True" ForeColor="#59753D" HorizontalAlign="Left" Wrap="False"/>
                            <ItemStyle HorizontalAlign="Center"/>
                            <ItemTemplate>
                              
                               <asp:Label ID="lblHours" runat="server" ></asp:Label>
                            </ItemTemplate>
                            <FooterTemplate>
                                 <asp:Label ID="lblTotalHours" runat="server" ></asp:Label>
                                <asp:Literal ID="TotalHours" runat="server"></asp:Literal>
                            </FooterTemplate>
                        </asp:TemplateField>
                        <%--<asp:TemplateField></asp:TemplateField>--%>
                        <asp:TemplateField>
                            <ItemStyle HorizontalAlign="Center"/>
                            <ItemTemplate>
                                
                                <asp:Button ID="btnEdit" runat="server" Text="Edit Entry" CommandArgument='<%# Eval("ID")%>' CommandName="editentry"  />
                                  <%--<asp:Button ID="Button2" runat="server" CommandArgument='<%# Eval("ID")%>' CommandName="Detls" Text="Details" />--%>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                                <FooterStyle BackColor="#336699" Font-Bold="True" ForeColor="White" HorizontalAlign="Left" />
   <%--<HeaderStyle BackColor="#336699" Font-Bold="True" ForeColor="White" HorizontalAlign="Left" />--%>
                                <HeaderStyle BackColor="#B2B2B2" Font-Bold="True" ForeColor="#59753D" HorizontalAlign="Left" Wrap="False"/>
                             

Can you please suggest me...

Thanks...

February 6th, 2015 3:16am

The reason this is happening is that your are setting ItemStyle-Width in percentages in some fields and in pixels in some fields and to add it to it they are different in different fields. Try setting it pixels and make sure all of them have same value. Like ItemStyle-Width="50px"
Free Windows Admin Tool Kit Click here and download it now
February 6th, 2015 3:29am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics