How to disable the delete button of datagrid ,according to db value...??

i have binded the datagrid with delete button , problem here i need to disable the delete button of datagrid if chk column of mytable (tbl1)contains value 1...
 if  chk, contains 0 that row  can be deleted can i do it..?

tbl1 contains these data's..
id name       chk
1  mathews  1
2  Julie         0
3  King         1
4  Romeo     1
5  Mose       0

<asp:DataGrid ID="DataGrid1" runat="server" AutoGenerateColumns="False" Style="left: 320px;
            position: relative; top: 147px" DataKeyField="id">
                <asp:TemplateColumn HeaderText="name">
                        <asp:Label ID="label1" runat="server" Text='<%# Bind("name") %>'></asp:Label>
                <asp:ButtonColumn CommandName="Delete" HeaderText="Delete" Text="Delete"></asp:ButtonColumn>
mports System.Data
Imports System.Data.SqlClient
Partial Class deletedisable
    Inherits System.Web.UI.Page
    Dim con As SqlConnection
    Dim Cmd As SqlCommand
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        Dim constr As String = ConfigurationManager.AppSettings("con")
        con = New SqlConnection(constr)
        Cmd = New SqlCommand()
        Cmd.Connection = con
        If Not IsPostBack Then
        End If
    End Sub
    Private Sub BindToGrid()
        Cmd = New SqlCommand("Select * from tbl1 ", con)
        Dim ds As New DataSet()
        Dim da As New SqlDataAdapter(Cmd)
        DataGrid1.DataSource = ds
            DataGrid1.CurrentPageIndex = 0
        End Try
    End Sub
    Protected Sub DataGrid1_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.DeleteCommand
        Cmd = New SqlCommand("delete from tbl1 where id='" & DataGrid1.DataKeys(e.Item.ItemIndex) & "'", con)
        End Try
        DataGrid1.EditItemIndex = -1
    End Sub
End Class

Open in new window

LVL 18
Rajar AhmedConsultantAsked:
Who is Participating?
MadhubalanConnect With a Mentor Commented:
Hey it seems you are using datagrid that is 1.0
If youa re gng to get the value from database then use lilke the below code
Trim(DataBinder.Eval(e.Item.DataItem, "Chk")) ="1"
Anurag AgarwalPython DeveloperCommented:
Use ItemDataBound event of Datagrid. This event will be fired when the rows of the grid are binded to data.
so the control will come into this event for each row. Check the value of "chk" column and accordingly disable the delete button.
for more details follow the below link
Hey .. you can Use the ItemDataBound in asp.net1.1 and RowDatabound in 2.0 there you can check like,
   If DataBinder.Eval(e.Row.DataItem, "chk").ToString = "1" Then
                        deletebutton.Visible  = false
    End if
Hey if u want to hide then use visible = false
or if u want to disable then make the button property to readonly
Rajar AhmedConsultantAuthor Commented:

can u plz  tell me ,. how to get the chk column value in itemdatabound .....becz i get this error,
 'Row' is not a member of 'System.Web.UI.WebControls.DataGridItemEventArgs'.    

Protected Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
 If DataBinder.Eval(e.Row.DataItem, "chk").ToString = "1" Then
            If e.Item.ItemType = ListItemType.EditItem Then
                Dim lb As LinkButton = DirectCast(e.Item.Cells(1).Controls(0), LinkButton)
                lb.Enabled = False
            End If
        End If
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.