Solved

Conditional Gridview

Posted on 2010-08-25
6
357 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone 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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
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…

829 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