Rajar Ahmed
asked on
ITEMDATABOUND problem,....stuck to retrive value from db..
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 retrieve the chk column in the itemdatabound event so that i can able to disable or enable the delete button.,..Plzzzz suggestttttttttttttt...... .
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:Button Column>
</Columns>
</asp:DataGrid>
if chk, contains 0 that row can be deleted ......how can i retrieve the chk column in the itemdatabound event so that i can able to disable or enable the delete button.,..Plzzzz suggestttttttttttttt......
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
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:Button
</Columns>
</asp:DataGrid>
imports System.Data
Imports System.Data.SqlClient
Partial Class deletedisable
Inherits System.Web.UI.Page
Dim con As SqlConnection
Dim Cmd As SqlCommand
Protected Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
'How can i retrive chk column value here.......???????????????
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 Sub
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
ASKER
i get this error, in this line....in the catch block/......
Dim lb As LinkButton = DirectCast(e.Item.Cells(1) .Controls( 0), LinkButto
Specified argument was out of the range of valid values.
Parameter name: index
If e.Item.ItemType = ListItemType.Header Then
Dim lb As LinkButton = DirectCast(e.Item.Cells(1) .Controls( 0), LinkButton)
lb.Enabled = Convert.ToBoolean(lb.Attri butes("myC hk"))
End If
Dim lb As LinkButton = DirectCast(e.Item.Cells(1)
Specified argument was out of the range of valid values.
Parameter name: index
If e.Item.ItemType = ListItemType.Header Then
Dim lb As LinkButton = DirectCast(e.Item.Cells(1)
lb.Enabled = Convert.ToBoolean(lb.Attri
End If
Don't use indexers to get the control. Instead use
Dim lb As Label= DirectCast(e.Item.FindCont rol("label 1"), Label)
Dim lb As Label= DirectCast(e.Item.FindCont
ASKER
hi GiftsonDJohn,
Now i get this error, in the second line , in catch block..
lb.Enabled = Convert.ToBoolean(lb.Attri butes("lab el2"))
Object reference not set to an instance of an object.
i dunno whethr i retrieved chk column value correectly or not, plz help me.......
If e.Item.ItemType = ListItemType.Header Then
Dim lb As LinkButton = DirectCast(e.Item.FindCont rol("Delet e"), LinkButton)
lb.Enabled = Convert.ToBoolean(lb.Attri butes("lab el2"))
End If
Now i get this error, in the second line , in catch block..
lb.Enabled = Convert.ToBoolean(lb.Attri
Object reference not set to an instance of an object.
i dunno whethr i retrieved chk column value correectly or not, plz help me.......
If e.Item.ItemType = ListItemType.Header Then
Dim lb As LinkButton = DirectCast(e.Item.FindCont
lb.Enabled = Convert.ToBoolean(lb.Attri
End If
<asp:DataGrid ID="DataGrid1" runat="server" AutoGenerateColumns="False" Style="left: 320px;
position: relative; top: 147px" DataKeyField="trid">
<Columns>
<asp:TemplateColumn HeaderText="name">
<ItemTemplate>
<asp:Label ID="label1" runat="server" Text='<%# Bind("name") %>' ></asp:Label>
<asp:Label ID="label2" runat="server" Text='<%# Bind("chk") %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:ButtonColumn CommandName="Delete" HeaderText="Delete" Text="Delete"></asp:ButtonColumn>
</Columns>
</asp:DataGrid>
Can you tell me what you are planning to do?
also there is no attribute called label2.
<asp:Label ID="label1" runat="server" Text='<%# Bind("name") %>' myChk='<%# Eval("chk") %>'></asp:Label>
Dim lb As Label= DirectCast(e.Item.FindCont rol("label 1"), Label)
lb.Enabled = Convert.ToBoolean(lb.Attri butes("myC hk"))
also there is no attribute called label2.
<asp:Label ID="label1" runat="server" Text='<%# Bind("name") %>' myChk='<%# Eval("chk") %>'></asp:Label>
Dim lb As Label= DirectCast(e.Item.FindCont
lb.Enabled = Convert.ToBoolean(lb.Attri
ASKER
Hi GiftsonDjohn,
If i try this code, i get the following error,
Two-way binding is only supported for properties. 'myChk' is not a valid property on 'Label'
<asp:Label ID="label1" runat="server" Text='<%# Bind("name") %>' myChk='<%# Eval("chk") %>'></asp:Label>
Dim lb As Label= DirectCast(e.Item.FindCont
lb.Enabled = Convert.ToBoolean(lb.Attri
This is my scenario,
Step 1:I binded the datagrid.... with BindToGrid() ,
Step 2: Datagrid holds 2 columns Name and deletebutton.
Step 3: Now i need to disable or enable the delete button in the datagrid according to value of chk column in database.....
Step 4 : if chk column contains 0 then delete button should be enable
if chk column contains 1 then delete button should be Disable
Hope this Clear......
This is my table | The output should be like this;
id name chk | (Data Grid Should be Displayed Like this)
1 mathews 1 | mathews delete(disable)
2 Julie 0 | julie delete(enable)
3 King 1 | King delete(disable)
4 Romeo 1 | Romeo delete(disable)
5 Mose 0 | Mose delete(enable)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
hi GiftsonDJohn,
i did by binding from database....
Thanksss a lot mate.......
i did by binding from database....
Thanksss a lot mate.......
Protected Sub GridView2_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView2.RowDataBound
e.Row.Cells(1).Visible = False
Dim delbutton As LinkButton = TryCast(e.Row.FindControl("Button1"), LinkButton)
Response.Write(e.Row.Cells(1).Text)
' Dim chk As Boolean = Convert.ToBoolean(e.Row.Cells(1).Text)
If e.Row.Cells(1).Text = "1" Then
delbutton.Enabled = False
ElseIf e.Row.Cells(1).Text = "0" Then
delbutton.Enabled = True
End If
'delbutton.Enabled = Not chk
End Sub
End Class
ASKER
...thankss again.,...
<asp:Label ID="label1" runat="server" Text='<%# Bind("name") %>' myChk='<%# Eval("chk") %>'></asp:Label>
If e.Item.ItemType = ListItemType.EditItem Then
Dim lb As LinkButton = DirectCast(e.Item.Cells(1)
lb.Enabled = Convert.toBoolean(lb.Attri
End If