Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to disable/enable a selected cell in a DataGRid view

Status
Not open for further replies.

Rod41

Programmer
Sep 24, 2015
7
0
0
PH
I need a sample code that will disable a selected cell in a DataGridView using VB.net code.
in my cell(10) there were two controls as TextBox. Only txtEntryDateTo_Store should be disabled.

Below is my sample code, kindly insert your code in between my script..


Dim todaydate As DateTime = DateTime.Now
Dim gridViewRow As GridViewRow
Dim intApprCount As Integer = 0
If grdTransaction_Store.Rows.Count > 0 Then
For Each gridViewRow In grdTransaction_Store.Rows
Dim lblEntryDateFrom_Store As Label = CType(gridViewRow.Cells(9).FindControl("lblEntryDateFrom_Store"), Label)
Dim lblExitDateFrom_Store As Label = CType(gridViewRow.Cells(9).FindControl("lblExitDateFrom_Store"), Label)
Dim txtEntryDateTo_Store As TextBox = CType(gridViewRow.Cells(10).FindControl("lblEntryDateTo_Store"), TextBox)
Dim txtExitDateTo_Store As TextBox = CType(gridViewRow.Cells(10).FindControl("lblExitDateTo_Store"), TextBox)
Dim ContractEntryDate As DateTime = Convert.ToDateTime(lblEntryDateFrom_Store.Text)
If ContractEntryDate.Month < todaydate.Month Then
'
' Place your sample code that will disable this current cell..
' gridViewRow.Cells(10).FindControl("lblEntryDateTo_Store")
'
'
End If
Next
End If
 
2 things:

First, please indent your code and put it in code tags. If you're going to ask us to help you, please make it easier for us to do so.

Code:
Dim todaydate As DateTime = DateTime.Now
Dim gridViewRow As GridViewRow
Dim intApprCount As Integer = 0
If grdTransaction_Store.Rows.Count > 0 Then
    For Each gridViewRow In grdTransaction_Store.Rows
        Dim lblEntryDateFrom_Store As Label = CType(gridViewRow.Cells(9).FindControl("lblEntryDateFrom_Store"), Label)
        Dim lblExitDateFrom_Store As Label = CType(gridViewRow.Cells(9).FindControl("lblExitDateFrom_Store"), Label)
        Dim txtEntryDateTo_Store As TextBox = CType(gridViewRow.Cells(10).FindControl("lblEntryDateTo_Store"), TextBox)
        Dim txtExitDateTo_Store As TextBox = CType(gridViewRow.Cells(10).FindControl("lblExitDateTo_Store"), TextBox)
        Dim ContractEntryDate As DateTime = Convert.ToDateTime(lblEntryDateFrom_Store.Text)
        If ContractEntryDate.Month < todaydate.Month Then
            '
            ' Place your sample code that will disable this current cell..
            ' gridViewRow.Cells(10).FindControl("lblEntryDateTo_Store")
            '
            '
        End If
    Next
End If

Second, what exactly is it that you want to do? The title says "disable/enable a selected cell", then in the post you say there are 2 textboxes in the cell and you want to disable only one of them, *not* the entire cell. Then later you again say you want to disable the entire cell ("Place your sample code that will disable this current cell.."). Then there's these 2 lines of code:

Dim txtEntryDateTo_Store As TextBox = CType(gridViewRow.Cells(10).FindControl("lblEntryDateTo_Store"), TextBox)
Dim txtExitDateTo_Store As TextBox = CType(gridViewRow.Cells(10).FindControl("lblExitDateTo_Store"), TextBox)

which indicate that the controls in the cell are actually Labels (assuming that's what the prefix "lbl" means), not TextBoxes.

So, perhaps a more high-level description of what you're trying to accomplish will help clarify things a bit.

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top