Solved

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

Posted on 2009-04-13
5
431 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

738 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