Navigation

Search

Categories

On this page

Checkbox in a gridview which turns the selected row a different color when checked

Archive

Blogroll

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

RSS 2.0 | Atom 1.0 | CDF

Send mail to the author(s) E-mail

Total Posts: 112
This Year: 50
This Month: 4
This Week: 0
Comments: 0

Sign In

 Thursday, November 08, 2007
Thursday, November 08, 2007 8:57:04 PM (Eastern Standard Time, UTC-05:00) ( )

This example is a gridview contains a checkbox which when checked turns the selected row a different color.  

<asp:GridView ID="MyGridView" runat="server">
<Columns>
<asp:TemplateField>
    <ItemTemplate>
       <asp:CheckBox ID="MyCheckBox" runat="server" AutoPostBack="true" OnCheckedChanged="CheckBox1_CheckedChanged" />
    </ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
  Dim DataTable As New Data.DataTable
  DataTable.Columns.Add("Column1", GetType(String))
  DataTable.Columns.Add("Column2", GetType(String))
  Dim DataRow As Data.DataRow = DataTable.NewRow
  DataRow.Item(0) = "Test1"
  DataRow.Item(1) = "Test2"
  DataTable.Rows.Add(DataRow)
  MyGridView.DataSource = DataTable
  MyGridView.DataBind()
End If
End Sub

Protected Sub CheckBox1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
  Dim checkbox As CheckBox = CType(sender, CheckBox)
  Dim row As GridViewRow = CType(checkbox.NamingContainer, GridViewRow)
   If checkbox.Checked = True Then
     row.BackColor = Drawing.Color.Red
     mygridview.Columns(0).Visible = False
   End If
End Sub