back up codes

always have a backup of your code

Archive for the tag “gridview”

find control inside gridview

Insert the following code on SelectedIndexChanged event of the gridview controlL:


Dim myRow As GridViewRow = myGridView.SelectedRow
Dim myTextBox1 As TextBox = CType(myRow.FindControl("myTextBox1"), TextBox)
Dim myTextBox2 As TextBox = CType(myRow.FindControl("myTextBox2"), TextBox)
If myTextBox1.Text = "" And myTextBox2.Text = "" Then
Exit Sub
Else
End If

display data on selected value of gridview

This code will display the data corresponding to the value of the data selected on gridview.

On the SelectedIndexChanged event of a gridview control, insert the following code:

Dim myID = myGrid.SelectedDataKey.Value
command = New Data.SqlClient.SqlCommand("SELECT * FROM THE_TABLE WHERE MY_ID = '" & myID & "'", connection)
reader = command.ExecuteReader()
If reader.Read() Then
Dim firstColumn As String = reader("FIRST_COLUMN")
Dim secondColumn As String = reader("SECOND_COLUMN")
Else
End If
reader.Close()

determines if checkbox inside gridview is checked

The following code determines if checkbox control inside gridview is checked or unchecked.


For Each myRow As GridViewRow In myGridview.Rows
Dim chkbx As CheckBox = myRow.FindControl("chkbxCheck")
If chkbx IsNot Nothing AndAlso chkbx.Checked AndAlso chkbx.Enabled Then
(do something here if checkbox is checked)
Else
(do something here if checkbox is not checked)
End If
Next

enable/disable checkbox inside gridview

This code will check/uncheck and enable/disable a checkbox inside a gridview, depending if data is already found on database.

Insert the following code on the DataBound event of the gridview control:

For Each myRow As GridViewRow In myGrid.Rows
Dim thechkbx As CheckBox = CType(myRow.FindControl("myChkbx"), CheckBox)
Dim myID As String = myGrid.DataKeys(myRow.RowIndex).Value
command = New Data.SqlClient.SqlCommand("SELECT * FROM MY_TABLE WHERE MY_PK = '" & myID & "'", connection)
reader = command.ExecuteReader()
If reader.Read() Then
thechkbx.Enabled = True
thechkbx.Checked = True
Else
thechkbx.Enabled = False
thechkbx.Checked = False
End If
reader.Close()
Next

datarow/footer of a gridview

On the RowDataBound event of a gridview insert this code:

This will check the value of a row:

If e.Row.RowType = DataControlRowType.DataRow Then
If (e.Row.DataItem.Item("COLUMN_NAME")) IsNot DBNull.Value Then
Dim myValue As String = (e.Row.DataItem.Item("COLUMN_NAME"))
Else
End If
End If

The following code will find a control inside a gridview footer:

If e.Row.RowType = DataControlRowType.Footer Then
Dim myLabel As Label = CType(e.Row.FindControl("theLabel"), Label)
End If

Post Navigation

Follow

Get every new post delivered to your Inbox.