locked
vb.net grid view row edit with condition RRS feed

  • Question

  • User-1578974752 posted

    In below code ,Upon clicking EDIT button,i can make two columns editable by placing the textbox and label box and visibility as below:

    But Now I have to put a condition such that if seventh column in my gridview is having a value= 0 then  that row must become non editable means label.Visible = true. But all other rows can be editable. How can I fetch the seventh column of the gridview in this edit button

    Protected Sub Edit_Click(sender As Object, e As EventArgs) Handles Edit.Click

                            For Each item As GridViewRow In kgrid.Rows

                If item.RowType = DataControlRowType.DataRow Then

                    Dim label As Label = CType(item.FindControl("Label1"), Label)
                    Dim box As TextBox = CType(item.FindControl("TextBox1"), TextBox)
                    label.Visible = False
                    box.Visible = True


                    Dim label1 As Label = CType(item.FindControl("Label94"), Label)
                    Dim box1 As TextBox = CType(item.FindControl("TextBox2"), TextBox)
                    label1.Visible = False
                    box1.Visible = True

        
                End If
            Next

    Appreciate the help

    Thursday, November 15, 2018 3:45 AM

Answers

  • User-1716253493 posted

    way 1

    visible='<%# not eval("colx") %>'
    visible='<%# eval("colx") %>'
    visible='<%# IIF(eval("colx")="abc",1,0) %>'

    way 2

    Dim isvisible = Convert.ToBolean(DataBinder.Eval(e.Row.DataItem,"field7"))
    Dim isvisible as Boolean = IIF(DataBinder.Eval(e.Row.DataItem,"field7")=0,True,False)
    Label1.Visible = isvisible

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 15, 2018 7:32 AM
  • User283571144 posted

    Hi shsu,

    If you use custom template(TemplateField) in your gridview, I suggest you could also use item.FindControl to find the column7's label control and take its value. If you use asp:BoundField, I suggest you could use item.Cells(7).text to get the value.

    More details, you could refer to below test demo:

    Notice: I used custom template(TemplateField) in my gridview.

    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="GrdiviewEditParameter.aspx.vb" Inherits="VBWebform.GrdiviewEditParameter" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:Button ID="Edit" runat="server" Text="Button" OnClick="Edit_Click"  />
     
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="MyId" DataSourceID="SqlDataSource1" AutoGenerateEditButton="True" OnRowEditing="GridView1_RowEditing" >
                    <Columns>
                        <asp:TemplateField HeaderText="MyId" InsertVisible="False" SortExpression="MyId">
                            <EditItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Eval("MyId") %>'></asp:Label>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Bind("MyId") %>'></asp:Label>
                                  <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("MyId") %>' Visible="false"></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="FriendId" SortExpression="FriendId">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("FriendId") %>'></asp:TextBox>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label2" runat="server" Text='<%# Bind("FriendId") %>'></asp:Label>
                                 <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("FriendId") %>' Visible="false"></asp:TextBox>
    
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="FriendStatus" SortExpression="FriendStatus">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("FriendStatus") %>'></asp:TextBox>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label3" runat="server" Text='<%# Bind("FriendStatus") %>'></asp:Label>
                                <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("FriendStatus") %>' Visible="false"></asp:TextBox>
    
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:aspnet-MVCApplication-20180424014300ConnectionString %>" DeleteCommand="DELETE FROM [Friends] WHERE [MyId] = @MyId" InsertCommand="INSERT INTO [Friends] ([FriendId], [FriendStatus]) VALUES (@FriendId, @FriendStatus)" SelectCommand="SELECT * FROM [Friends]" UpdateCommand="UPDATE [Friends] SET [FriendId] = @FriendId, [FriendStatus] = @FriendStatus WHERE [MyId] = @MyId">
                    <DeleteParameters>
                        <asp:Parameter Name="MyId" Type="Int32" />
                    </DeleteParameters>
                    <InsertParameters>
                        <asp:Parameter Name="FriendId" Type="Int32" />
                        <asp:Parameter Name="FriendStatus" Type="Int32" />
                    </InsertParameters>
                    <UpdateParameters>
                        <asp:Parameter Name="FriendId" Type="Int32" />
                        <asp:Parameter Name="FriendStatus" Type="Int32" />
                        <asp:Parameter Name="MyId" Type="Int32" />
                    </UpdateParameters>
                </asp:SqlDataSource>
    
            </div>
        </form>
    </body>
    </html>
    

    Code-behind:

    Public Class GrdiviewEditParameter
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        End Sub
    
        Protected Sub Edit_Click(sender As Object, e As EventArgs)
            For Each item As GridViewRow In GridView1.Rows
    
                If item.RowType = DataControlRowType.DataRow Then
                    Dim Value As Int32 = CType(CType(item.FindControl("Label3"), Label).Text, Int32)
                    Dim re = Convert.ToBoolean(Value)
                    If re Then
                        Dim label As Label = CType(item.FindControl("Label1"), Label)
                        Dim box As TextBox = CType(item.FindControl("TextBox1"), TextBox)
                        label.Visible = False
                        box.Visible = True
                        Dim label1 As Label = CType(item.FindControl("Label2"), Label)
                        Dim box1 As TextBox = CType(item.FindControl("TextBox2"), TextBox)
                        label1.Visible = False
                        box1.Visible = True
    
                    End If
                End If
    
    
    
            Next
        End Sub
    End Class

    Result:

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 22, 2018 8:41 AM
  • User-1716253493 posted

    You can bind bit column to edit button

    enabled ='<%# eval("col7") %>'

    col7 must contain 0 or 1 value, so you can't click the edit button in the row containing col7=0 value

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 22, 2018 8:48 AM

All replies

  • User-1716253493 posted

    way 1

    visible='<%# not eval("colx") %>'
    visible='<%# eval("colx") %>'
    visible='<%# IIF(eval("colx")="abc",1,0) %>'

    way 2

    Dim isvisible = Convert.ToBolean(DataBinder.Eval(e.Row.DataItem,"field7"))
    Dim isvisible as Boolean = IIF(DataBinder.Eval(e.Row.DataItem,"field7")=0,True,False)
    Label1.Visible = isvisible

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 15, 2018 7:32 AM
  • User283571144 posted

    Hi shsu,

    If you use custom template(TemplateField) in your gridview, I suggest you could also use item.FindControl to find the column7's label control and take its value. If you use asp:BoundField, I suggest you could use item.Cells(7).text to get the value.

    More details, you could refer to below test demo:

    Notice: I used custom template(TemplateField) in my gridview.

    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="GrdiviewEditParameter.aspx.vb" Inherits="VBWebform.GrdiviewEditParameter" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:Button ID="Edit" runat="server" Text="Button" OnClick="Edit_Click"  />
     
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="MyId" DataSourceID="SqlDataSource1" AutoGenerateEditButton="True" OnRowEditing="GridView1_RowEditing" >
                    <Columns>
                        <asp:TemplateField HeaderText="MyId" InsertVisible="False" SortExpression="MyId">
                            <EditItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Eval("MyId") %>'></asp:Label>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Bind("MyId") %>'></asp:Label>
                                  <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("MyId") %>' Visible="false"></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="FriendId" SortExpression="FriendId">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("FriendId") %>'></asp:TextBox>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label2" runat="server" Text='<%# Bind("FriendId") %>'></asp:Label>
                                 <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("FriendId") %>' Visible="false"></asp:TextBox>
    
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="FriendStatus" SortExpression="FriendStatus">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("FriendStatus") %>'></asp:TextBox>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label3" runat="server" Text='<%# Bind("FriendStatus") %>'></asp:Label>
                                <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("FriendStatus") %>' Visible="false"></asp:TextBox>
    
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:aspnet-MVCApplication-20180424014300ConnectionString %>" DeleteCommand="DELETE FROM [Friends] WHERE [MyId] = @MyId" InsertCommand="INSERT INTO [Friends] ([FriendId], [FriendStatus]) VALUES (@FriendId, @FriendStatus)" SelectCommand="SELECT * FROM [Friends]" UpdateCommand="UPDATE [Friends] SET [FriendId] = @FriendId, [FriendStatus] = @FriendStatus WHERE [MyId] = @MyId">
                    <DeleteParameters>
                        <asp:Parameter Name="MyId" Type="Int32" />
                    </DeleteParameters>
                    <InsertParameters>
                        <asp:Parameter Name="FriendId" Type="Int32" />
                        <asp:Parameter Name="FriendStatus" Type="Int32" />
                    </InsertParameters>
                    <UpdateParameters>
                        <asp:Parameter Name="FriendId" Type="Int32" />
                        <asp:Parameter Name="FriendStatus" Type="Int32" />
                        <asp:Parameter Name="MyId" Type="Int32" />
                    </UpdateParameters>
                </asp:SqlDataSource>
    
            </div>
        </form>
    </body>
    </html>
    

    Code-behind:

    Public Class GrdiviewEditParameter
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        End Sub
    
        Protected Sub Edit_Click(sender As Object, e As EventArgs)
            For Each item As GridViewRow In GridView1.Rows
    
                If item.RowType = DataControlRowType.DataRow Then
                    Dim Value As Int32 = CType(CType(item.FindControl("Label3"), Label).Text, Int32)
                    Dim re = Convert.ToBoolean(Value)
                    If re Then
                        Dim label As Label = CType(item.FindControl("Label1"), Label)
                        Dim box As TextBox = CType(item.FindControl("TextBox1"), TextBox)
                        label.Visible = False
                        box.Visible = True
                        Dim label1 As Label = CType(item.FindControl("Label2"), Label)
                        Dim box1 As TextBox = CType(item.FindControl("TextBox2"), TextBox)
                        label1.Visible = False
                        box1.Visible = True
    
                    End If
                End If
    
    
    
            Next
        End Sub
    End Class

    Result:

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 22, 2018 8:41 AM
  • User-1716253493 posted

    You can bind bit column to edit button

    enabled ='<%# eval("col7") %>'

    col7 must contain 0 or 1 value, so you can't click the edit button in the row containing col7=0 value

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 22, 2018 8:48 AM