ASP.NET - how to fit the textbox in gridview? - Asked By Bharath Kumar on 13-Jun-12 05:56 AM

kalpana aparnathi replied to Bharath Kumar on 13-Jun-12 06:33 AM

set like this:



bharti odedra replied to Bharath Kumar on 13-Jun-12 06:45 AM
try this string q = "select city from table1 where Edate between('" + TextBox1.Text
  + "' AND '" + TextBox1.Text + "')";

ALTER PROCEDURE dbo.BetweenDates  
  @FromDate datetime,
  @ToDate datetime
select * from table1 where orderDate between @FromDate and @ToDate
Venkat K replied to Bharath Kumar on 13-Jun-12 07:23 AM
If you have a fixed width for the gridview column, Just set the width of the textbox to the size of the column of a gridview.
Chintan Vaghela replied to Bharath Kumar on 14-Jun-12 08:31 AM

Hi Frndz,


Functionality:  Set textbox width inside GridView


For that ,

  1. Set Header Style Width
  2. Set Item Style Width
  3. Set Textbox Width
  4. If your string is too much long then you need to break your string dynamically


Create one function which break the string after specific number of character.


Check BreakLongString Function for break the string.


Pass BreakLongString Function as

Text='<%# BreakLongString(Eval("Value").ToString(),30) %>'



There is two argument.

  1. Pass the original string
  2. Pass number (Break string after ths number)






Textbox inside Templatefield



    <asp:TemplateField HeaderStyle-Width="80px" ItemStyle-Width="80px">


            <asp:TextBox ID="txtLongString" Width="80px" runat="server" Text='<%# BreakLongString(Eval("Value").ToString(),30) %>'></asp:TextBox>






Function for Break Long String


public string BreakLongString(string SubjectString, int CharsToBreakAfter)


      string Pattern = "\\S{" + CharsToBreakAfter + ",}";

      int Counter = 0;

      bool IsMatching = Regex.IsMatch(SubjectString, Pattern);

      while (IsMatching)



        string MatchedString = Regex.Match(SubjectString, Pattern).Value;

        SubjectString = SubjectString.Replace(MatchedString.Substring(0, (CharsToBreakAfter - 1)), MatchedString.Substring(0, (CharsToBreakAfter - 1)) + " ");

        // Prevent endless loops

        if (Counter > 20) break;

        // Check if we still have long strings

        IsMatching = Regex.IsMatch(SubjectString, Pattern);


      return SubjectString;



 Hope this helpful!