Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Conditional Gridview

Posted on 2010-08-25
6
Medium Priority
?
362 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
[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
  • 2
6 Comments
 
LVL 3

Expert Comment

by:firozmusthafa
ID: 33519833
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
ID: 33519909
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 2000 total points
ID: 33519927
- 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:thaytu888888
ID: 33519935
- canEdit is belong to user permission.
0
 
LVL 8

Author Comment

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

Author Closing Comment

by:johny_bravo1
ID: 33520841
Thanks for the quick solutions
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

636 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