Filling Datagrid

A

Aaron

I am programmatically adding columns to my datagrid and that is working very
well.
I am using this code to add the column:
Dim Activity_Notes As New BoundColumn()
With Activity_Notes
..DataField = "Activity_Notes"
..HeaderText = "Notes"
End With
datagrid.columns.add(Activity_Notes)
datagrid.bind()

The data in this cell is very long, and I wish to only display the first 25
chars. What is the best way to do this?

I do not want to only select the first 25 chars from sql...

Thanks,
Aaron
 
K

Ken Cox [Microsoft MVP]

Hi Aaron,

During the ItemDataBound event, you get a reference to the row that is
getting data. At that time you can get a reference to the cell's text and
take the Left 25 characters. Some code to show this is below.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]
Toronto


Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
If Not IsPostBack Then
Dim Activity_Notes As New BoundColumn
With Activity_Notes
.DataField = "Activity_Notes"
.HeaderText = "Notes"
End With
DataGrid1.Columns.Add(Activity_Notes)
DataGrid1.DataSource = CreateDataSource()
DataGrid1.AutoGenerateColumns = False
DataGrid1.DataBind()
End If
End Sub
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 _
("Activity_Notes", 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


Private Sub DataGrid1_ItemDataBound _
(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
Handles DataGrid1.ItemDataBound
e.Item.Cells(0).Text = Left(e.Item.Cells(0).Text, 25)
End Sub
 
A

Aaron

Yes, this helps, but it is also perfoming this function on the column
headers. Is there any way to prevent this?
Thanks,
Aaron


Ken Cox said:
Hi Aaron,

During the ItemDataBound event, you get a reference to the row that is
getting data. At that time you can get a reference to the cell's text and
take the Left 25 characters. Some code to show this is below.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]
Toronto


Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
If Not IsPostBack Then
Dim Activity_Notes As New BoundColumn
With Activity_Notes
.DataField = "Activity_Notes"
.HeaderText = "Notes"
End With
DataGrid1.Columns.Add(Activity_Notes)
DataGrid1.DataSource = CreateDataSource()
DataGrid1.AutoGenerateColumns = False
DataGrid1.DataBind()
End If
End Sub
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 _
("Activity_Notes", 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


Private Sub DataGrid1_ItemDataBound _
(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
Handles DataGrid1.ItemDataBound
e.Item.Cells(0).Text = Left(e.Item.Cells(0).Text, 25)
End Sub

Aaron said:
I am programmatically adding columns to my datagrid and that is working
very
well.
I am using this code to add the column:
Dim Activity_Notes As New BoundColumn()
With Activity_Notes
.DataField = "Activity_Notes"
.HeaderText = "Notes"
End With
datagrid.columns.add(Activity_Notes)
datagrid.bind()

The data in this cell is very long, and I wish to only display the first
25
chars. What is the best way to do this?

I do not want to only select the first 25 chars from sql...

Thanks,
Aaron
 
K

Ken Cox [Microsoft MVP]

Hi Aaron,

Just add in a check for the item type so that it only looks at regular rows,
not the header or footer:

Private Sub DataGrid1_ItemDataBound _
(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
Handles DataGrid1.ItemDataBound
If e.Item.ItemType = ListItemType.AlternatingItem Or _
e.Item.ItemType = ListItemType.Item Then
e.Item.Cells(0).Text = Left(e.Item.Cells(0).Text, 10)
End If
End Sub

Ken

Aaron said:
Yes, this helps, but it is also perfoming this function on the column
headers. Is there any way to prevent this?
Thanks,
Aaron


Ken Cox said:
Hi Aaron,

During the ItemDataBound event, you get a reference to the row that is
getting data. At that time you can get a reference to the cell's text and
take the Left 25 characters. Some code to show this is below.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]
Toronto


Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
If Not IsPostBack Then
Dim Activity_Notes As New BoundColumn
With Activity_Notes
.DataField = "Activity_Notes"
.HeaderText = "Notes"
End With
DataGrid1.Columns.Add(Activity_Notes)
DataGrid1.DataSource = CreateDataSource()
DataGrid1.AutoGenerateColumns = False
DataGrid1.DataBind()
End If
End Sub
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 _
("Activity_Notes", 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


Private Sub DataGrid1_ItemDataBound _
(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
Handles DataGrid1.ItemDataBound
e.Item.Cells(0).Text = Left(e.Item.Cells(0).Text, 25)
End Sub

Aaron said:
I am programmatically adding columns to my datagrid and that is working
very
well.
I am using this code to add the column:
Dim Activity_Notes As New BoundColumn()
With Activity_Notes
.DataField = "Activity_Notes"
.HeaderText = "Notes"
End With
datagrid.columns.add(Activity_Notes)
datagrid.bind()

The data in this cell is very long, and I wish to only display the
first
25
chars. What is the best way to do this?

I do not want to only select the first 25 chars from sql...

Thanks,
Aaron
 
A

Aaron

Yeah, I guess that would be the easiest way. Thanks, I'll give it a shot.

Aaron


Ken Cox said:
Hi Aaron,

Just add in a check for the item type so that it only looks at regular rows,
not the header or footer:

Private Sub DataGrid1_ItemDataBound _
(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
Handles DataGrid1.ItemDataBound
If e.Item.ItemType = ListItemType.AlternatingItem Or _
e.Item.ItemType = ListItemType.Item Then
e.Item.Cells(0).Text = Left(e.Item.Cells(0).Text, 10)
End If
End Sub

Ken

Aaron said:
Yes, this helps, but it is also perfoming this function on the column
headers. Is there any way to prevent this?
Thanks,
Aaron


Ken Cox said:
Hi Aaron,

During the ItemDataBound event, you get a reference to the row that is
getting data. At that time you can get a reference to the cell's text and
take the Left 25 characters. Some code to show this is below.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]
Toronto


Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
If Not IsPostBack Then
Dim Activity_Notes As New BoundColumn
With Activity_Notes
.DataField = "Activity_Notes"
.HeaderText = "Notes"
End With
DataGrid1.Columns.Add(Activity_Notes)
DataGrid1.DataSource = CreateDataSource()
DataGrid1.AutoGenerateColumns = False
DataGrid1.DataBind()
End If
End Sub
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 _
("Activity_Notes", 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


Private Sub DataGrid1_ItemDataBound _
(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
Handles DataGrid1.ItemDataBound
e.Item.Cells(0).Text = Left(e.Item.Cells(0).Text, 25)
End Sub

I am programmatically adding columns to my datagrid and that is working
very
well.
I am using this code to add the column:
Dim Activity_Notes As New BoundColumn()
With Activity_Notes
.DataField = "Activity_Notes"
.HeaderText = "Notes"
End With
datagrid.columns.add(Activity_Notes)
datagrid.bind()

The data in this cell is very long, and I wish to only display the
first
25
chars. What is the best way to do this?

I do not want to only select the first 25 chars from sql...

Thanks,
Aaron
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top