Solved

Conditional Gridview

Posted on 2010-08-25
6
349 Views
Last Modified: 2013-11-08
Hi,
I have created a Gridview where user can enter records.
Now the requirement is that,those people who don't have rights to enter the records should see records in read-only mode
(in label rather than in textBox).

we can know if the user has access to enter data or not.
Stored in a variable.

//Code to bind grid
private void BindExecutionSummary()
        {
            testcBL = new TestBL();
            List<Summary> lstSummary = new List<Summary>();
            lstSummary = testStatisticBL.GetSummary(Id);
            Gridview1.DataSource = lstSummary;
                Gridview1.DataBind();
      }

How can I display data in label/textbox in the Gridview.
Your help is really appreciated.Thanks
<asp:GridView ID="Gridview1" runat="server" AutoGenerateColumns="False" HeaderStyle-BackColor="#b3d5fc"
                                CellPadding="4" OnRowCommand="Gridview1_RowCommand" OnRowCreated="Gridview1_RowCreated"
                                OnRowDataBound="Gridview1_RowDataBound">
                                <Columns>
                                    <asp:TemplateField HeaderText="Test Area">
                                        <ItemTemplate>
                                            <asp:Label ID="lblTestAreaId" ForeColor="Black" runat="server" Visible="false" Text='<%# Bind("AreaId") %>'></asp:Label>
                                            <asp:Label ID="lblTestArea" ForeColor="Black" runat="server" Text='<%# Bind("TestCaseArea") %>'></asp:Label>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Total TC#" ItemStyle-Width="50px" ItemStyle-ForeColor="Black"
                                        HeaderStyle-HorizontalAlign="Right">
                                        <ItemTemplate>
                                            <asp:TextBox ID="txtTotalTc" CssClass="inputtextright" MaxLength="6" Width="50px"
                                                runat="server" Text='<%# Bind("TotalTest") %>'></asp:TextBox>
                                           
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Executed" HeaderStyle-HorizontalAlign="Right">
                                        <ItemTemplate>
                                            <asp:TextBox ID="txtExecuted" CssClass="inputtextright" MaxLength="6" Width="50px"
                                                runat="server" Text='<%# Bind("Executed") %>'></asp:TextBox>
                                            
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Pass" HeaderStyle-HorizontalAlign="Right">
                                        <ItemTemplate>
                                            <asp:TextBox ID="txtPass" runat="server" CssClass="inputtextright" MaxLength="6"
                                                Width="50px" Text='<%# Bind("Passed") %>'></asp:TextBox>
                                           
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Fail" HeaderStyle-HorizontalAlign="Right">
                                        <ItemTemplate>
                                            <asp:TextBox ID="txtFail" runat="server" CssClass="inputtextright" MaxLength="6"
                                                Width="50px" Text='<%# Bind("Failed") %>'></asp:TextBox>
                                           
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Not Testable" ItemStyle-Width="50px" HeaderStyle-HorizontalAlign="Right">
                                        <ItemTemplate>
                                            <asp:TextBox ID="txtNotest" runat="server" CssClass="inputtextright" MaxLength="6"
                                                Width="50px" Text='<%# Bind("NotTested") %>'></asp:TextBox>
                                           
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Blocked" HeaderStyle-HorizontalAlign="Right">
                                        <ItemTemplate>
                                            <asp:TextBox ID="txtBlock" runat="server" CssClass="inputtextright" MaxLength="6"
                                                Width="50px" Text='<%# Bind("Block") %>'></asp:TextBox>
                                           
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Executed %" HeaderStyle-HorizontalAlign="Right" ItemStyle-HorizontalAlign="Right">
                                        <ItemTemplate>
                                            <asp:TextBox ID="txtExecutePer" runat="server" CssClass="inputtextright" Width="50px"
                                                BorderColor="White" Text='<%# Bind("ExecutePercent") %>'></asp:TextBox>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Blocked %" HeaderStyle-HorizontalAlign="Right">
                                        <ItemTemplate>
                                            <asp:TextBox ID="txtBlockper" runat="server" CssClass="inputtextright" Width="50px"
                                                BorderColor="White" Text='<%# Bind("BlockePercent") %>'></asp:TextBox>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Pass %" HeaderStyle-HorizontalAlign="Right">
                                        <ItemTemplate>
                                            <asp:TextBox ID="txtPassPer" runat="server" CssClass="inputtextright" Width="50px"
                                                BorderColor="White" Text='<%# Bind("PassRate") %>'></asp:TextBox>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField>
                                        <ItemTemplate>
                                            <asp:ImageButton ID="ImgbtnSave" ToolTip="Save" runat="server" CommandName="Save"
                                                ImageUrl="save.JPG" />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                </Columns>
                                <PagerStyle HorizontalAlign="Center" />
                                <HeaderStyle />
                                <FooterStyle BackColor="#F2F2F2" />
                            </asp:GridView>

Open in new window

0
Comment
Question by:johny_bravo1
  • 3
  • 2
6 Comments
 
LVL 3

Expert Comment

by:firozmusthafa
Comment Utility
observe this sample code carefully, it has what you need:

<asp:GridView ID="grdstudent" runat="server" AutoGenerateColumns="False"
        CellPadding="4" ForeColor="#333333" GridLines="None" Height="179px"
        onrowcommand="grdstudent_RowCommand"
        style="margin-right: 85px; margin-bottom: 104px" Width="302px">
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <Columns>
            <asp:BoundField DataField="username" HeaderText="Username" />
            <asp:BoundField DataField="subscription" HeaderText="Subscription" />
            <asp:TemplateField HeaderText="Edit">
                <ItemTemplate>
                    <asp:LinkButton ID="lnkedit" runat="server" CommandName="edit" CommandArgument='<%#Eval["useid"]%>'>Edit</asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <EditRowStyle BackColor="#999999" />
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    </asp:GridView>

Hint: see command argument
0
 
LVL 8

Author Comment

by:johny_bravo1
Comment Utility
Sorry but I am not getting your point.
I need to display label instead of below field when user don't have access.
Not sure whether I need to put another templatefield for label or should include the labels in the same.

<asp:TemplateField HeaderText="Pass %" HeaderStyle-HorizontalAlign="Right">
 <ItemTemplate>
<asp:TextBox ID="txtPassPer" runat="server" CssClass="inputtextright" Width="50px"
    BorderColor="White" Text='<%# Bind("PassRate") %>'></asp:TextBox>
  </ItemTemplate>
</asp:TemplateField>
0
 
LVL 6

Accepted Solution

by:
thaytu888888 earned 500 total points
Comment Utility
- Here is an example I have just made for you:
<asp:GridView ID="grdView" runat="server" ondatabinding="grdView_DataBinding" 
            onrowdatabound="grdView_RowDataBound">
            <Columns>
                <asp:TemplateField>                    
                    <ItemTemplate>
                        <asp:Label ID="lblName" runat="server"></asp:Label>             
                        <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
                    </ItemTemplate>                    
                </asp:TemplateField>
            </Columns>
        </asp:GridView>



protected void grdView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        bool canEdit = false;
        Label lbl = (Label)e.Row.FindControl("txtName");
        TextBox txt = (TextBox)e.Row.FindControl("txtName");
        if(txt == null || lbl == null)
        {
            return;
        }
        if (canEdit)
        {            
            {
                //Display text box
                txt.Text = "Your value";
                txt.Visible = true;
                //Hide label
                lbl.Visible = false;
            }
        }
        else
        {
            //Display label
            lbl.Text = "Your value";
            lbl.Visible = true;
            //Hide text box
            txt.Visible = false;
        }
    }

Open in new window

0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 6

Expert Comment

by:thaytu888888
Comment Utility
- canEdit is belong to user permission.
0
 
LVL 8

Author Comment

by:johny_bravo1
Comment Utility
well I think that should work.I am working on it.Will update you soon
0
 
LVL 8

Author Closing Comment

by:johny_bravo1
Comment Utility
Thanks for the quick solutions
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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 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

11 Experts available now in Live!

Get 1:1 Help Now