Results 1 to 14 of 14

Thread: [RESOLVED] gridview width

  1. #1

    Thread Starter
    Frenzied Member met0555's Avatar
    Join Date
    Jul 2006
    Posts
    1,385

    Resolved [RESOLVED] gridview width

    Hi,

    I'm having a little problem, I have a gridview, and i want for each column different width size, and i was not able to give different width size. Even there is a text i would like to cut it out.
    This is my code, and it only changes the textbox at the edit mode only.
    Code:
    <asp:BoundField DataField="reconciliated" HeaderText="Status" 
                            SortExpression="reconciliated" ControlStyle-Width="95px" >
                            
                        </asp:BoundField>

  2. #2
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: gridview width

    Hey,

    What do you mean by this part:

    Even there is a text i would like to cut it out.
    As for the other question, rather than use ControlStyle-Width, have you tried setting the Width property of the ItemStyle of the BoundField?

    If you are still having problems, can you show a screenshot of the problem?

    Gary

  3. #3

    Thread Starter
    Frenzied Member met0555's Avatar
    Join Date
    Jul 2006
    Posts
    1,385

    Re: gridview width

    Hi,

    I tried with ItemStyle but it's all the same , nothing changed.
    By that i meant, ex. if text in the column it's "experience" an if i want the width to be 65px, i would like to see the text ass "experie"


    thanks

  4. #4
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: gridview width

    Hey,

    In which case, you are going to need to inspect the text that you are writing into the GridView, and make a decision about whether it needs to be truncated or not. You can do this in the RowDataBound event of the GridView.

    Alternatively, rather than use a BoundField, use a TemplateField, and in it's definition, add controls with the correct CSS style to clip the text that you are adding into the cell.

    Gary

  5. #5

    Thread Starter
    Frenzied Member met0555's Avatar
    Join Date
    Jul 2006
    Posts
    1,385

    Re: gridview width

    Code:
    In which case, you are going to need to inspect the text that you are writing into the GridView, and make a decision about whether it needs to be truncated or not. You can do this in the RowDataBound event of the GridView.
    Can you please give me hints on wha ti should write there?

    Thanks

  6. #6
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: gridview width

    Hey,

    Sure, here is the documentation for the RowDataBound event:

    http://msdn.microsoft.com/en-us/libr...databound.aspx

    Within this event, you can get access to the current Data Item that is being bound using:

    Code:
    e.Row.DataItem
    Here is the documentation:
    http://msdn.microsoft.com/en-us/libr....dataitem.aspx

    Grab the column of interest, you should know the name of this. Convert this to a string, and check the length. If it is longer than what you want it to be, only take the portion that you want, there are a number of static method on the string object that will help you here:

    http://msdn.microsoft.com/en-us/libr...g_members.aspx

    Once you have the truncated value, write it back into the cell, as shown in the first link.

    Gary

  7. #7

    Thread Starter
    Frenzied Member met0555's Avatar
    Join Date
    Jul 2006
    Posts
    1,385

    Re: gridview width

    Hi,

    I tried the following, but nothing is changing, is there something i forgot?

    Code:
     If e.Row.RowType = DataControlRowType.DataRow Then
    
                ' Display the company name in italics.
                ' e.Row.Cells(2).Text = "<i>" & e.Row.Cells(12).Text & "</i>"
                e.Row.Cells(12).Width = 30
            End If

  8. #8
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: gridview width

    Hey,

    Why are you changing the width? I thought you wanted to truncate the string?

    Have you set a breakpoint on that line? Is it getting hit?

    I don't think the above will work, as the width of the cell will be constrained by the containing element.

    Gary

  9. #9

    Thread Starter
    Frenzied Member met0555's Avatar
    Join Date
    Jul 2006
    Posts
    1,385

    Re: gridview width

    I also tried the substring but if there is a blank field it will show an error that why i tried with width.

  10. #10

    Thread Starter
    Frenzied Member met0555's Avatar
    Join Date
    Jul 2006
    Posts
    1,385

    Re: gridview width

    Even if there isn't a blank space or less amount of chars than the one wrote in the substring, it's not showing the correct number of chars .

    If e.Row.RowType = DataControlRowType.DataRow Then
    Dim A As String
    A = e.Row.Cells(3).Text.Length.ToString()

    e.Row.Cells(3).Text.Substring(1, 3)
    End If

  11. #11
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: gridview width

    Hey,

    That is because the code that you have written doesn't make any sense.

    Text.Substring returns a string, it doesn't alter the string that it is being worked on. As such, you would have to do something like:

    Code:
    e.Row.Cells(3).Text = e.Row.Cells(3).Text.Substring(1, 3)
    This was shown in the example that you pasted into post #7.

    Gary

    Gary

  12. #12
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618

    Re: gridview width

    Quote Originally Posted by met0555 View Post
    I also tried the substring but if there is a blank field it will show an error that why i tried with width.
    Check your data type before you do the substring. And if it is a string, make sure you check the length too before you do the substring. You can't do a substring(0,3) on a string that is 2 characters long.
    Sean

    Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.

  13. #13

    Thread Starter
    Frenzied Member met0555's Avatar
    Join Date
    Jul 2006
    Posts
    1,385

    Re: gridview width

    Thanks, this helped a lot

    vb Code:
    1. e.Row.Cells(3).Text = e.Row.Cells(3).Text.Substring(1, 3)

    i added and If to avoid error when field is blank...

    Thanks

  14. #14
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] gridview width

    Hey,

    Glad to hear you got it working!

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width