Filling Datagrid

Discussion in 'ASP .Net Datagrid Control' started by Aaron, Aug 16, 2004.

  1. Aaron

    Aaron Guest

    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
     
    Aaron, Aug 16, 2004
    #1
    1. Advertising

  2. 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" <> 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
    >
    >
     
    Ken Cox [Microsoft MVP], Aug 16, 2004
    #2
    1. Advertising

  3. Aaron

    Aaron Guest

    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) =
    >

    "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" <> 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
    > >
    > >

    >
     
    Aaron, Aug 17, 2004
    #3
  4. 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) =
    >>

    > "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" <> 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
    >> >
    >> >

    >>

    >
    >
     
    Ken Cox [Microsoft MVP], Aug 17, 2004
    #4
  5. Aaron

    Aaron Guest

    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) =
    > >>

    > >

    "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" <> 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
    > >> >
    > >> >
    > >>

    > >
    > >

    >
     
    Aaron, Aug 18, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. thegame
    Replies:
    1
    Views:
    957
    =?Utf-8?B?ZGlyazQx?=
    Feb 28, 2005
  2. Replies:
    0
    Views:
    362
  3. Aaron

    ASP.NET Datagrid filling textboxes

    Aaron, Jul 19, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    2
    Views:
    182
    Aaron
    Jul 19, 2004
  4. Mohsen Pahlevanzadeh
    Replies:
    0
    Views:
    134
    Mohsen Pahlevanzadeh
    Sep 9, 2013
  5. MRAB
    Replies:
    0
    Views:
    128
Loading...

Share This Page