.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

If GridView width not set how to get the width of a GridView from codebehind?

Posted By:      Posted Date: September 23, 2010    Points: 0   Category :ASP.Net


We didn't set the width of a gridview. I wants to get the width of a gridview.  

When we get it returns 0.0.

int width = (int)gvStylish.Width.Value;

How to get the width?


Babu Kumarasamy.

View Complete Post

More Related Resource Links

How to set the width of column on GridView

How to set the specific the width of each column, the columns are created automaticly 

Unable to set GridView Column Width


Hello all,

I have been searching the internet for a solution to this for hours. I have tried all of the following methods to set the column width on my GridView control:

1. <asp:TemplateField HeaderText="ColumnHeader" ItemStyle-Width="100px">

2. <ItemStyle Width="100px" Wrap="False" />

3. GridView.Columns[0].ItemStyle.Width = 100 both on the PageLoad event and, out of desperation, on the RowDataBound event.

I have tried these settings by themselves and in combination. I have also experimented with different CSS width settings for the GridView and its containing <div> (including none) to no avail. The GridView columns are always equally spaced and wrap the row description text in the first column in a rather ugly fashion.

As this needs to be fixed for a presentation to a client on Monday, I would be eternally grateful to anyone who could point me in the right direction.

Thanks in advance.

Clone GridView-Columns Widths (assign the Column-Width from 1 Gridview to another)


Hi everyone,

I've googled quite a lot, but couldn't find a solution - in the end this forum always helped me out the best! :)

This is my problem: I've got my Gridview gv_1, it gets its data from the code behind through a DataSet, gets generated like it should be, and assigns it's column-width to the longest entry in the datarow. Now I've got my Gridview gv_2, which has other entries and column widhts (gets filled the same way) - what I want to do is to change the width of the columns that the gridviews have the same overall width.

I've uploaded a screenshot: http://stepdown.elementfx.com/image16.jpg

What I've already tried is playing around with the GridView.HeaderRow.Cells[0].Width-Property, but when I debug it (after it's DataBound) I've got no values, meaning the values are "0.0" etc. The try to assign a value also didnt work (not directly for Width and not for Width.Value).

The next problem I could encounter is that my second GridView has no visible HeaderRow - and as far as I've googled people always somehow set the width of the column - but I'm not even that far yet. ;)

So the perfect solution (working in my head at least) would be to get the width of each column from the 2 gridviews, pick the larger one, and ass

Dynamically adjusting Gridview Column Width



I populate a gridview dynamically which has differen column numbers depend on requirements. My goal is to adjust column width dynamically. I have tried several methods and events, then RowCreated event of GridView looks best one to do. My code is as below. When i check result at the ViewSource on the browser, i can see that width of <td> successfully adjusted. BUT nothing changes on the browser, column width always as wide as text? Can you please advise what to do?

My Code

    Protected Sub GridViewAgeing_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridViewAgeing.RowCreated
        If e.Row.RowType = DataControlRowType.Header Then
            Dim td As TableCell
            For Each td In e.Row.Cells
                td.Width = Unit.Pixel(500)
                td.Text = td.Text.Replace("_", " ")
        End If

        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim td2 As TableCell
            For Each td2 In e.Row.Cells
                td2.Width = Unit.Pixel(500)
        End If
    End Sub



	<table cellspacing="0" rules="all" border="

Problem with gridview width


Is there any way to set the width of a GridView to a pre-determined width?

I have set the width of the gridview and all the columns to a % and then to a number of pixels but it does work. The gridview always sets itself to a width required to display all fields and it goes beyond the resolution of the screen. I would like to contain it to 1024 pixels.


Here's my .aspx


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewInsertEditUpdateDelete.aspx.cs" Inherits="GridViewInsertEditUpdateDelete" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Untitled Page</title>

    <form id="form1" runat="server">
        <br />
        <asp:Label ID="Label9" runat="server" Text="Project"></asp:Label>
        <asp:TextBox ID="TextBox2" runat="server" Width="277px"></asp:TextBox>
        <br />
        <br />

    <div >

gridview filtering using a codebehind (textbox + search button)

i am trying to replicate the following sequence: input text into text box > press search button > search a database using LIKE operator > display using a gridview or listview. this sequence is fairly easy to do using the drag and drop then sqldatasource options, but i am trying to do the same result using a codebehind.  here is my code:   ____________________________________________________________________________________________________________ SqlConnection myConn; myConn = new SqlConnection(@"Data Source=.\SQLEXPRESS; AttachDbFilename=|DataDirectory|\projectDB.mdf; Integrated Security=True;User Instance=True"); myConn.Open(); string strSQL; strSQL = "SELECT * FROM Message";       //strSQL = "SELECT * FROM Message WHERE Message LIKE '" + txtSearch.Text + "'"; SqlDataAdapter da = new SqlDataAdapter(strSQL, myConn); DataSet ds = new DataSet(); da.Fill(ds, "Message"); GridView1.DataSource = ds; GridView1.DataBind(); myConn.Close(); _____________________________________________________________________________________________________________  now, if I use this code inside a page_load event, it does work. if I put it inside a button_click event i get nothing. i am mystified by this.. any help? why can't i use a button to display my gridview1 when i'm using a code behind.  

How can I set the width property the LayoutTemplate in a listview through codebehind

Taking the following simplified layout for a listview, how do I get and set the width property of the columns. Using Codebehind, how do I set the width of Th2 to Th1. <LayoutTemplate> <table ID="itemPlaceholderContainer" runat="server"> <tr ID="Tr1" runat="server"> <th ID="Th1" runat="server"> </th> <th ID="Th2" runat="server">                         </th> <th ID="Th3" runat="server">                         </th> </tr> </table> </LayoutTemplate>    

Why does "reader" in my c# codebehind skip the first row of data when binding to a gridview?


Argh.. I'm making the switch from vb.net to c#, and am just trying to bind some data from a reader in my c # codebehind, but no matter what, the gridview always skips the first row returned from my stored procedure.  What am I doing wrong???

comm.CommandText = "123456";
comm.Parameters.AddWithValue("@SubSectionParentID", SubSectionParentID.Text);

reader = comm.ExecuteReader();

                  while (reader.Read())
            gvMenu.DataSource = reader;


Adding a javascript to Gridview in CodeBehind.


Good Day everyone,

Is there a way to turn this

 <asp:TemplateField HeaderText="Price">
             <asp:HyperLink ID="selectionHyperLink"
                     NavigateUrl='<%# Eval("PartNo", "javascript:popWindow('../../Synergy/Docs/HLI_CCareItemInfo.asp?close=1&Item={0}');") %>'
                     Text='<%# Eval("Price")%>'/>
Into a Code Behind code? 
Anyone have any idea. I tried calling a TemplateField but then what do I do? 

Any help is greatly appreciated.

Inplace Edit in GridView

In this article I am presenting a different way to look at EditItemTemplate, using which user navigation for editing is reduced and . it looks very different (see screeen shots) then regular implementation of GridView.

GridView Confirmation Box Using JQuery BlockUI in asp.net

JQuery is the most popular JavaScript library. One of the main reasons for its fame is the plugin architecture. Anyone can create a separate library that leverage the JQuery library. In this article we are going to look at the BlockUI library which is used to block the user screen while performing a task.

Textbox Editable GridView in ASP.NET

This article describes how I made a fully editable gridview in C# using ASP.NET 2. The idea is that the GridView looks and works like an Excel spreadsheet. You see all the cells in the table, and you can edit any of the cells you like, and they are automatically updated (i.e. saved to the database). This is very often how people expect data tables to work in web pages, and I've often seen people clicking in vain on un-editable gridview cells, somehow expecting to edit them, and not understanding that they need to click an Edit button at the end of the row.

Adding Dynamic Rows in ASP.NET GridView Control with TextBoxes and with Delete functionality

In my previous examples, I have demonstrated on how to add dynamic rows in GridView control with TextBoxes and how to save the values into the database. Now, seems that most of the developers are asking if how to add a delete functionality with it. So in this example, I'm going to show on how to delete a certain row in the dynamic GridView with TextBoxes.

AJAX GridView Checkbox Select All in ASP.Net

In the previous article GridView Select Multiple Rows you learnt the multiple row selection in GridView control. Here you will get the C# code sample to select all the gridview rows using single checkbox server control. You can highlight the selected rows with different color and Font state Bold. If you want to use the selection values on different ASP.Net

AJAX GridView Paging and Sorting using C# in ASP.Net

To enable the GridView Paging and Sorting to update the GridView Data without refreshing the ASP.Net web page AJAX Extension controls are used. UpdatePanel is used to hold the GridView control that triggers the events for the controls placed inside it. By Default UpdatePanel triggers the asynchronous postbacks for its child controls.

GridView Paging Using Ajax in ASP.Net

To enable the GridView Paging and Sorting to update the GridView Data without refreshing the ASP.Net web page AJAX Extension controls are used. UpdatePanel is used to hold the GridView control that triggers the events for the controls placed inside it. By Default UpdatePanel triggers the asynchronous postbacks for its child controls.

Displaying Row Details Tooltip on GridView using JQuery

Continuing exploring JQuery! I decided to implement a feature to display some kind of details related to a row displayed on GridView. For example when displaying employees details on GridView some information might not fit in the GridView because it will make it huge and wide. These infomration can be diplayed as Tooltip. Or when you want to display a picture and some kind of formated text to be diplayed as tooltip when mouse hover on the image.
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend