Solved

Conditional Gridview

Posted on 2010-08-25
6
358 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
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 500 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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

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…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

730 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