|
-
Mar 13th, 2010, 01:34 PM
#1
Thread Starter
Frenzied Member
[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>
-
Mar 14th, 2010, 05:22 AM
#2
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
-
Mar 14th, 2010, 06:07 AM
#3
Thread Starter
Frenzied Member
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
-
Mar 14th, 2010, 06:14 AM
#4
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
-
Mar 16th, 2010, 04:05 PM
#5
Thread Starter
Frenzied Member
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
-
Mar 17th, 2010, 02:29 AM
#6
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:
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
-
Mar 17th, 2010, 11:59 AM
#7
Thread Starter
Frenzied Member
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
-
Mar 17th, 2010, 12:31 PM
#8
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
-
Mar 17th, 2010, 03:45 PM
#9
Thread Starter
Frenzied Member
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.
-
Mar 17th, 2010, 04:49 PM
#10
Thread Starter
Frenzied Member
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
-
Mar 18th, 2010, 03:02 AM
#11
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
-
Mar 18th, 2010, 12:12 PM
#12
Frenzied Member
Re: gridview width
 Originally Posted by met0555
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.
-
Mar 18th, 2010, 12:56 PM
#13
Thread Starter
Frenzied Member
Re: gridview width
Thanks, this helped a lot
vb Code:
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
-
Mar 18th, 2010, 04:35 PM
#14
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|