Skip to main content

ASP.net GridView CheckBox

Ancient piece of code that I keep forgetting.

<asp:GridView ID="grdR" runat="server" AutoGenerateColumns="False"
            DataKeyNames="Id" style="margin-left: 6px; font-size: x-small;" Font-Size="Small">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:CheckBox ID="CheckBox1" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:CommandField SelectText="Edit" ShowSelectButton="True" />
                <asp:BoundField DataField="Length" HeaderText="Length" />
                <asp:BoundField DataField="Breadth" HeaderText="Breadth" />
            </Columns>
            <EmptyDataTemplate>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; There is no Value&nbsp;&nbsp;&nbsp;&nbsp;
            </EmptyDataTemplate>
        </asp:GridView>

    Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Dim i As Integer
        ' Dim bidlist(grdR.Rows.Count) As Integer
        For i = 0 To grdR.Rows.Count - 1
            Dim cbgrd As CheckBox = CType(grdR.Rows(i).FindControl("CheckBox1"), CheckBox)
            If (cbgrd.Checked) Then
                Dim rowId As Integer = grdR.DataKeys(grdR.Rows(i).DataItemIndex).Value
          ' do what you want

            End If
        Next

Comments

Popular posts from this blog

Siemens Washing Machine : Kill the Buzzer

The washing machine we have has the habit of sending out an irritating beep after it finishes the wash cycle. Ok enough; but these intermittent beeps go on and on and on till it is sure everyone came back home after your attending your funeral. One awful engineer who programmed the chip. Anyway, been looking around the net if there is a work around. Found this written by some Russian. I didn’t have patience to correct the grammar entirely. If the below does  not work, there is always the option of an Axe and ‘hey Siemens,..Heeeere is Johnyyy!) -- text You can change the volume of the buzzer according to your requirement. The operation procedure: 1. Switch on the machine,turn the program selector to Off . 2. Turn the program selector to cold Easy-care , press the additional function button Intensive stains and dont let go. You can hear the volume of the buzzer from minimum to maximum to off cycled (I didnt hear this!). If you decide the volume that yo...

Getting ENUM values by Reflection

Recently I had this problem, where I had  to get the Items and Values of an Enum within a class. The class name and enum name (as strings) are known and the instance itself has to be created at runtime. i.e; via Reflection Namespace N1abcd .... Public Class TestClass     Public Enum TestEnum         val1 = 20         val2 = 30     End Enum End Class The syntax in VB.net is as below '   System.Type.GetType("Namespace Name" + "." + "Class Name" + "+" + "Enum Name") Dim fieldInfos() As System.Reflection.FieldInfo = System.Type.GetType(" N1abcd .TestClass+TestEnum").GetFields             For Each f As System.Reflection.FieldInfo In fieldInfos                 If f.IsLiteral Then            ...

SQL Server - Get columns and data types from all tables

Useful script USE SmartHire GO  SELECT TABLE_NAME, COLUMN_NAME, COLUMNPROPERTY(OBJECT_ID(TABLE_SCHEMA + '.' + TABLE_NAME), COLUMN_NAME, 'ColumnID') AS COLUMN_ID, DATA_TYPE  FROM SmartHire.INFORMATION_SCHEMA.COLUMNS  GO   Output TABLE_NAME COLUMN_NAME COLUMN_ID DATA_TYPE TblBankDetails BankID 1 int TblBankDetails BankName 2 varchar TblContactPreferences ContactPreferenceID 1 int