Link to home
Start Free TrialLog in
Avatar of fwsteal
fwsteal

asked on

trim datagrid contents

I've got to read a talbe that contains the following data into a datagrid:

        'c:\temp\testjob.exe
        'c:\temp\testjob1.exe

What I'd like to do is read just the name of the job into the datagrid as opposed to the entire string such as:
testjob.exe
testjob1.exe

How could this be done?

Public strConnection As String = ConfigurationSettings.AppSettings("ConnectionString")
Public objConnection As New SqlConnection(strConnection)
Public strSQL As String

strSQL = "Select Distinct JobEXE From JbExe"
Dim objCommand As New SqlCommand(strSQL, objConnection)
objConnection.Open()
dgJobExe.DataSource = objCommand.ExecuteReader()
dgJobExe.DataBind()
objConnection.Close()

Avatar of dba_netanya
dba_netanya

You need to use T-SQL SubString function or use OnDataBound event for each row.
Avatar of fwsteal

ASKER

I'm new to this; how is that done?
Avatar of fwsteal

ASKER

also, I'll never know the exact length of the strings?
In your datagrid OnItemDataBound event, set the value of the cell something like this:

dim temp as string = row("fileName")
temp = temp.substring(temp.lastindexof("\"))
cell value = temp
Avatar of fwsteal

ASKER

chaosian, not sure I'm following you

' dg is a datagrid
' controlname is the name of the label whose value you want to set
Private Sub dg_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dg.ItemDataBound
  With e.Item
    If .ItemType = ListItemType.AlternatingItem OrElse .ItemType = ListItemType.Item Then
      Dim row as DataRow = CType(.DataItem, DataRowView).Row
      Dim temp as string = row("fileName")
      temp = temp.substring(temp.lastindexof("\"))
      CType(e.Item.Cells(0).FindControl("controlname"), Label).Text= temp
    End If
  End With
End Sub
Avatar of fwsteal

ASKER

This is what I did:

    Private Sub dgJobExe_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgJobExe.ItemDataBound
        With e.Item
            If .ItemType = ListItemType.AlternatingItem OrElse .ItemType = ListItemType.Item Then
                Dim row As DataRow = CType(.DataItem, DataRowView).Row
                Dim temp As String = row("fileName")
                temp = temp.Substring(temp.LastIndexOf("\"))
                CType(e.Item.Cells(0).FindControl("JobExe"), Label).Text = temp
            End If
        End With
    End Sub


and it gave me an erorr:

    'c:\temp\testjob.exe
    'System.InvalidCastException: Specified cast is not valid. at
    'TreeMenu1.WebForm1.dgJobExe_ItemDataBound(Object sender, DataGridItemEventArgs e)
    'in C:\Inetpub\wwwroot\WebForm1.aspx.vb:line 73 at
    'System.Web.UI.WebControls.DataGrid.OnItemDataBound(DataGridItemEventArgs e) at
    'System.Web.UI.WebControls.DataGrid.CreateItem(Int32 itemIndex, Int32 dataSourceIndex,
    'ListItemType itemType, Boolean dataBind, Object dataItem, DataGridColumn[] columns,
    'TableRowCollection rows, PagedDataSource pagedDataSource) at
    'System.Web.UI.WebControls.DataGrid.CreateControlHierarchy(Boolean useDataSource) at
    'System.Web.UI.WebControls.BaseDataList.OnDataBinding(EventArgs e) at
    'System.Web.UI.WebControls.BaseDataList.DataBind() at TreeMenu1.WebForm1.BindData() in
    'C:\Inetpub\wwwroot\WebForm1.aspx.vb:line 48
It might help if you indicated which line is line 73 in your code...
Avatar of fwsteal

ASKER

               temp = temp.Substring(temp.LastIndexOf("\"))
Avatar of fwsteal

ASKER

here is where I'm at:

Public strConnection As String = ConfigurationSettings.AppSettings("ConnectionString")
Public objConnection As New SqlConnection(strConnection)
Public strSQL As String

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
     If Not Page.IsPostBack Then
       BindData()
     End If
End Sub

Public Sub BindData() 'lets bind record info to a datagrid
     strSQL = "Select Distinct JobEXE From JbExe"
     Dim objCommand As New SqlCommand(strSQL, objConnection)
     objConnection.Open()
     dgJobExe.DataSource = objCommand.ExecuteReader()
     dgJobExe.DataBind()
     objConnection.Close()
End Sub

Private Sub dgJobExe_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgJobExe.ItemDataBound
     With e.Item
        If .ItemType = ListItemType.AlternatingItem OrElse .ItemType = ListItemType.Item Then
          Dim row As DataRow = CType(.DataItem, DataRowView).Row
          Dim temp As String = row("fileName")
          temp = temp.Substring(temp.LastIndexOf("\"))
          CType(e.Item.Cells(0).FindControl("JobExe"), Label).Text = temp
        End If
     End With
End Sub
Funny... I can't get it to throw an error.

There's a couple minor tweaks that need to be made (but they shouldn't affect this issue)

Private Sub dgJobExe_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgJobExe.ItemDataBound
        With e.Item
            If .ItemType = ListItemType.AlternatingItem OrElse .ItemType = ListItemType.Item Then
                Dim row As DataRow = CType(.DataItem, DataRowView).Row
                Dim temp As String = row("fileName")
                temp = temp.Substring(temp.LastIndexOf("\"c) + 1)
                CType(e.Item.Cells(0).FindControl("JobExe"), Label).Text = temp
            End If
        End With
    End Sub
Avatar of fwsteal

ASKER

I still got the same error
ASKER CERTIFIED SOLUTION
Avatar of Jeff Certain
Jeff Certain
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial