Solved

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

Posted on 2009-04-13
5
429 Views
Last Modified: 2013-11-07
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 ..how 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">
            <Columns>
                <asp:TemplateColumn HeaderText="name">
                    <ItemTemplate>
                        <asp:Label ID="label1" runat="server" Text='<%# Bind("name") %>'></asp:Label>
                    </ItemTemplate>
                    </asp:TemplateColumn>
                <asp:ButtonColumn CommandName="Delete" HeaderText="Delete" Text="Delete"></asp:ButtonColumn>
            </Columns>
        </asp:DataGrid>
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
            BindToGrid()
        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)
        da.Fill(ds)
        DataGrid1.DataSource = ds
        Try
            DataGrid1.DataBind()
        Catch
            DataGrid1.CurrentPageIndex = 0
            BindToGrid()
        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)
        con.Open()
        Try
            Cmd.ExecuteNonQuery()
        Catch
            Response.Write("Nothing")
        End Try
        DataGrid1.EditItemIndex = -1
        con.Close()
        BindToGrid()
    End Sub
 
 
End Class

Open in new window

0
Comment
Question by:Rajar Ahmed
  • 3
5 Comments
 
LVL 11

Expert Comment

by:Anurag Agarwal
ID: 24129845
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
http://www.codeproject.com/KB/webforms/ItemCreated.aspx
Anurag
0
 
LVL 1

Expert Comment

by:Madhubalan
ID: 24135261
Hey .. you can Use the ItemDataBound in asp.net1.1 and RowDatabound in asp.net 2.0 there you can check like,
   If DataBinder.Eval(e.Row.DataItem, "chk").ToString = "1" Then
                        deletebutton.Visible  = false
    End if
0
 
LVL 1

Expert Comment

by:Madhubalan
ID: 24135267
Hey if u want to hide then use visible = false
or if u want to disable then make the button property to readonly
0
 
LVL 18

Author Comment

by:Rajar Ahmed
ID: 24135521

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
0
 
LVL 1

Accepted Solution

by:
Madhubalan earned 500 total points
ID: 24149162
Hey it seems you are using datagrid that is asp.net 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"
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question