Click to See Complete Forum and Search --> : check boxes in datagrid


Huzaifa
April 5th, 2003, 05:33 AM
i want to use check boxes in datagird
the following code is VB
i tried converting the code in C#
but it is giving errors

1:Class is found when variable is expected
for Label
strStoreName = CType(myDataGridItem.FindControl("lblStoreName"), Label).Text

2:
Object reference not found

if(chkSelected.Checked)

the code works perfectly fine in VB


thanks

huzaifa s.m

Html
---------------
<asp:DataGrid id="DG_CheckBox" style="Z-INDEX: 101; LEFT: 80px; POSITION: absolute; TOP: 8px"
AutoGenerateColumns="False" Width="712px" HeaderStyle-BackColor="#ff0000" HeaderStyle-Font-Bold="True"
HeaderStyle-Font-Name="Verdana" HeaderStyle-Font-Size="13px" HeaderStyle-ForeColor="#ffffff"
ItemStyle-BackColor="Beige" ItemStyle-Font-Name="verdana" ItemStyle-Font-Size="13px" BorderColor="#000000"
Runat="server">
<Columns>
<asp:TemplateColumn HeaderStyle-HorizontalAlign="Center" HeaderText="X">
<ItemTemplate>
<asp:Checkbox ID="chkSelection" Runat="server" />
<asp:Label ID="hdnStoreID" Visible="False" Text='<%# DataBinder.Eval(Container.DataItem, "stor_id") %>' Runat=server />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Store Name">
<ItemTemplate>
<asp:Label ID="lblStoreName" Text='<%# DataBinder.Eval(Container.DataItem, "stor_name") %>' Runat=server />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Store Address">
<ItemTemplate>
<asp:Label ID="lblStoreAddress" Text='<%# DataBinder.Eval(Container.DataItem, "stor_address") %>' Runat=server />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="City">
<ItemTemplate>
<asp:Label ID="lblCity" Text='<%# DataBinder.Eval(Container.DataItem, "city") %>' Runat=server />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="State">
<ItemTemplate>
<asp:Label ID="lblState" Text='<%# DataBinder.Eval(Container.DataItem, "state") %>' Runat=server />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Zip">
<ItemTemplate>
<asp:Label ID="lblZip" Text='<%# DataBinder.Eval(Container.DataItem, "zip") %>' Runat=server />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>


VB File
------------------
Public Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

If Not Page.IsPostBack Then

BindGrid()

End If

End Sub



Private Sub BindGrid()

' Create Instance of Connection and Command Object

'Dim myConnection As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))
Dim myConnection As SqlConnection = New SqlConnection("Data Source=huzaifa;User id=sa;pwd=;Database=pubs;")
Dim myCommand As SqlCommand = New SqlCommand("sp_stores_sel", myConnection)



' Mark the Command as a SPROC

myCommand.CommandType = CommandType.StoredProcedure



Try

myConnection.Open()

DG_CheckBox.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection)

DG_CheckBox.DataBind()

Catch ex As Exception

Response.Write(ex.Message)

End Try

End Sub

Public Sub btnSelect_ServerClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelect.ServerClick

Dim myDataGridItem As DataGridItem

Dim chkSelected As System.Web.UI.WebControls.CheckBox

Dim strStoreName As String

Dim strStoreID As String



lblStatus.Text = "<br>You selected the Following items:<br><br>"

For Each myDataGridItem In DG_CheckBox.Items

chkSelected = myDataGridItem.FindControl("chkSelection")

If chkSelected.Checked Then

strStoreName = CType(myDataGridItem.FindControl("lblStoreName"), Label).Text

strStoreID = CType(myDataGridItem.FindControl("hdnStoreID"), Label).Text

lblStatus.Text += "The store name is <b>" & strStoreName & "</b>"

lblStatus.Text += " and the StoreID is <b>" & strStoreID & "</b><br>"

End If

Next

End Sub