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.AppS ettings("C onnectionS tring")
Public objConnection As New SqlConnection(strConnectio n)
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()
'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.AppS
Public objConnection As New SqlConnection(strConnectio
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()
You need to use T-SQL SubString function or use OnDataBound event for each row.
ASKER
I'm new to this; how is that done?
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.lastin dexof("\") )
cell value = temp
dim temp as string = row("fileName")
temp = temp.substring(temp.lastin
cell value = temp
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. DataGridIt emEventArg s) Handles dg.ItemDataBound
With e.Item
If .ItemType = ListItemType.AlternatingIt em OrElse .ItemType = ListItemType.Item Then
Dim row as DataRow = CType(.DataItem, DataRowView).Row
Dim temp as string = row("fileName")
temp = temp.substring(temp.lastin dexof("\") )
CType(e.Item.Cells(0).Find Control("c ontrolname "), Label).Text= temp
End If
End With
End Sub
' 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.
With e.Item
If .ItemType = ListItemType.AlternatingIt
Dim row as DataRow = CType(.DataItem, DataRowView).Row
Dim temp as string = row("fileName")
temp = temp.substring(temp.lastin
CType(e.Item.Cells(0).Find
End If
End With
End Sub
ASKER
This is what I did:
Private Sub dgJobExe_ItemDataBound(ByV al sender As Object, ByVal e As System.Web.UI.WebControls. DataGridIt emEventArg s) Handles dgJobExe.ItemDataBound
With e.Item
If .ItemType = ListItemType.AlternatingIt em OrElse .ItemType = ListItemType.Item Then
Dim row As DataRow = CType(.DataItem, DataRowView).Row
Dim temp As String = row("fileName")
temp = temp.Substring(temp.LastIn dexOf("\") )
CType(e.Item.Cells(0).Find Control("J obExe"), Label).Text = temp
End If
End With
End Sub
and it gave me an erorr:
'c:\temp\testjob.exe
'System.InvalidCastExcepti on: Specified cast is not valid. at
'TreeMenu1.WebForm1.dgJobE xe_ItemDat aBound(Obj ect sender, DataGridItemEventArgs e)
'in C:\Inetpub\wwwroot\WebForm 1.aspx.vb: line 73 at
'System.Web.UI.WebControls .DataGrid. OnItemData Bound(Data GridItemEv entArgs 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. CreateCont rolHierarc hy(Boolean useDataSource) at
'System.Web.UI.WebControls .BaseDataL ist.OnData Binding(Ev entArgs e) at
'System.Web.UI.WebControls .BaseDataL ist.DataBi nd() at TreeMenu1.WebForm1.BindDat a() in
'C:\Inetpub\wwwroot\WebFor m1.aspx.vb :line 48
Private Sub dgJobExe_ItemDataBound(ByV
With e.Item
If .ItemType = ListItemType.AlternatingIt
Dim row As DataRow = CType(.DataItem, DataRowView).Row
Dim temp As String = row("fileName")
temp = temp.Substring(temp.LastIn
CType(e.Item.Cells(0).Find
End If
End With
End Sub
and it gave me an erorr:
'c:\temp\testjob.exe
'System.InvalidCastExcepti
'TreeMenu1.WebForm1.dgJobE
'in C:\Inetpub\wwwroot\WebForm
'System.Web.UI.WebControls
'System.Web.UI.WebControls
'ListItemType itemType, Boolean dataBind, Object dataItem, DataGridColumn[] columns,
'TableRowCollection rows, PagedDataSource pagedDataSource) at
'System.Web.UI.WebControls
'System.Web.UI.WebControls
'System.Web.UI.WebControls
'C:\Inetpub\wwwroot\WebFor
It might help if you indicated which line is line 73 in your code...
ASKER
temp = temp.Substring(temp.LastIn dexOf("\") )
ASKER
here is where I'm at:
Public strConnection As String = ConfigurationSettings.AppS ettings("C onnectionS tring")
Public objConnection As New SqlConnection(strConnectio n)
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(ByV al sender As Object, ByVal e As System.Web.UI.WebControls. DataGridIt emEventArg s) Handles dgJobExe.ItemDataBound
With e.Item
If .ItemType = ListItemType.AlternatingIt em OrElse .ItemType = ListItemType.Item Then
Dim row As DataRow = CType(.DataItem, DataRowView).Row
Dim temp As String = row("fileName")
temp = temp.Substring(temp.LastIn dexOf("\") )
CType(e.Item.Cells(0).Find Control("J obExe"), Label).Text = temp
End If
End With
End Sub
Public strConnection As String = ConfigurationSettings.AppS
Public objConnection As New SqlConnection(strConnectio
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(ByV
With e.Item
If .ItemType = ListItemType.AlternatingIt
Dim row As DataRow = CType(.DataItem, DataRowView).Row
Dim temp As String = row("fileName")
temp = temp.Substring(temp.LastIn
CType(e.Item.Cells(0).Find
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(ByV al sender As Object, ByVal e As System.Web.UI.WebControls. DataGridIt emEventArg s) Handles dgJobExe.ItemDataBound
With e.Item
If .ItemType = ListItemType.AlternatingIt em OrElse .ItemType = ListItemType.Item Then
Dim row As DataRow = CType(.DataItem, DataRowView).Row
Dim temp As String = row("fileName")
temp = temp.Substring(temp.LastIn dexOf("\"c ) + 1)
CType(e.Item.Cells(0).Find Control("J obExe"), Label).Text = temp
End If
End With
End Sub
There's a couple minor tweaks that need to be made (but they shouldn't affect this issue)
Private Sub dgJobExe_ItemDataBound(ByV
With e.Item
If .ItemType = ListItemType.AlternatingIt
Dim row As DataRow = CType(.DataItem, DataRowView).Row
Dim temp As String = row("fileName")
temp = temp.Substring(temp.LastIn
CType(e.Item.Cells(0).Find
End If
End With
End Sub
ASKER
I still got the same error
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.