?
Solved

Conditional Gridview

Posted on 2010-08-25
6
Medium Priority
?
361 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
Independent Software Vendors: 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

762 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