Solved

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

Posted on 2009-04-13
5
427 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:anuragal
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Visual Studio .Net 2012 and Crystal Reports 9 43
SQL Login 17 37
Help with Syntax 9 24
Reset asp.net login password 4 25
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

943 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now