Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Datagrid Control > Filling Datagrid

Reply
Thread Tools

Filling Datagrid

 
 
Aaron
Guest
Posts: n/a
 
      08-16-2004
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


 
Reply With Quote
 
 
 
 
Ken Cox [Microsoft MVP]
Guest
Posts: n/a
 
      08-16-2004
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) =
"Itemxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxx
" + 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" <> wrote in message
news:e54%...
>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
>
>


 
Reply With Quote
 
 
 
 
Aaron
Guest
Posts: n/a
 
      08-17-2004
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 [Microsoft MVP]" <> wrote in message
news:ecJvu%...
> 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) =
>

"Itemxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxx
> " + 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" <> wrote in message
> news:e54%...
> >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
> >
> >

>



 
Reply With Quote
 
Ken Cox [Microsoft MVP]
Guest
Posts: n/a
 
      08-17-2004
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" <> wrote in message
news:%...
> 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 [Microsoft MVP]" <> wrote in message
> news:ecJvu%...
>> 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) =
>>

> "Itemxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxx
>> " + 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" <> wrote in message
>> news:e54%...
>> >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
>> >
>> >

>>

>
>


 
Reply With Quote
 
Aaron
Guest
Posts: n/a
 
      08-18-2004
Yeah, I guess that would be the easiest way. Thanks, I'll give it a shot.

Aaron


"Ken Cox [Microsoft MVP]" <> wrote in message
news:...
> 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" <> wrote in message
> news:%...
> > 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 [Microsoft MVP]" <> wrote in message
> > news:ecJvu%...
> >> 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) =
> >>

> >

"Itemxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxx
> >> " + 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" <> wrote in message
> >> news:e54%...
> >> >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
> >> >
> >> >
> >>

> >
> >

>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Filling a listbox with the values in a enumeration Timothy Parez ASP .Net 3 06-29-2010 05:59 AM
Datagrid not completely filling in webusercontrol page 1 in 4 times. nigel@idsol.co.uk ASP .Net 0 12-21-2005 08:04 AM
Trouble Filling DropDownList in Datagrid David Londeck ASP .Net Web Controls 1 03-28-2005 02:56 PM
Filling One DataGrid Based on Selection from Another DataGrid - Both in Separate User Controls thegame ASP .Net 1 02-28-2005 04:23 PM
ASP.NET Datagrid filling textboxes Aaron ASP .Net Datagrid Control 2 07-19-2004 09:26 PM



Advertisments