Table or Datagrid

K

Khumar

Hello,

Im new in asp.net especially in web base application.
I want to ask about datagrid. I had shown my table on
datagrid, and what I want to ask is how to set the column
height fixed? Because when I have a long word it
automatically set new line instead of hide it. I want it
to be hide like this 928192... with 3 dot at the end of
the word if it is to long.

Is it possible to do it?

Thanks alll
 
E

Eliyahu Goldin

You need to write a method that will take a string as a parameter and return
a truncated string with dots at the end if the input string is too long.
Having done that, you can use the string in your databind expression.

Alternatively you can handle ItemDataBound event to catch the cell value and
to truncate it in the code using the same function.

Eliyahu
 
K

Ken Cox [Microsoft MVP]

The easiest way is to use a helper function that trims the text to the
correct length and adds the ellipsis. Here's some code that uses only the
first ten characters, let us know if it helps?

Ken
Microsoft MVP [ASP.NET]
Toronto

<asp:datagrid id="DataGrid1" runat="server"
AutoGenerateColumns="False" PageSize="3">
<columns>
<asp:templatecolumn>
<itemtemplate>
<asp:label runat="server" Text='<%#
FixLongLine(DataBinder.Eval(Container, "DataItem.StringValue")) %>'
ID="Label1">
</asp:label>
</itemtemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
If Not IsPostBack Then
DataGrid1.DataSource = CreateDataSource()
DataGrid1.DataBind()
End If
End Sub

Public Function FixLongLine(ByVal strIn) As String
If Len(strIn) > 10 Then
Return Left(strIn, 10) & "..."
End If
Return strIn
End Function

Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i
dr(1) =
"Itemxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
" + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource
 
K

Khumar

Hello Ken,

Im trying your code and its work, but it doesnt work with
my code. This is how I connect to database. I create a
class which allow connection to database. In class I
create dataset module so it return dataset.
I connect from database to datagrid is like this
dataGrid1.DataSource = custInfo.getCust("Select *
From Customers")
dataGrid1.DataBind()

When I try to add your code:
<itemtemplate>
<asp:label runat="server"
Text='<%#
FixLongLine(DataBinder.Eval
(Container, "DataItem.StringValue")) %>'
ID="Label1">
</asp:label>
</itemtemplate>
</asp:templatecolumn>

the error shows in DataBinder.Eval:
DataBinder.Eval: 'System.Data.DataRowView' does not
contain a property with the name StringValue.

Is there any problem with my connection to database?
Thanks
-----Original Message-----
The easiest way is to use a helper function that trims the text to the
correct length and adds the ellipsis. Here's some code that uses only the
first ten characters, let us know if it helps?

Ken
Microsoft MVP [ASP.NET]
Toronto

<asp:datagrid id="DataGrid1" runat="server"
AutoGenerateColumns="False" PageSize="3">
<columns>
<asp:templatecolumn>
<itemtemplate>
<asp:label runat="server" Text='<%#
FixLongLine(DataBinder.Eval
(Container, "DataItem.StringValue")) %>'
 
M

Matt Berther

Hello Ken Cox [Microsoft MVP],

Instead of "..." you could also use "&hellip;", which is the HTML code for
a horizontal ellipsis.

--
Matt Berther
http://www.mattberther.com
The easiest way is to use a helper function that trims the text to the
correct length and adds the ellipsis. Here's some code that uses only
the first ten characters, let us know if it helps?

Ken
Microsoft MVP [ASP.NET]
Toronto
<asp:datagrid id="DataGrid1" runat="server"
AutoGenerateColumns="False" PageSize="3">
<columns>
<asp:templatecolumn>
<itemtemplate>
<asp:label runat="server" Text='<%#
FixLongLine(DataBinder.Eval(Container, "DataItem.StringValue")) %>'
ID="Label1">
</asp:label>
</itemtemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>
Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
If Not IsPostBack Then
DataGrid1.DataSource = CreateDataSource()
DataGrid1.DataBind()
End If
End Sub
Public Function FixLongLine(ByVal strIn) As String
If Len(strIn) > 10 Then
Return Left(strIn, 10) & "..."
End If
Return strIn
End Function
Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i
dr(1) =
"Itemxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxx
" + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource
Hello,

Im new in asp.net especially in web base application.
I want to ask about datagrid. I had shown my table on
datagrid, and what I want to ask is how to set the column
height fixed? Because when I have a long word it
automatically set new line instead of hide it. I want it
to be hide like this 928192... with 3 dot at the end of
the word if it is to long.
Is it possible to do it?

Thanks alll
 
K

Ken Cox [Microsoft MVP]

You need to tell the helper the name of *your* data item. Mine was
"StringValue", yours could be "name". If so, replace StringValue with name.

DataItem.StringValue

Let us know?

Ken

Khumar said:
Hello Ken,

Im trying your code and its work, but it doesnt work with
my code. This is how I connect to database. I create a
class which allow connection to database. In class I
create dataset module so it return dataset.
I connect from database to datagrid is like this
dataGrid1.DataSource = custInfo.getCust("Select *
From Customers")
dataGrid1.DataBind()

When I try to add your code:
<itemtemplate>
<asp:label runat="server"
Text='<%#
FixLongLine(DataBinder.Eval
(Container, "DataItem.StringValue")) %>'
ID="Label1">
</asp:label>
</itemtemplate>
</asp:templatecolumn>

the error shows in DataBinder.Eval:
DataBinder.Eval: 'System.Data.DataRowView' does not
contain a property with the name StringValue.

Is there any problem with my connection to database?
Thanks
-----Original Message-----
The easiest way is to use a helper function that trims the text to the
correct length and adds the ellipsis. Here's some code that uses only the
first ten characters, let us know if it helps?

Ken
Microsoft MVP [ASP.NET]
Toronto

<asp:datagrid id="DataGrid1" runat="server"
AutoGenerateColumns="False" PageSize="3">
<columns>
<asp:templatecolumn>
<itemtemplate>
<asp:label runat="server" Text='<%#
FixLongLine(DataBinder.Eval
(Container, "DataItem.StringValue")) %>'
ID="Label1">
</asp:label>
</itemtemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
If Not IsPostBack Then
DataGrid1.DataSource = CreateDataSource()
DataGrid1.DataBind()
End If
End Sub

Public Function FixLongLine(ByVal strIn) As String
If Len(strIn) > 10 Then
Return Left(strIn, 10) & "..."
End If
Return strIn
End Function

Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i
dr(1) =
"Itemxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxx
" + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource




.
 
G

Guest

Ken,

Could you tell me how could I enable edit in template column field. I have a
edit butten in datagrid, when I click it, data bound column change to textbox
but not template column. I have table in template column. Here is my code:

Thanks for help,
cms123

<asp:TemplateColumn HeaderText="Address Information">
<ItemTemplate>
<table border="0">
<tr>
<td align="right"><b>Street:</b></td>
<td><%# DataBinder.Eval(Container.DataItem, "Street") %></td>
</tr>
<tr>
<td align="right"><b>City:</b></td>
<td><%# DataBinder.Eval(Container.DataItem, "City") %></td>
</tr>
<tr>
<td align="right"><b>State:</b></td>
<td><%# DataBinder.Eval(Container.DataItem, "State") %></td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateColumn>




Ken Cox said:
You need to tell the helper the name of *your* data item. Mine was
"StringValue", yours could be "name". If so, replace StringValue with name.

DataItem.StringValue

Let us know?

Ken

Khumar said:
Hello Ken,

Im trying your code and its work, but it doesnt work with
my code. This is how I connect to database. I create a
class which allow connection to database. In class I
create dataset module so it return dataset.
I connect from database to datagrid is like this
dataGrid1.DataSource = custInfo.getCust("Select *
From Customers")
dataGrid1.DataBind()

When I try to add your code:
<itemtemplate>
<asp:label runat="server"
Text='<%#
FixLongLine(DataBinder.Eval
(Container, "DataItem.StringValue")) %>'
ID="Label1">
</asp:label>
</itemtemplate>
</asp:templatecolumn>

the error shows in DataBinder.Eval:
DataBinder.Eval: 'System.Data.DataRowView' does not
contain a property with the name StringValue.

Is there any problem with my connection to database?
Thanks
-----Original Message-----
The easiest way is to use a helper function that trims the text to the
correct length and adds the ellipsis. Here's some code that uses only the
first ten characters, let us know if it helps?

Ken
Microsoft MVP [ASP.NET]
Toronto

<asp:datagrid id="DataGrid1" runat="server"
AutoGenerateColumns="False" PageSize="3">
<columns>
<asp:templatecolumn>
<itemtemplate>
<asp:label runat="server" Text='<%#
FixLongLine(DataBinder.Eval
(Container, "DataItem.StringValue")) %>'
ID="Label1">
</asp:label>
</itemtemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
If Not IsPostBack Then
DataGrid1.DataSource = CreateDataSource()
DataGrid1.DataBind()
End If
End Sub

Public Function FixLongLine(ByVal strIn) As String
If Len(strIn) > 10 Then
Return Left(strIn, 10) & "..."
End If
Return strIn
End Function

Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i
dr(1) =
"Itemxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxx
" + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource


Hello,

Im new in asp.net especially in web base application.
I want to ask about datagrid. I had shown my table on
datagrid, and what I want to ask is how to set the column
height fixed? Because when I have a long word it
automatically set new line instead of hide it. I want it
to be hide like this 928192... with 3 dot at the end of
the word if it is to long.

Is it possible to do it?

Thanks alll

.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,163
Latest member
Sasha15427
Top