Solved

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

Posted on 2009-04-13
5
426 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

12 Experts available now in Live!

Get 1:1 Help Now