problem with grid

A

androoo

Hi

I am learing how to use the grid in asp.net.
Im trying to replace the button columns with nice friendly images.

So :
<asp:ButtonColumn Text="Delete"
CommandName="Delete"></asp:ButtonColumn>

would become :
<asp:TemplateColumn>
<ItemTemplate>
<asp:ImageButton id="cmdDel" runat="server" CommandName="Delete"
ImageUrl="pics/DELETE.JPG" CausesValidation="false"></asp:ImageButton>
</ItemTemplate>
</asp:TemplateColumn>


but i just get a message saying Specified cast is not valid.
any ideas on what i may be doing .. here is the complete listing of my
grid code when its working without the template column for the delete
button:

<asp:datagrid id="dgReportCode" runat="server"
OnItemDataBound="dgReportCode_ItemDataBound" DataKeyField="ID"
OnDeleteCommand="dgReportCode_Delete"
OnUpdateCommand="dgReportCode_Update"
OnCancelCommand="dgReportCode_Cancel" OnEditCommand="dgReportCode_Edit"
HeaderStyle-Font-Bold="True" HeaderStyle-HorizontalAlign="Center"
HeaderStyle-ForeColor="Black" HeaderStyle-BackColor="Gold"
AutoGenerateColumns="False" CellPadding="2" ForeColor="Black">
<HeaderStyle Font-Size="8pt" Font-Names="Verdana" Font-Bold="True"
HorizontalAlign="Center"
ForeColor="Gold"BackColor="Navy"></HeaderStyle>
<Columns>
<asp:ButtonColumn Text="Delete"
CommandName="Delete"></asp:ButtonColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:ImageButton id="ImageButton1" CommandName="Edit" runat="server"
ImageUrl="pics/NOTEBOOK.JPG"></asp:ImageButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton id="Button4" runat="server" Text="Update"
CommandName="Update" CausesValidation="false"></asp:LinkButton>
<asp:LinkButton id="Button3" runat="server" Text="Cancel"
CommandName="Cancel" CausesValidation="false"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="ReportName" HeaderText="Report
Name"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Code">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Code") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id=txtCode runat="server" Text='<%#
Container.DataItem("Code") %>' columns="70" TextMode="MultiLine"
Wrap="True" Rows="5"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="Version"
HeaderText="Ver"></asp:BoundColumn>
</Columns>
</asp:datagrid>
 
G

Guest

Hi androoo,

It seems some thing wrong in your code (c# or vb.net). Please post your code.

HTH

Elton Wang
(e-mail address removed)
 
A

androoo

Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.Common
Imports System.Data.SqlClient
Imports System.Data.SqlTypes
Public Class WebForm1
Inherits System.Web.UI.Page



#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents dgReportCode As
System.Web.UI.WebControls.DataGrid
Protected WithEvents cmbVersion As
System.Web.UI.WebControls.DropDownList
Protected WithEvents btnGo As System.Web.UI.WebControls.Button
Protected WithEvents cmbCountry As
System.Web.UI.WebControls.DropDownList
Protected WithEvents cmbModule As
System.Web.UI.WebControls.DropDownList

'NOTE: The following placeholder declaration is required by the Web
Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer

'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
LoadVersion()
LoadCountry()
LoadModule()
BindData(False)
End If
End Sub
Function LoadVersion()
Dim verArr As New ArrayList(3)
verArr.Add("Select all")
verArr.Add("8")
verArr.Add("9")
verArr.Add("10")
verArr.Add("11")
cmbVersion.DataSource = verArr
cmbVersion.DataBind()
cmbVersion.SelectedIndex = 1
End Function

Function LoadCountry()
Dim strSQL As String
Dim objData As clsDatabase
Dim dr As OleDb.OleDbDataReader

strSQL = "SELECT DISTINCT Country from Reports"

Try
objData = objData.GetInstance
dr = objData.GetRecordset(strSQL)
cmbCountry.DataSource = dr
cmbCountry.DataTextField = "Country"
cmbCountry.DataBind()
cmbCountry.Items.Insert(0, "Select all")

Catch objError As Exception
Exit Function ' and stop execution
End Try
End Function
Function LoadModule()
Dim strSQL As String
Dim objData As clsDatabase
Dim dr As OleDb.OleDbDataReader

strSQL = "SELECT DISTINCT Mamut_Module from Reports "
If cmbCountry.SelectedItem.Text <> "Select all" Then strSQL +=
" where country ='" & cmbCountry.SelectedItem.Text & "'"

Try
objData = objData.GetInstance
dr = objData.GetRecordset(strSQL)
cmbModule.DataSource = dr
cmbModule.DataValueField = "Mamut_Module"
cmbModule.DataTextField = "Mamut_Module"
cmbModule.DataBind()
cmbModule.SelectedIndex = 4
cmbModule.Items.Insert(0, "Select all")
Catch objError As Exception
Exit Function ' and stop execution
End Try
End Function
Function LoadName()
'Dim strSQL As String
'Dim objData As clsDatabase
'Dim dr As OleDb.OleDbDataReader

'strSQL = "SELECT DISTINCT ReportName from Reports where
country ='" & cmbCountry.SelectedItem.Text & "' and Mamut_Module ='" &
cmbModule.SelectedItem.Text & "'"

'Try
' objData = objData.GetInstance
' dr = objData.GetRecordset(strSQL)
' cmbName.DataSource = dr
' cmbName.DataTextField = "ReportName"
' cmbName.DataBind()

'Catch objError As Exception
' Exit Function ' and stop execution
'End Try
End Function
Sub BindData(ByVal blnShowall As Boolean)

Dim strSQL As String
Dim objData As clsDatabase
Dim dr As OleDb.OleDbDataReader

'build sql
strSQL += "select * from Reports where id > 0"
If cmbVersion.SelectedItem.Text <> "Select all" Then strSQL +=
" and version ='" & cmbVersion.SelectedItem.Text & "'"

If cmbCountry.SelectedItem.Text <> "Select all" Then strSQL +=
" and country ='" & cmbCountry.SelectedItem.Text & "'"

If cmbModule.SelectedItem.Text <> "Select all" Then strSQL += "
and Mamut_Module ='" & cmbModule.SelectedItem.Text & "'"

Try
objData = objData.GetInstance
dr = objData.GetRecordset(strSQL)
dgReportCode.DataSource = dr
dgReportCode.DataBind()

Catch objError As Exception
dgReportCode.Visible = False
Response.Write(objError.Message)
Response.End()
Exit Sub ' and stop execution
End Try

End Sub
Sub dgReportCode_Edit(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)
dgReportCode.EditItemIndex = e.Item.ItemIndex
BindData(False)
End Sub

Sub dgReportCode_Cancel(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)
dgReportCode.EditItemIndex = -1
BindData(False)
End Sub

Sub dgReportCode_Delete(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)
Dim iID As Integer = dgReportCode.DataKeys(e.Item.ItemIndex)
Dim conn As New SqlConnection("Persist Security
Info=False;Integrated Security=SSPI;database=TAG;server=Andys")
conn.Open()

Dim trans As SqlTransaction = conn.BeginTransaction()
Dim paramID As New SqlParameter("@ID", SqlDbType.Char, 20)
paramID.Value = iID

Try
' Perform the credit operation
SqlServer.ExecuteNonQuery(trans,
CommandType.StoredProcedure, "delete_ReportCode", paramID)
trans.Commit()

Catch ex As Exception
' throw an exception
trans.Rollback()
Throw ex
Finally
conn.Close()
conn = Nothing
End Try
BindData(False)
End Sub

Sub dgReportCode_Update(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)
'Read in the values of the updated row
'Dim iID As Integer = e.Item.Cells(2).Text
'Dim dWDate As Date = CType(e.Item.Cells(4).Controls(0),
TextBox).Text
'Dim iSun As Integer = CType(e.Item.Cells(5).Controls(0),
TextBox).Text
'Dim iWind As Integer = CType(e.Item.Cells(6).Controls(0),
TextBox).Text
'Dim strSql As String

'Dim strConnString As String =
ConfigurationSettings.AppSettings("connString")
'Dim objConn As New OleDbConnection(strConnString)
'objConn.Open()

'strSql = "Update weather set WDate ='" & dWDate & "',"
'strSql = strSql & "Sun = " & iSun & ","
'strSql = strSql & "Wind = " & iWind
'strSql = strSql & " where ID =" & iID

'Dim myCommand As OleDbCommand = New OleDbCommand(strSql,
objConn)
'myCommand.ExecuteNonQuery() 'Execute the UPDATE query
'objConn.Close() 'Close the connection
''Finally, set the EditItemIndex to -1 and rebind the DataGrid
'dgReportCode.EditItemIndex = -1
'BindData()

End Sub

Sub dgReportCode_ItemDataBound(ByVal sender As Object, ByVal e As
DataGridItemEventArgs)
' First, make sure we're NOT dealing with a Header or Footer
row
If e.Item.ItemType <> ListItemType.Header And _
e.Item.ItemType <> ListItemType.Footer Then
'Now, reference the LinkButton control that the Delete
ButtonColumn
'has been rendered to
Dim deleteButton As LinkButton =
e.Item.Cells(0).Controls(0)

'We can now add the onclick event handler
deleteButton.Attributes("onclick") = "javascript:return " &
_
"confirm('Are you sure you want to delete #" & _
DataBinder.Eval(e.Item.DataItem, "ID") & "?')"
End If
End Sub

Private Sub cmbModule_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
cmbModule.SelectedIndexChanged

LoadName()
BindData(False)
End Sub

Private Sub cmbVersion_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
cmbVersion.SelectedIndexChanged

BindData(False)
End Sub

Private Sub cmbCountry_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
cmbCountry.SelectedIndexChanged

BindData(False)
End Sub

Private Sub dgReportCode_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
dgReportCode.SelectedIndexChanged

End Sub
End Class
 
G

Guest

androoo said:
Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.Common
Imports System.Data.SqlClient
Imports System.Data.SqlTypes
Public Class WebForm1
Inherits System.Web.UI.Page



#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents dgReportCode As
System.Web.UI.WebControls.DataGrid
Protected WithEvents cmbVersion As
System.Web.UI.WebControls.DropDownList
Protected WithEvents btnGo As System.Web.UI.WebControls.Button
Protected WithEvents cmbCountry As
System.Web.UI.WebControls.DropDownList
Protected WithEvents cmbModule As
System.Web.UI.WebControls.DropDownList

'NOTE: The following placeholder declaration is required by the Web
Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer

'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
LoadVersion()
LoadCountry()
LoadModule()
BindData(False)
End If
End Sub
Function LoadVersion()
Dim verArr As New ArrayList(3)
verArr.Add("Select all")
verArr.Add("8")
verArr.Add("9")
verArr.Add("10")
verArr.Add("11")
cmbVersion.DataSource = verArr
cmbVersion.DataBind()
cmbVersion.SelectedIndex = 1
End Function

Function LoadCountry()
Dim strSQL As String
Dim objData As clsDatabase
Dim dr As OleDb.OleDbDataReader

strSQL = "SELECT DISTINCT Country from Reports"

Try
objData = objData.GetInstance
dr = objData.GetRecordset(strSQL)
cmbCountry.DataSource = dr
cmbCountry.DataTextField = "Country"
cmbCountry.DataBind()
cmbCountry.Items.Insert(0, "Select all")

Catch objError As Exception
Exit Function ' and stop execution
End Try
End Function
Function LoadModule()
Dim strSQL As String
Dim objData As clsDatabase
Dim dr As OleDb.OleDbDataReader

strSQL = "SELECT DISTINCT Mamut_Module from Reports "
If cmbCountry.SelectedItem.Text <> "Select all" Then strSQL +=
" where country ='" & cmbCountry.SelectedItem.Text & "'"

Try
objData = objData.GetInstance
dr = objData.GetRecordset(strSQL)
cmbModule.DataSource = dr
cmbModule.DataValueField = "Mamut_Module"
cmbModule.DataTextField = "Mamut_Module"
cmbModule.DataBind()
cmbModule.SelectedIndex = 4
cmbModule.Items.Insert(0, "Select all")
Catch objError As Exception
Exit Function ' and stop execution
End Try
End Function
Function LoadName()
'Dim strSQL As String
'Dim objData As clsDatabase
'Dim dr As OleDb.OleDbDataReader

'strSQL = "SELECT DISTINCT ReportName from Reports where
country ='" & cmbCountry.SelectedItem.Text & "' and Mamut_Module ='" &
cmbModule.SelectedItem.Text & "'"

'Try
' objData = objData.GetInstance
' dr = objData.GetRecordset(strSQL)
' cmbName.DataSource = dr
' cmbName.DataTextField = "ReportName"
' cmbName.DataBind()

'Catch objError As Exception
' Exit Function ' and stop execution
'End Try
End Function
Sub BindData(ByVal blnShowall As Boolean)

Dim strSQL As String
Dim objData As clsDatabase
Dim dr As OleDb.OleDbDataReader

'build sql
strSQL += "select * from Reports where id > 0"
If cmbVersion.SelectedItem.Text <> "Select all" Then strSQL +=
" and version ='" & cmbVersion.SelectedItem.Text & "'"

If cmbCountry.SelectedItem.Text <> "Select all" Then strSQL +=
" and country ='" & cmbCountry.SelectedItem.Text & "'"

If cmbModule.SelectedItem.Text <> "Select all" Then strSQL += "
and Mamut_Module ='" & cmbModule.SelectedItem.Text & "'"

Try
objData = objData.GetInstance
dr = objData.GetRecordset(strSQL)
dgReportCode.DataSource = dr
dgReportCode.DataBind()

Catch objError As Exception
dgReportCode.Visible = False
Response.Write(objError.Message)
Response.End()
Exit Sub ' and stop execution
End Try

End Sub
Sub dgReportCode_Edit(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)
dgReportCode.EditItemIndex = e.Item.ItemIndex
BindData(False)
End Sub

Sub dgReportCode_Cancel(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)
dgReportCode.EditItemIndex = -1
BindData(False)
End Sub

Sub dgReportCode_Delete(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)
Dim iID As Integer = dgReportCode.DataKeys(e.Item.ItemIndex)
Dim conn As New SqlConnection("Persist Security
Info=False;Integrated Security=SSPI;database=TAG;server=Andys")
conn.Open()

Dim trans As SqlTransaction = conn.BeginTransaction()
Dim paramID As New SqlParameter("@ID", SqlDbType.Char, 20)
paramID.Value = iID

Try
' Perform the credit operation
SqlServer.ExecuteNonQuery(trans,
CommandType.StoredProcedure, "delete_ReportCode", paramID)
trans.Commit()

Catch ex As Exception
' throw an exception
trans.Rollback()
Throw ex
Finally
conn.Close()
conn = Nothing
End Try
BindData(False)
End Sub

Sub dgReportCode_Update(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)
'Read in the values of the updated row
'Dim iID As Integer = e.Item.Cells(2).Text
'Dim dWDate As Date = CType(e.Item.Cells(4).Controls(0),
TextBox).Text
'Dim iSun As Integer = CType(e.Item.Cells(5).Controls(0),
TextBox).Text
'Dim iWind As Integer = CType(e.Item.Cells(6).Controls(0),
TextBox).Text
'Dim strSql As String

'Dim strConnString As String =
ConfigurationSettings.AppSettings("connString")
'Dim objConn As New OleDbConnection(strConnString)
'objConn.Open()

'strSql = "Update weather set WDate ='" & dWDate & "',"
'strSql = strSql & "Sun = " & iSun & ","
'strSql = strSql & "Wind = " & iWind
'strSql = strSql & " where ID =" & iID

'Dim myCommand As OleDbCommand = New OleDbCommand(strSql,
objConn)
'myCommand.ExecuteNonQuery() 'Execute the UPDATE query
'objConn.Close() 'Close the connection
''Finally, set the EditItemIndex to -1 and rebind the DataGrid
'dgReportCode.EditItemIndex = -1
'BindData()

End Sub

Sub dgReportCode_ItemDataBound(ByVal sender As Object, ByVal e As
DataGridItemEventArgs)
' First, make sure we're NOT dealing with a Header or Footer
row
If e.Item.ItemType <> ListItemType.Header And _
e.Item.ItemType <> ListItemType.Footer Then
'Now, reference the LinkButton control that the Delete
ButtonColumn
'has been rendered to
Dim deleteButton As LinkButton =
e.Item.Cells(0).Controls(0)

'We can now add the onclick event handler
deleteButton.Attributes("onclick") = "javascript:return " &
_
"confirm('Are you sure you want to delete #" & _
DataBinder.Eval(e.Item.DataItem, "ID") & "?')"
End If
End Sub

Private Sub cmbModule_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
cmbModule.SelectedIndexChanged

LoadName()
BindData(False)
End Sub

Private Sub cmbVersion_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
cmbVersion.SelectedIndexChanged

BindData(False)
End Sub

Private Sub cmbCountry_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
cmbCountry.SelectedIndexChanged

BindData(False)
End Sub

Private Sub dgReportCode_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
dgReportCode.SelectedIndexChanged

End Sub
End Class
 
G

Guest

Hi Andro,

From your code it's still hard to say what is wrong. If you can tell in
witch line error is occurred, it may help to find out what causes error.

Generally speaking, when you use button (link button, image button, and push
button) in TemplateColumn, it's better to use datagrid_ItemCommand event to
implement logic. In the event, you can use e.CommandName to distinguish
different button. e.g.
If e.CommandName.Equals("Delete") Then
' Process deleting
Else
' other function
End If


HTH

Elton Wang
 

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

Top